# DeepSeek-OCR Model

DeepSeek-OCR is an advanced OCR model capable of recognizing text in images and converting it into specified text formats.

## Request Example

You can use the DeepSeek-OCR model by sending a request to the `https://api-us-ca.umodelverse.ai/v1/chat/completions` endpoint.

> **Note:**
> DeepSeek-OCR supports a maximum `max_tokens` value of **8192**. The model is currently available for free.
>
> **Important:** This model only accepts base64-encoded images (i.e., "data:image/..." format) as input. It does not support direct image URLs. If your image is hosted remotely, you can obtain the base64 string using the following command:
>
> ```bash
> curl -s https://umodelverse-inference.cn-wlcb.ufileos.com/ucloud-maxcot.jpg | base64 | tr -d '\n'
> ```

### Non-Streaming Request

<!-- tabs:start -->
#### **cURL**
```bash
curl https://api-us-ca.umodelverse.ai/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $YOUR_API_KEY" \
  -d '{
    "model": "deepseek-ai/DeepSeek-OCR",
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "type": "text",
            "text": "convert to markdown"
          },
          {
            "type": "image_url",
            "image_url": {
              "url": "data:image/jpeg;base64,'$(curl -s https://umodelverse-inference.cn-wlcb.ufileos.com/ucloud-maxcot.jpg | base64 | tr -d '\n')'"
            }
          }
        ]
      }
    ]
  }'
```
#### **Python**
```python
import base64
import os
from openai import OpenAI

# Function to encode the image
def encode_image(image_path):
  with open(image_path, "rb") as image_file:
    return base64.b64encode(image_file.read()).decode('utf-8')

# Path to your image
image_path = os.path.expanduser("ucloud.png")

# Getting the base64 string
base64_image = encode_image(image_path)

client = OpenAI(
    api_key=os.getenv("MODELVERSE_API_KEY", "<YOUR_MODELVERSE_API_KEY>"),
    base_url="https://api-us-ca.umodelverse.ai/v1/",
)

response = client.chat.completions.create(
  model="deepseek-ai/DeepSeek-OCR",
  messages=[
    {
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": "convert to markdown"
        },
        {
          "type": "image_url",
          "image_url": {
            "url": f"data:image/jpeg;base64,{base64_image}"
          }
        }
      ]
    }
  ]
)

print(response.choices[0].message.content)
```
<!-- tabs:end -->

### Streaming Request

By setting the `stream` parameter to `true`, you can enable streaming responses.


<!-- tabs:start -->
#### **cURL**
```bash
curl https://api-us-ca.umodelverse.ai/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $YOUR_API_KEY" \
  -d '{
    "model": "deepseek-ai/DeepSeek-OCR",
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "type": "text",
            "text": "convert to markdown"
          },
          {
            "type": "image_url",
            "image_url": {
              "url": "data:image/jpeg;base64,'$(curl -s https://umodelverse-inference.cn-wlcb.ufileos.com/ucloud-maxcot.jpg | base64 | tr -d '\n')'"
            }
          }
        ]
      }
    ],
    "stream": true
  }'
```
#### **Python**
```python
import base64
import os
from openai import OpenAI

# Function to encode the image
def encode_image(image_path):
  with open(image_path, "rb") as image_file:
    return base64.b64encode(image_file.read()).decode('utf-8')

# Path to your image
image_path = os.path.expanduser("ucloud.png")

# Getting the base64 string
base64_image = encode_image(image_path)

client = OpenAI(
    api_key=os.getenv("MODELVERSE_API_KEY", "<YOUR_MODELVERSE_API_KEY>"),
    base_url="https://api-us-ca.umodelverse.ai/v1/",
)

stream = client.chat.completions.create(
    model="deepseek-ai/DeepSeek-OCR",
    messages=[
        {
          "role": "user",
          "content": [
            {
              "type": "text",
              "text": "convert to markdown"
            },
            {
              "type": "image_url",
              "image_url": {
                "url": f"data:image/jpeg;base64,{base64_image}"
              }
            }
          ]
        }
    ],
    stream=True,
)

for chunk in stream:
    print(chunk.choices[0].delta.content or "", end="")
```
<!-- tabs:end -->
