Repository navigation
Expand file tree
/
Copy pathllmhandler.py
More file actions
68 lines (56 loc) · 2.28 KB
/
Copy pathllmhandler.py
File metadata and controls
68 lines (56 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import httpx
import json
from Configurator import get_config
class LLMHandler:
def __init__(self):
# Load configuration from keys.json
config = get_config()
# Azure OpenAI configuration from config
self.api_key = config.azure_api_key
self.deployment_name = config.llm.model
self.api_version = config.azure_api_version
self.azure_endpoint = config.azure_endpoint
self.url = f"{self.azure_endpoint}/openai/deployments/{self.deployment_name}/chat/completions?api-version={self.api_version}"
# LLM parameters from config
self.max_tokens = config.llm.max_tokens
self.temperature = config.llm.temperature
# Headers for the request
self.headers = {
"Content-Type": "application/json",
"api-key": self.api_key
}
async def process_request(self, user_input: str) -> str:
"""
Process user input through OpenAI GPT-4o and return the response.
Args:
user_input (str): The input text from the user
Returns:
str: The response from OpenAI GPT-4o
"""
try:
# Prepare the request payload
payload = {
"messages": [
{"role": "system", "content": "You are a helpful AI assistant."},
{"role": "user", "content": user_input}
],
"max_tokens": self.max_tokens,
"temperature": self.temperature
}
# Make the HTTP request
async with httpx.AsyncClient() as client:
response = await client.post(
self.url,
headers=self.headers,
json=payload,
timeout=30.0
)
if response.status_code == 200:
result = response.json()
return result["choices"][0]["message"]["content"]
else:
return f"Error: HTTP {response.status_code} - {response.text}"
except Exception as e:
return f"Error processing request: {str(e)}"
# Create a global instance
llm_handler = LLMHandler()