-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[apps] add history to deepseek and openai examples
- Loading branch information
1 parent
df56969
commit 597ca43
Showing
2 changed files
with
33 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,25 @@ | ||
from openai import OpenAI | ||
import hal9 as h9 | ||
|
||
client = OpenAI( | ||
api_key="hal9", | ||
base_url="https://api.hal9.com/proxy/server=https://api.deepseek.com") | ||
client = OpenAI(api_key = "placeholder", base_url = "https://api.hal9.com/proxy/server=https://api.deepseek.com") | ||
|
||
stream = client.chat.completions.create( | ||
model="deepseek-reasoner", | ||
messages=[ | ||
{"role": "system", "content": "You are a helpful assistant"}, | ||
{"role": "user", "content": input()}, | ||
], | ||
stream=True | ||
) | ||
messages = h9.load("messages", [ {"role": "system", "content": "You are a helpful assistant"} ]) | ||
messages.append({"role": "user", "content": input()}) | ||
|
||
for chunk in stream: | ||
completion = client.chat.completions.create(model = "deepseek-reasoner", messages = messages, stream = True) | ||
|
||
response = "" | ||
print("Thinking...\n") | ||
for chunk in completion: | ||
if len(chunk.choices) > 0: | ||
if chunk.choices[0].delta.reasoning_content is not None and chunk.choices[0].delta.reasoning_content: | ||
print(chunk.choices[0].delta.reasoning_content, end="") | ||
elif chunk.choices[0].delta.content is not None: | ||
print(chunk.choices[0].delta.content, end="") | ||
delta = chunk.choices[0].delta | ||
if delta.reasoning_content is not None and delta.reasoning_content: | ||
print(delta.reasoning_content, end="") | ||
elif delta.content is not None: | ||
if response == "": | ||
print("\n\n---\n\n") | ||
print(delta.content, end="") | ||
response += delta.content | ||
|
||
messages.append({"role": "assistant", "content": response}) | ||
h9.save("messages", messages, hidden = True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters