| import time |
|
|
| import httpx |
| from openai import OpenAI |
| from typing import Optional |
|
|
|
|
| |
| |
| |
| class ClientConfig: |
| def __init__(self, api_key: str, base_url: str, model_id: str): |
| self.api_key = api_key |
| self.base_url = base_url |
| self.model_id = model_id |
|
|
|
|
| class OpenAIClient: |
| """ |
| A client wrapper for interacting with the OpenAI ChatCompletion API. |
| It handles client initialization and API calls with retry logic. |
| """ |
|
|
| def __init__(self, config: ClientConfig, max_retries: int = 3): |
| |
| |
| |
| custom_http_client = httpx.Client(trust_env=False) |
| self.client = OpenAI( |
| api_key=config['api_key'], |
| base_url=config['api_base'], |
| http_client=custom_http_client, |
| ) |
| self.model_id = config['model_id'] |
| self.max_retries = max_retries |
|
|
| def get_completion( |
| self, |
| user_prompt: str, |
| system_prompt: Optional[str] = None, |
| max_tokens: int = 1024 |
| ) -> Optional[str]: |
| """ |
| Calls the OpenAI ChatCompletion API and returns the result. |
| Includes retry logic for handling transient errors. |
| |
| Args: |
| user_prompt (str): The main input/prompt from the user. |
| system_prompt (Optional[str]): The system-level instruction for the model. Defaults to None. |
| max_tokens (int): The maximum number of tokens to generate. Defaults to 1024. |
| |
| Returns: |
| Optional[str]: The content of the model's response, or None if the API call fails after all retries. |
| """ |
| |
| messages = [] |
| if system_prompt: |
| messages.append({'role': 'system', 'content': system_prompt}) |
| messages.append({'role': 'user', 'content': user_prompt}) |
|
|
| |
| for attempt in range(self.max_retries): |
| try: |
| |
| response = self.client.chat.completions.create( |
| model=self.model_id, |
| messages=messages, |
| max_tokens=max_tokens, |
| |
| ) |
| |
| return response.choices[0].message.content |
|
|
| except Exception as e: |
| |
| print(f"API call failed on attempt {attempt + 1}/{self.max_retries}. Error: {e}") |
|
|
| |
| if attempt + 1 == self.max_retries: |
| print("All retry attempts failed.") |
| break |
|
|
| |
| print("Retrying in 2 seconds...") |
| time.sleep(2) |
|
|
| |
| return None |
|
|
|
|
| |
| if __name__ == '__main__': |
| |
| |
|
|
| CLIENT_CONFIG = { |
| "client_type": "openai", |
| "api_key": "none", |
| "api_base": "http://127.0.0.1:23333/v1", |
| "timeout": 60, |
| "model_id": "Qwen/Qwen2.5-14B-Instruct-AWQ", |
| "init_port": 23333, |
| "num_server": 8 |
| } |
| |
| my_client = OpenAIClient(config=CLIENT_CONFIG) |
|
|
| |
| user_message = "What is the capital of France?" |
| system_message = "You are a helpful assistant that provides concise answers." |
|
|
| |
| response_content = my_client.get_completion( |
| user_prompt=user_message, |
| system_prompt=system_message |
| ) |
|
|
| |
| if response_content: |
| print("\nModel Response:") |
| print(response_content) |
| else: |
| print("\nFailed to get a response from the model.") |