-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathOllamaAPI.py
157 lines (147 loc) · 4.58 KB
/
OllamaAPI.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
import urllib3
import json
def request_ollama(data, api, url='127.0.0.1', port='11434'):
"""
向Ollama发送请求
:param data: 请求数据
:param api: 请求的API
:param url: 服务器地址
:param port: 服务器端口
:return: 服务器返回的数据
"""
url = f'http://{url}:{port}/api/{api}'
# 使用POST方法发送请求
http = urllib3.PoolManager()
response = http.request('POST',
url,
headers={'Content-Type': 'application/json'},
body=data)
try:
res = json.loads(response.data.decode('utf-8'))
except Exception:
res = response.data.decode('utf-8')
return res
def get_ollama_embedding(text,
model='qwen2.5:14b',
url='127.0.0.1',
port='11434'):
"""
获取ollama的embedding
:param text: 输入文本
:param model: 模型名称
:param url: 服务器地址
:param port: 服务器端口
:return: embedding, 服务器返回的数据
"""
data = json.dumps({"model": model, "input": text})
res = request_ollama(data, 'embed', url, port)
embedding = res['embeddings']
if embedding.ndim == 2:
embedding = embedding.squeeze()
return embedding, res
def get_ollama_completion(prompt,
suffix=None,
system=None,
temperature=None,
top_p=None,
top_k=None,
seed=None,
context=None,
model='qwen2.5:14b',
url='127.0.0.1',
port=11434):
"""
获取ollama的completion
:param prompt: 输入文本
:param suffix: 后缀文本 (可选)
:param system: 系统信息 (可选)
:param temperature: 温度 (可选)
:param top_p: top_p (可选)
:param top_k: top_k (可选)
:param seed: 随机种子 (可选)
:param context: 上下文 (可选)
:param model: 模型名称
:param url: 服务器地址
:param port: 服务器端口
:return: 服务器返回的数据
"""
options = {}
if temperature is not None:
options['temperature'] = temperature
if top_p is not None:
options['top_p'] = top_p
if top_k is not None:
options['top_k'] = top_k
data = {
"model": model,
"prompt": prompt,
"stream": False,
"options": options
}
if suffix is not None:
data['suffix'] = suffix
if seed is not None:
data['options']['seed'] = seed
if system is not None:
data['system'] = system
if context is not None:
data['context'] = context
data = json.dumps(data)
res = request_ollama(data, 'generate', url, port)
return res
class OllamaChatAgent:
"""
聊天代理
"""
def __init__(self,
system='',
model='qwen2.5:14b',
url='127.0.0.1',
port='11434'):
"""
初始化
:param system: 系统信息 (可选)
:param model: 模型名称
:param url: 服务器地址
:param port: 服务器端口
"""
self.model = model
self.url = url
self.port = port
self.message = [{"role": "system", "content": system}]
def chat(self,
prompt,
temperature=None,
top_p=None,
top_k=None,
seed=None):
"""
获取ollama的chat
:param prompt: 输入文本
:param temperature: 温度 (可选)
:param top_p: top_p (可选)
:param top_k: top_k (可选)
:param seed: 随机种子 (可选)
:return: 服务器返回的数据
"""
self.message.append({"role": "user", "content": prompt})
options = {}
if temperature is not None:
options['temperature'] = temperature
if top_p is not None:
options['top_p'] = top_p
if top_k is not None:
options['top_k'] = top_k
data = {
"model": self.model,
"messages": self.message,
"stream": False,
"options": options
}
if seed is not None:
data['options']['seed'] = seed
data = json.dumps(data)
res = request_ollama(data, 'chat', self.url, self.port)
# 将聊天记录添加到message中
self.message.append(res['message'])
return res['message']['content']