Spaces:
Running
Running
| from openai import OpenAI | |
| import config | |
| # Get API key from environment variable | |
| if not config.ANTCHAT_API_KEY: | |
| raise ValueError("ANTCHAT_API_KEY environment variable is not set.") | |
| # Create the OpenAI client | |
| client = OpenAI( | |
| api_key=config.ANTCHAT_API_KEY, | |
| base_url=config.ANTCHAT_BASE_URL, | |
| ) | |
| def get_completion(messages, model="default-model", temperature=0.7, max_tokens=1024, stream=False): | |
| """ | |
| Get completion from the OpenAI-compatible API. | |
| """ | |
| try: | |
| completion = client.chat.completions.create( | |
| model=model, | |
| messages=messages, | |
| temperature=temperature, | |
| max_tokens=max_tokens, | |
| stream=stream, | |
| ) | |
| return completion | |
| except Exception as e: | |
| print(f"Error getting completion: {e}") | |
| return None | |
| def get_multimodal_completion(messages, model="default-vision-model", temperature=0.7, max_tokens=1024, stream=False): | |
| """ | |
| Get multimodal completion from the OpenAI-compatible API. | |
| The 'messages' should be in the format for multimodal requests, including image_url. | |
| """ | |
| try: | |
| completion = client.chat.completions.create( | |
| model=model, | |
| messages=messages, | |
| temperature=temperature, | |
| max_tokens=max_tokens, | |
| stream=stream, | |
| ) | |
| return completion | |
| except Exception as e: | |
| print(f"Error getting multimodal completion: {e}") | |
| return None | |