Support of reasoning

This commit is contained in:
Aiden Dai
2025-02-28 08:08:54 +08:00
parent 4095c2e74e
commit 283115000a
2 changed files with 54 additions and 0 deletions

View File

@@ -377,6 +377,8 @@ curl $OPENAI_BASE_URL/chat/completions \
或者使用 OpenAI SDK (请先运行`pip3 install -U openai` 升级到最新版本)
- Non-Streaming
```python
from openai import OpenAI
client = OpenAI()
@@ -391,4 +393,29 @@ response = client.chat.completions.create(
reasoning_content = response.choices[0].message.reasoning_content
content = response.choices[0].message.content
```
- Streaming
```python
from openai import OpenAI
client = OpenAI()
messages = [{"role": "user", "content": "9.11 and 9.8, which is greater?"}]
response = client.chat.completions.create(
model="us.anthropic.claude-3-7-sonnet-20250219-v1:0",
messages=messages,
reasoning_effort="low",
max_completion_tokens=4096,
stream=True,
)
reasoning_content = ""
content = ""
for chunk in response:
if hasattr(chunk.choices[0].delta, 'reasoning_content') and chunk.choices[0].delta.reasoning_content:
reasoning_content += chunk.choices[0].delta.reasoning_content
elif hasattr(chunk.choices[0].delta, 'content') and chunk.choices[0].delta.content:
content += chunk.choices[0].delta.content
```