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 XAILLM(LLMBase): |
| #11 | def __init__(self, config: Optional[BaseLlmConfig] = None): |
| #12 | super().__init__(config) |
| #13 | |
| #14 | if not self.config.model: |
| #15 | self.config.model = "grok-2-latest" |
| #16 | |
| #17 | api_key = self.config.api_key or os.getenv("XAI_API_KEY") |
| #18 | base_url = self.config.xai_base_url or os.getenv("XAI_API_BASE") or "https://api.x.ai/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=None, |
| #25 | tools: Optional[List[Dict]] = None, |
| #26 | tool_choice: str = "auto", |
| #27 | ): |
| #28 | """ |
| #29 | Generate a response based on the given messages using XAI. |
| #30 | |
| #31 | Args: |
| #32 | messages (list): List of message dicts containing 'role' and 'content'. |
| #33 | response_format (str or object, optional): Format of the response. Defaults to "text". |
| #34 | tools (list, optional): List of tools that the model can call. Defaults to None. |
| #35 | tool_choice (str, optional): Tool choice method. Defaults to "auto". |
| #36 | |
| #37 | Returns: |
| #38 | str: The generated response. |
| #39 | """ |
| #40 | params = { |
| #41 | "model": self.config.model, |
| #42 | "messages": messages, |
| #43 | "temperature": self.config.temperature, |
| #44 | "max_tokens": self.config.max_tokens, |
| #45 | "top_p": self.config.top_p, |
| #46 | } |
| #47 | |
| #48 | if response_format: |
| #49 | params["response_format"] = response_format |
| #50 | |
| #51 | response = self.client.chat.completions.create(**params) |
| #52 | return response.choices[0].message.content |
| #53 |