-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
62 lines (49 loc) · 1.9 KB
/
main.py
File metadata and controls
62 lines (49 loc) · 1.9 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
import requests
from pydantic_settings import BaseSettings
class Settings(BaseSettings):
llm_url: str
llm: str
settings = Settings()
print(settings.llm_url)
def get_llm_endpoint():
"""Returns the complete LLM API endpoint URL"""
base_url = settings.llm_url
return f"{base_url}/chat/completions"
def get_model_name():
"""Returns the model name to use for API requests"""
return settings.llm
def call_llm_api(user_message):
"""Calls the LLM API and returns the response with caching"""
chat_request = {
"model": get_model_name(),
"messages": [
{
"role": "system",
"content": "You are a helpful assistant. Please provide structured responses using markdown formatting. Use headers (# for main points), bullet points (- for lists), bold (**text**) for emphasis, and code blocks (```code```) for code examples. Organize your responses with clear sections and concise explanations."
},
{
"role": "user",
"content": user_message
}
]
}
headers = {"Content-Type": "application/json"}
# Send request to LLM API
response = requests.post(
get_llm_endpoint(),
headers=headers,
json=chat_request,
timeout=30
)
# Check if the status code is not 200 OK
if response.status_code != 200:
raise Exception(f"API returned status code {response.status_code}: {response.text}")
# Parse the response
chat_response = response.json()
# Extract the assistant's message
if chat_response.get('choices') and len(chat_response['choices']) > 0:
return chat_response['choices'][0]['message']['content'].strip()
raise Exception("No response choices returned from API")
if __name__ == '__main__':
response = call_llm_api("Do you know poet, Ronelda Kamfer?")
print(response)