repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | import os |
| #2 | from typing import Dict, List, Optional |
| #3 | |
| #4 | from openai import OpenAI |
| #5 | |
| #6 | from mem0.configs.llms.base import BaseLlmConfig |
| #7 | from mem0.llms.base import LLMBase |
| #8 | |
| #9 | |
| #10 | class OpenAIStructuredLLM(LLMBase): |
| #11 | def __init__(self, config: Optional[BaseLlmConfig] = None): |
| #12 | super().__init__(config) |
| #13 | |
| #14 | if not self.config.model: |
| #15 | self.config.model = "gpt-4o-2024-08-06" |
| #16 | |
| #17 | api_key = self.config.api_key or os.getenv("OPENAI_API_KEY") |
| #18 | base_url = self.config.openai_base_url or os.getenv("OPENAI_API_BASE") or "https://api.openai.com/v1" |
| #19 | self.client = OpenAI(api_key=api_key, base_url=base_url) |
| #20 | |
| #21 | def generate_response( |
| #22 | self, |
| #23 | messages: List[Dict[str, str]], |
| #24 | response_format: Optional[str] = None, |
| #25 | tools: Optional[List[Dict]] = None, |
| #26 | tool_choice: str = "auto", |
| #27 | ) -> str: |
| #28 | """ |
| #29 | Generate a response based on the given messages using OpenAI. |
| #30 | |
| #31 | Args: |
| #32 | messages (List[Dict[str, str]]): A list of dictionaries, each containing a 'role' and 'content' key. |
| #33 | response_format (Optional[str]): The desired format of the response. Defaults to None. |
| #34 | |
| #35 | |
| #36 | Returns: |
| #37 | str: The generated response. |
| #38 | """ |
| #39 | params = { |
| #40 | "model": self.config.model, |
| #41 | "messages": messages, |
| #42 | "temperature": self.config.temperature, |
| #43 | } |
| #44 | |
| #45 | if response_format: |
| #46 | params["response_format"] = response_format |
| #47 | if tools: |
| #48 | params["tools"] = tools |
| #49 | params["tool_choice"] = tool_choice |
| #50 | |
| #51 | response = self.client.beta.chat.completions.parse(**params) |
| #52 | return response.choices[0].message.content |
| #53 |