-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
998d448
commit 551de1a
Showing
5 changed files
with
54 additions
and
31 deletions.
There are no files selected for viewing
Binary file modified
BIN
-139 Bytes
(95%)
api/utils/llm_providers/__pycache__/anthropic.cpython-311.pyc
Binary file not shown.
Binary file modified
BIN
+105 Bytes
(110%)
api/utils/llm_providers/__pycache__/openai.cpython-311.pyc
Binary file not shown.
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,40 +1,40 @@ | ||
import asyncio | ||
from anthropic import Anthropic | ||
from anthropic import AsyncAnthropic | ||
from starlette.config import Config | ||
|
||
config = Config('.env') | ||
client = Anthropic(api_key=config("ANTHROPIC_API_KEY")) | ||
client = AsyncAnthropic(api_key=config("ANTHROPIC_API_KEY")) | ||
|
||
async def anthropic_generate_response(conversation): | ||
messages = [ | ||
{"role": message.role, "content": message.content} | ||
for message in conversation.messages | ||
] | ||
|
||
with client.messages.stream( | ||
stream = await client.messages.create( | ||
model=conversation.model.name, | ||
messages=messages, | ||
max_tokens=1024, | ||
) as stream: | ||
for text in stream.text_stream: | ||
yield text | ||
stream=True, | ||
) | ||
|
||
async for event in stream: | ||
if event.type == "content_block_delta": | ||
content = event.delta.text | ||
yield content | ||
|
||
async def generate_conversation_name(conversation): | ||
messages = [ | ||
{"role": message.role, "content": message.content} | ||
for message in conversation.messages | ||
if message.content.strip() # Filter out messages with empty content | ||
] | ||
messages.append({"role": "user", "content": "Please give a short, concise name for the above conversation."}) | ||
|
||
def sync_create_message(): | ||
response = client.messages.create( | ||
model="claude-3-haiku-20240307", | ||
system="You are a conversation namer. Give a short, concise name for the given conversation.", | ||
messages=messages, | ||
max_tokens=10, | ||
) | ||
return response | ||
|
||
response = await asyncio.to_thread(sync_create_message) | ||
|
||
|
||
response = await client.messages.create( | ||
model="claude-3-haiku-20240307", | ||
system="You are a conversation namer. Give a short, concise name for the given conversation.", | ||
messages=messages, | ||
max_tokens=10, | ||
) | ||
|
||
return response.content[0].text |
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,22 +1,23 @@ | ||
from openai import OpenAI | ||
from openai import AsyncOpenAI | ||
from starlette.config import Config | ||
|
||
config = Config('.env') | ||
|
||
client = OpenAI(api_key=config("OPENAI_API_KEY")) | ||
client = AsyncOpenAI(api_key=config("OPENAI_API_KEY")) | ||
|
||
async def openai_generate_response(conversation): | ||
|
||
messages = [ | ||
{"role": message.role, "content": message.content} | ||
for message in conversation.messages | ||
] | ||
|
||
response = client.chat.completions.create(model=conversation.model.name, | ||
messages=messages, | ||
stream=True) | ||
stream = await client.chat.completions.create( | ||
model=conversation.model.name, | ||
messages=messages, | ||
stream=True, | ||
) | ||
|
||
for chunk in response: | ||
# Extract the content from the chunk | ||
content = chunk.choices[0].delta.content | ||
yield content | ||
async for chunk in stream: | ||
content = chunk.choices[0].delta.content | ||
if content is None: | ||
content = "" | ||
yield content |
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