-
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.
Merge pull request #1 from hack4impact-uiuc/andrewlester/llama-3
Add provider for llama 3
- Loading branch information
Showing
4 changed files
with
57 additions
and
0 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
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# api/utils/llm_providers/alllama.py | ||
|
||
import tiktoken | ||
from openai import AsyncOpenAI | ||
from starlette.config import Config | ||
|
||
from api.utils.conversation_utils import update_user_usage | ||
|
||
config = Config(".env") | ||
client = AsyncOpenAI( | ||
base_url=config("ALLLAMA_API_BASE_URL"), api_key=config("ALLLAMA_API_KEY") | ||
) | ||
|
||
async def alllama_generate_response(conversation): | ||
messages = [ | ||
{"role": message.role, "content": message.content} | ||
for message in conversation.messages | ||
] | ||
|
||
# Count the input tokens | ||
encoding = tiktoken.encoding_for_model(conversation.model.name) | ||
input_tokens = sum(len(encoding.encode(message["content"])) for message in messages) | ||
|
||
stream = await client.chat.completions.create( | ||
max_tokens=1500, | ||
model=conversation.model.name, | ||
messages=messages, | ||
stream=True, | ||
) | ||
|
||
collected_chunks = [] | ||
output_tokens = 0 | ||
async for chunk in stream: | ||
content = chunk.choices[0].delta.content | ||
if content is None: | ||
content = "" | ||
collected_chunks.append(content) | ||
output_tokens += len(encoding.encode(content)) | ||
yield content | ||
|
||
# Update the user's usage | ||
await update_user_usage( | ||
conversation.user_email, conversation.model.name, input_tokens, output_tokens | ||
) |
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
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