diff --git a/agent_server.py b/agent_server.py index 8364a5f..b26222f 100644 --- a/agent_server.py +++ b/agent_server.py @@ -30,9 +30,23 @@ def __init__(self): raise ValueError( "No OpenAI API key found. Please set the OPENAI_API_KEY environment variable." ) - + + self.provider = os.getenv("API_PROVIDER", "openai") + match self.provider: + case "openai": + self.initial_model = "gpt-4o" + self.conversational_model = "gpt-4-turbo-preview" + self.base_url = "https://api.openai.com/v1" + case "xai": + self.initial_model = "grok-2-latest" + self.conversational_model = "grok-2-latest" + self.base_url = "https://api.x.ai/v1" + # Initialize OpenAI client - self.client = OpenAI(api_key=self.api_key) + self.client = OpenAI( + api_key=self.api_key, + base_url=self.base_url, + ) # Initialize conversation histories self.conversations = {} @@ -107,7 +121,7 @@ async def get_response( # Get response from OpenAI response = await asyncio.to_thread( self.client.chat.completions.create, - model="gpt-4o", + model=self.initial_model, messages=[ { "role": "system", @@ -183,7 +197,7 @@ async def get_response( # Get final response second_response = await asyncio.to_thread( self.client.chat.completions.create, - model="gpt-4-turbo-preview", + model=self.conversational_model, messages=self.conversations[session_id], max_tokens=2000, temperature=0.7, diff --git a/readme.md b/readme.md index 3f43192..649279d 100644 --- a/readme.md +++ b/readme.md @@ -32,9 +32,10 @@ All required packages are listed in requirements.txt. Ensure you have Python 3.1 ```bash pip install -r requirements.txt ``` -3. Setup OpenAI api Key +3. Setup OpenAI api Key and API provider (`"openai"` is set by default; `"xai"` is also supported). ```bash export OPENAI_API_KEY="your_openai_api_key_here" + export API_PROVIDER="openai|xai" ``` 4. Running the agent To start the backend on a specified port (default is 5000), run: