Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
72 lines (48 loc) · 1.66 KB
/
Copy pathmain.py
File metadata and controls
72 lines (48 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import asyncio
import os
import sys
import time
from dotenv import load_dotenv
from aiodeepseek import DeepSeekClient, Conversation
load_dotenv()
def print_prompt() -> None:
print("\nYou: ", end="", flush=True)
async def stream_reply(conversation: Conversation, prompt: str) -> None:
total_start = time.perf_counter()
print("\nDeepSeek: ", end="", flush=True)
async for chunk in conversation.ask_stream(prompt):
print(chunk, end="", flush=True)
print(f"\n[done {time.perf_counter() - total_start:.2f}s]")
async def main() -> None:
token = os.getenv("DEEPSEEK_TOKEN")
email = os.getenv("DEEPSEEK_EMAIL")
password = os.getenv("DEEPSEEK_PASSWORD")
kwargs = {}
if token:
kwargs["token"] = token
else:
if not email or not password:
raise RuntimeError(
"Set DEEPSEEK_TOKEN or DEEPSEEK_EMAIL and DEEPSEEK_PASSWORD in .env"
)
kwargs["email"] = email
kwargs["password"] = password
async with DeepSeekClient(**kwargs) as client:
conversation = client.new_conversation()
while True:
print_prompt()
user_input = await asyncio.get_event_loop().run_in_executor(
None,
sys.stdin.readline,
)
user_input = user_input.strip()
if not user_input:
continue
if user_input.lower() in {"quit", "exit"}:
break
await stream_reply(conversation, user_input)
if not token:
token = client.token
print(f"Token: {token}")
if __name__ == "__main__":
asyncio.run(main())