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 json |
| #2 | import os |
| #3 | from typing import Dict, List, Optional, Union |
| #4 | |
| #5 | from openai import OpenAI |
| #6 | |
| #7 | from mem0.configs.llms.base import BaseLlmConfig |
| #8 | from mem0.configs.llms.vllm import VllmConfig |
| #9 | from mem0.llms.base import LLMBase |
| #10 | from mem0.memory.utils import extract_json |
| #11 | |
| #12 | |
| #13 | class VllmLLM(LLMBase): |
| #14 | def __init__(self, config: Optional[Union[BaseLlmConfig, VllmConfig, Dict]] = None): |
| #15 | # Convert to VllmConfig if needed |
| #16 | if config is None: |
| #17 | config = VllmConfig() |
| #18 | elif isinstance(config, dict): |
| #19 | config = VllmConfig(**config) |
| #20 | elif isinstance(config, BaseLlmConfig) and not isinstance(config, VllmConfig): |
| #21 | # Convert BaseLlmConfig to VllmConfig |
| #22 | config = VllmConfig( |
| #23 | model=config.model, |
| #24 | temperature=config.temperature, |
| #25 | api_key=config.api_key, |
| #26 | max_tokens=config.max_tokens, |
| #27 | top_p=config.top_p, |
| #28 | top_k=config.top_k, |
| #29 | enable_vision=config.enable_vision, |
| #30 | vision_details=config.vision_details, |
| #31 | http_client_proxies=config.http_client, |
| #32 | ) |
| #33 | |
| #34 | super().__init__(config) |
| #35 | |
| #36 | if not self.config.model: |
| #37 | self.config.model = "Qwen/Qwen2.5-32B-Instruct" |
| #38 | |
| #39 | self.config.api_key = self.config.api_key or os.getenv("VLLM_API_KEY") or "vllm-api-key" |
| #40 | base_url = self.config.vllm_base_url or os.getenv("VLLM_BASE_URL") |
| #41 | self.client = OpenAI(api_key=self.config.api_key, base_url=base_url) |
| #42 | |
| #43 | def _parse_response(self, response, tools): |
| #44 | """ |
| #45 | Process the response based on whether tools are used or not. |
| #46 | |
| #47 | Args: |
| #48 | response: The raw response from API. |
| #49 | tools: The list of tools provided in the request. |
| #50 | |
| #51 | Returns: |
| #52 | str or dict: The processed response. |
| #53 | """ |
| #54 | if tools: |
| #55 | processed_response = { |
| #56 | "content": response.choices[0].message.content, |
| #57 | "tool_calls": [], |
| #58 | } |
| #59 | |
| #60 | if response.choices[0].message.tool_calls: |
| #61 | for tool_call in response.choices[0].message.tool_calls: |
| #62 | processed_response["tool_calls"].append( |
| #63 | { |
| #64 | "name": tool_call.function.name, |
| #65 | "arguments": json.loads(extract_json(tool_call.function.arguments)), |
| #66 | } |
| #67 | ) |
| #68 | |
| #69 | return processed_response |
| #70 | else: |
| #71 | return response.choices[0].message.content |
| #72 | |
| #73 | def generate_response( |
| #74 | self, |
| #75 | messages: List[Dict[str, str]], |
| #76 | response_format=None, |
| #77 | tools: Optional[List[Dict]] = None, |
| #78 | tool_choice: str = "auto", |
| #79 | **kwargs, |
| #80 | ): |
| #81 | """ |
| #82 | Generate a response based on the given messages using vLLM. |
| #83 | |
| #84 | Args: |
| #85 | messages (list): List of message dicts containing 'role' and 'content'. |
| #86 | response_format (str or object, optional): Format of the response. Defaults to "text". |
| #87 | tools (list, optional): List of tools that the model can call. Defaults to None. |
| #88 | tool_choice (str, optional): Tool choice method. Defaults to "auto". |
| #89 | **kwargs: Additional vLLM-specific parameters. |
| #90 | |
| #91 | Returns: |
| #92 | str: The generated response. |
| #93 | """ |
| #94 | params = self._get_supported_params(messages=messages, **kwargs) |
| #95 | params.update( |
| #96 | { |
| #97 | "model": self.config.model, |
| #98 | "messages": messages, |
| #99 | } |
| #100 | ) |
| #101 | |
| #102 | if tools: |
| #103 | params["tools"] = tools |
| #104 | params["tool_choice"] = tool_choice |
| #105 | |
| #106 | response = self.client.chat.completions.create(**params) |
| #107 | return self._parse_response(response, tools) |
| #108 |