-
Notifications
You must be signed in to change notification settings - Fork 11
✨feat(LLM): Added "llm" func to Sdk for interacting with LLM agent #96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
b14179b
1578f92
1d7511a
befe592
9ae2877
31f50b7
40e0003
788c4a4
ef33098
7132d7a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| from typing import Dict, List, Literal, Optional | ||
|
|
||
| from openai import OpenAI | ||
|
|
||
| from harambe.settings import get_settings | ||
|
|
||
| LLM_AGENTS = Literal["openai"] | ||
|
|
||
|
|
||
| class LLMManager: | ||
| def __init__(self, agent: Optional[LLM_AGENTS] = None, model: Optional[str] = None): | ||
| self.agent_name = "openai" if agent is None else agent | ||
| self.model = "gpt-4o-mini" if model is None else model | ||
|
|
||
| if self.agent_name == "openai": | ||
| self.agent = OpenAI(api_key=get_settings().openai_api_key) | ||
|
|
||
| def query(self, prompts: List[Dict[str, str]]) -> str: | ||
| """ | ||
| Query the LLM agent with the given prompts. | ||
| Parameters: | ||
| prompts: List[{ type, content}] | ||
| type: one of ["text", "image"] | ||
| content: "string" | ||
|
|
||
| Returns: | ||
| response: The response from the LLM agent. | ||
| """ | ||
|
|
||
| if self.agent_name == "openai": | ||
| content = [] | ||
| for prompt in prompts: | ||
| if prompt["type"] == "text": | ||
| content.append({"type": "text", "text": prompt["content"]}) | ||
|
|
||
| if prompt["type"] == "image": | ||
| content.append( | ||
| { | ||
| "type": "image_url", | ||
| "image_url": {"url": f"{prompt['content']}"}, | ||
| } | ||
| ) | ||
|
|
||
| response = self.agent.chat.completions.create( | ||
| model=self.model, | ||
| messages=[{"role": "user", "content": content}], | ||
| ) | ||
|
|
||
| return response.choices[0].message.content |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| from functools import lru_cache | ||
|
|
||
| # from pydantic import SecretStr | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: remove
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea, I was planning to remove this. |
||
| from pydantic_settings import BaseSettings, SettingsConfigDict | ||
|
|
||
|
|
||
| class Settings(BaseSettings): | ||
| model_config = SettingsConfigDict(env_file=(".env")) | ||
| openai_api_key: str | ||
|
|
||
|
|
||
| @lru_cache() | ||
| def get_settings(): | ||
| return Settings() | ||
|
Comment on lines
+12
to
+14
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this from their examples?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope, I got this from a blog. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,8 @@ dependencies = [ | |
| "curl-cffi==0.7.3", | ||
| "ua-generator==1.0.5", | ||
| "python-slugify>=8.0.4", | ||
| "pydantic-settings>=2.6.1", | ||
| "openai>=1.56.2", | ||
| ] | ||
|
|
||
| [tool.uv] | ||
|
|
@@ -32,6 +34,7 @@ dev-dependencies = [ | |
| "pytest==7.4.4", | ||
| "pytest-cov==4.1.0", | ||
| "pytest-asyncio==0.21.2", | ||
| "rich>=13.9.4", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what's this for?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I used it on local for pretty printing on the console, I can remove it, its just a dev dep |
||
| ] | ||
|
|
||
| [tool.uv.sources] | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.