Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
DavdGao committed May 14, 2024
1 parent 77faa3a commit 7a0fd47
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 0 deletions.
71 changes: 71 additions & 0 deletions docs/sphinx_doc/en/source/tutorial/206-prompt.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ dictionaries as input, where the dictionary must obey the following rules

#### Prompt Strategy

##### Non-Vision Models

In OpenAI Chat API, the `name` field enables the model to distinguish
different speakers in the conversation. Therefore, the strategy of `format`
function in `OpenAIChatWrapper` is simple:
Expand Down Expand Up @@ -100,6 +102,75 @@ print(prompt)
]
```

##### Vision Models

For vision models (gpt-4-turbo, gpt-4o, ...), if the input message contains image urls, the generated `content` field will be a list of dicts, which contains text and image urls.

Specifically, the web image urls will be pass to OpenAI Chat API directly, while the local image urls will be converted to base64 format. More details please refer to the [official guidance](https://platform.openai.com/docs/guides/vision).

Note the invalid image urls (e.g. `/Users/xxx/test.mp3`) will be ignored.

```python
from agentscope.models import OpenAIChatWrapper
from agentscope.message import Msg

model = OpenAIChatWrapper(
config_name="", # empty since we directly initialize the model wrapper
model_name="gpt-4",
)

prompt = model.format(
Msg("system", "You're a helpful assistant", role="system"), # Msg object
[ # a list of Msg objects
Msg(name="user", content="Describe this image", role="user", url="https://xxx.png"),
Msg(name="user", content="And these images", role="user", urls=["/Users/xxx/test.png", "/Users/xxx/test.mp3"]),
],
)
print(prompt)
```

```python
[
{
"role": "system",
"name": "system",
"content": "You are a helpful assistant"
},
{
"role": "user",
"name": "user",
"content": [
{
"type": "text",
"text": "Describe this image"
},
{
"type": "image_url",
"image_url": {
"url": "https://xxx.png"
}
},
]
},
{
"role": "user",
"name": "user",
"content": [
{
"type": "text",
"text": "And these images"
},
{
"type": "image_url",
"image_url": {
"url": "data:image/png;base64,YWJjZGVm..." # for /Users/xxx/test.png
}
},
]
},
]
```

### DashScopeChatWrapper

`DashScopeChatWrapper` encapsulates the DashScope chat API, which takes a list of messages as input. The message must obey the following rules (updated in 2024/03/22):
Expand Down
71 changes: 71 additions & 0 deletions docs/sphinx_doc/zh_CN/source/tutorial/206-prompt.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ AgentScope为以下的模型API提供了内置的提示构建策略。

#### 提示的构建策略

##### 非视觉(Vision)模型

在OpenAI Chat API中,`name`字段使模型能够区分对话中的不同发言者。因此,`OpenAIChatWrapper``format`函数的策略很简单:

- `Msg`: 直接将带有`role``content``name`字段的字典传递给API。
Expand Down Expand Up @@ -76,6 +78,75 @@ print(prompt)
]
```

##### 视觉(Vision)模型

对支持视觉的模型而言,如果输入消息包含图像url,生成的`content`字段将是一个字典的列表,其中包含文本和图像url。

具体来说,如果是网络图片url,将直接传递给OpenAI Chat API,而本地图片url将被转换为base64格式。更多细节请参考[官方指南](https://platform.openai.com/docs/guides/vision)

注意无效的图片url(例如`/Users/xxx/test.mp3`)将被忽略。

```python
from agentscope.models import OpenAIChatWrapper
from agentscope.message import Msg

model = OpenAIChatWrapper(
config_name="", # 为空,因为我们直接初始化model wrapper
model_name="gpt-4",
)

prompt = model.format(
Msg("system", "You're a helpful assistant", role="system"), # Msg 对象
[ # Msg 对象的列表
Msg(name="user", content="Describe this image", role="user", url="https://xxx.png"),
Msg(name="user", content="And these images", role="user", urls=["/Users/xxx/test.png", "/Users/xxx/test.mp3"]),
],
)
print(prompt)
```

```python
[
{
"role": "system",
"name": "system",
"content": "You are a helpful assistant"
},
{
"role": "user",
"name": "user",
"content": [
{
"type": "text",
"text": "Describe this image"
},
{
"type": "image_url",
"image_url": {
"url": "https://xxx.png"
}
},
]
},
{
"role": "user",
"name": "user",
"content": [
{
"type": "text",
"text": "And these images"
},
{
"type": "image_url",
"image_url": {
"url": "data:image/png;base64,YWJjZGVm..." # 对应 /Users/xxx/test.png
}
},
]
},
]
```

### `DashScopeChatWrapper`

`DashScopeChatWrapper`封装了DashScope聊天API,它接受消息列表作为输入。消息必须遵守以下规则:
Expand Down

0 comments on commit 7a0fd47

Please sign in to comment.