-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
ollama_api.py
183 lines (157 loc) · 6.61 KB
/
ollama_api.py
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#ollama_api.py
import aiohttp
import asyncio
import json
import requests
from typing import List, Union
import logging
logger = logging.getLogger(__name__)
async def create_ollama_embedding(api_base: str, model: str, prompt: Union[str, List[str]]) -> List[float]:
"""
Create embeddings using Ollama with the REST API asynchronously.
:param api_base: The base URL for the Ollama API
:param model: The name of the Ollama model to use
:param prompt: A string or list of strings to embed
:return: A list of embeddings
"""
# Normalize the API base URL
api_base = api_base.rstrip('/')
if not api_base.endswith('/api'):
api_base += '/api'
url = f"{api_base}/embeddings"
payload = {
"model": model,
"prompt": prompt if isinstance(prompt, str) else prompt[0] # API expects a single string
}
headers = {
"Content-Type": "application/json"
}
async with aiohttp.ClientSession() as session:
try:
async with session.post(url, json=payload, headers=headers) as response:
response.raise_for_status()
result = await response.json()
except aiohttp.ClientError as e:
raise RuntimeError(f"Error calling Ollama embedding API: {str(e)}") from e
if "embedding" in result:
return result["embedding"]
else:
raise ValueError("Unexpected response format: 'embedding' key not found")
async def send_ollama_request(api_url, base64_images, model, system_message, user_message, messages, seed,
temperature, max_tokens, random, top_k, top_p, repeat_penalty, stop, keep_alive,
tools=None, tool_choice=None):
"""
Sends a request to the Ollama API and returns a unified response format.
Args:
api_url (str): The Ollama API endpoint URL.
base64_images (List[str]): List of images encoded in base64.
model (str): The model to use.
system_message (str): System message for the LLM.
user_message (str): User message for the LLM.
messages (List[Dict[str, Any]]): Conversation messages.
seed (int): Random seed.
temperature (float): Temperature for randomness.
max_tokens (int): Maximum tokens to generate.
random (bool): Whether to use randomness.
top_k (int): Top K for sampling.
top_p (float): Top P for sampling.
repeat_penalty (float): Penalty for repetition.
stop (List[str] or None): Stop sequences.
keep_alive (bool): Whether to keep the session alive.
tools (Any, optional): Tools to be used.
tool_choice (Any, optional): Tool choice.
Returns:
Union[str, Dict[str, Any]]: Standardized response.
"""
try:
ollama_messages = prepare_ollama_messages(system_message, user_message, messages, base64_images)
options = {
"num_predict": max_tokens,
"top_k": top_k,
"top_p": top_p,
"repeat_penalty": repeat_penalty,
"stop": stop if stop else None
}
options = {k: v for k, v in options.items() if v is not None}
if random:
options["seed"] = seed
else:
options["temperature"] = temperature
data = {
"model": model,
"messages": ollama_messages,
"stream": False,
"options": options,
"keep_alive": -1 if keep_alive else 0,
}
# Add tools if provided
if tools:
data["tools"] = tools
if tool_choice:
data["tool_choice"] = tool_choice
ollama_headers = {"Content-Type": "application/json"}
async with aiohttp.ClientSession() as session:
async with session.post(api_url, headers=ollama_headers, json=data) as response:
response.raise_for_status()
response_json = await response.json()
if tools:
return response_json
else:
if "response" in response_json:
return {"choices": [{"message": {"content": response_json["response"].strip()}}]}
elif "message" in response_json:
return {"choices": [{"message": {"content": response_json["message"]["content"].strip()}}]}
else:
error_msg = f"Error: Unexpected response format - {json.dumps(response_json)}"
logger.error(error_msg)
return {"choices": [{"message": {"content": error_msg}}]}
except aiohttp.ClientResponseError as e:
error_msg = f"HTTP error occurred: {e.status}, message='{e.message}', url={e.request_info.real_url}"
logger.error(error_msg)
return {"choices": [{"message": {"content": error_msg}}]}
except json.JSONDecodeError as e:
error_msg = f"Error decoding JSON: {str(e)}"
logger.error(error_msg)
return {"choices": [{"message": {"content": error_msg}}]}
except Exception as e:
error_msg = f"Exception during API call: {str(e)}"
logger.error(error_msg)
return {"choices": [{"message": {"content": error_msg}}]}
def prepare_ollama_messages(system_message, user_message, messages, base64_images=None):
"""
Prepares messages for the Ollama API.
Args:
system_message (str): The system message.
user_message (str): The user message.
messages (List[Dict[str, Any]]): Previous conversation messages.
base64_images (List[str], optional): Base64-encoded images.
Returns:
List[Dict[str, Any]]: Formatted messages.
"""
ollama_messages = [
{"role": "system", "content": system_message},
]
for message in messages:
ollama_messages.append(message)
if base64_images:
ollama_messages.append({
"role": "user",
"content": user_message,
"images": base64_images
})
else:
ollama_messages.append({"role": "user", "content": user_message})
return ollama_messages
def parse_function_call(response, tools):
try:
# Look for JSON-like structure in the response
start = response.find("{")
end = response.rfind("}") + 1
if start != -1 and end != -1:
json_str = response[start:end]
parsed = json.loads(json_str)
if "function_call" in parsed:
return parsed
except json.JSONDecodeError:
pass
return None