Skip to content

Commit 558ce8a

Browse files
committed
Add an example of using DeepSeek-R1
1 parent eef91b0 commit 558ce8a

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

examples/deepseek-r1/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# DeepSeek-R1
2+
3+
This example demonstrates how to implement an agent that uses [DeepSeek-R1][1], the [reasoning model][2] released by DeepSeek.
4+
5+
6+
## Prerequisites
7+
8+
- Install `coagent` (see [Installation](../../README.md#installation)).
9+
10+
11+
## Quick Start
12+
13+
Run the agent:
14+
15+
```bash
16+
export DEEPSEEK_API_KEY="your-api-key"
17+
python examples/deepseek-r1/agent.py
18+
```
19+
20+
21+
[1]: https://api-docs.deepseek.com/news/news250120
22+
[2]: https://api-docs.deepseek.com/guides/reasoning_model

examples/deepseek-r1/agent.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import asyncio
2+
import os
3+
4+
from coagent.agents import ChatAgent, ChatMessage, ModelClient
5+
from coagent.core import AgentSpec, new, set_stderr_logger
6+
from coagent.runtimes import LocalRuntime
7+
8+
9+
client = ModelClient(
10+
model="openai/deepseek-reasoner",
11+
api_base="https://api.deepseek.com/v1",
12+
api_key=os.getenv("DEEPSEEK_API_KEY"),
13+
)
14+
15+
16+
deepseek_reasoner = AgentSpec("deepseek_reasoner", new(ChatAgent, client=client))
17+
18+
19+
async def main():
20+
async with LocalRuntime() as runtime:
21+
await runtime.register(deepseek_reasoner)
22+
23+
result = await deepseek_reasoner.run(
24+
ChatMessage(
25+
role="user", content="9.11 and 9.8, which is greater?"
26+
).encode(),
27+
stream=True,
28+
)
29+
30+
reasoning_started = False
31+
reasoning_stopped = False
32+
async for chunk in result:
33+
msg = ChatMessage.decode(chunk)
34+
if msg.reasoning_content:
35+
if not reasoning_started:
36+
print("<think>", flush=True)
37+
reasoning_started = True
38+
print(msg.reasoning_content, end="", flush=True)
39+
if msg.content:
40+
if not reasoning_stopped:
41+
print("</think>", flush=True)
42+
reasoning_stopped = True
43+
print(msg.content, end="", flush=True)
44+
45+
46+
if __name__ == "__main__":
47+
set_stderr_logger()
48+
asyncio.run(main())

0 commit comments

Comments
 (0)