forked from timescale/pgai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ai--0.1.0--0.2.0.sql
253 lines (231 loc) · 7.35 KB
/
ai--0.1.0--0.2.0.sql
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
-------------------------------------------------------------------------------
-- ai 0.2.0
-------------------------------------------------------------------------------
-- ollama_list_models
-- https://github.com/ollama/ollama/blob/main/docs/api.md#list-local-models
--
create function @extschema@.ollama_list_models(_host text default null)
returns table
( "name" text
, model text
, size bigint
, digest text
, family text
, format text
, families jsonb
, parent_model text
, parameter_size text
, quantization_level text
, modified_at timestamptz
)
as $func$
_host_1 = _host
if _host_1 is None:
r = plpy.execute("select pg_catalog.current_setting('ai.ollama_host', true) as ollama_host")
if len(r) >= 0:
_host_1 = r[0]["ollama_host"]
if _host_1 is None:
_host_1 = "http://localhost:11434"
plpy.warning(f"defaulting Ollama host to: {_host_1}")
import json
from ollama import Client
client = Client(host=_host_1)
resp = client.list()
models = resp.get("models")
if models is None:
raise StopIteration
for m in models:
d = m.get("details")
yield ( m.get("name")
, m.get("model")
, m.get("size")
, m.get("digest")
, d.get("family") if d is not None else None
, d.get("format") if d is not None else None
, json.dumps(d.get("families")) if d is not None else None
, d.get("parent_model") if d is not None else None
, d.get("parameter_size") if d is not None else None
, d.get("quantization_level") if d is not None else None
, m.get("modified_at")
)
$func$
language plpython3u volatile parallel safe security invoker
set search_path to pg_catalog, pg_temp
;
-------------------------------------------------------------------------------
-- ollama_ps
-- https://github.com/ollama/ollama/blob/main/docs/api.md#list-running-models
create function @extschema@.ollama_ps(_host text default null)
returns table
( "name" text
, model text
, size bigint
, digest text
, parent_model text
, format text
, family text
, families jsonb
, parameter_size text
, quantization_level text
, expires_at timestamptz
, size_vram bigint
)
as $func$
_host_1 = _host
if _host_1 is None:
r = plpy.execute("select pg_catalog.current_setting('ai.ollama_host', true) as ollama_host")
if len(r) >= 0:
_host_1 = r[0]["ollama_host"]
if _host_1 is None:
_host_1 = "http://localhost:11434"
plpy.warning(f"defaulting Ollama host to: {_host_1}")
import json
from ollama import Client
client = Client(host=_host_1)
resp = client.ps()
models = resp.get("models")
if models is None:
raise StopIteration
for m in models:
d = m.get("details")
yield ( m.get("name")
, m.get("model")
, m.get("size")
, m.get("digest")
, d.get("parent_model") if d is not None else None
, d.get("format") if d is not None else None
, d.get("family") if d is not None else None
, json.dumps(d.get("families")) if d is not None else None
, d.get("parameter_size") if d is not None else None
, d.get("quantization_level") if d is not None else None
, m.get("expires_at")
, m.get("size_vram")
)
$func$
language plpython3u volatile parallel safe security invoker
set search_path to pg_catalog, pg_temp
;
-------------------------------------------------------------------------------
-- ollama_embed
-- https://github.com/ollama/ollama/blob/main/docs/api.md#generate-embeddings
create function @extschema@.ollama_embed
( _model text
, _input text
, _host text default null
, _keep_alive float8 default null
, _options jsonb default null
) returns @extschema:vector@.vector
as $func$
_host_1 = _host
if _host_1 is None:
r = plpy.execute("select pg_catalog.current_setting('ai.ollama_host', true) as ollama_host")
if len(r) >= 0:
_host_1 = r[0]["ollama_host"]
if _host_1 is None:
_host_1 = "http://localhost:11434"
plpy.warning(f"defaulting Ollama host to: {_host_1}")
_options_1 = None
if _options is not None:
import json
_options_1 = {k: v for k, v in json.loads(_options).items()}
from ollama import Client
client = Client(host=_host_1)
resp = client.embeddings(_model, _input, options=_options, keep_alive=_keep_alive)
return resp.get("embedding")
$func$
language plpython3u volatile parallel safe security invoker
set search_path to pg_catalog, pg_temp
;
-------------------------------------------------------------------------------
-- ollama_generate
-- https://github.com/ollama/ollama/blob/main/docs/api.md#generate-a-completion
create function @extschema@.ollama_generate
( _model text
, _prompt text
, _host text default null
, _images bytea[] default null
, _keep_alive float8 default null
, _options jsonb default null
, _system text default null
, _template text default null
, _context int[] default null
) returns jsonb
as $func$
_host_1 = _host
if _host_1 is None:
r = plpy.execute("select pg_catalog.current_setting('ai.ollama_host', true) as ollama_host")
if len(r) >= 0:
_host_1 = r[0]["ollama_host"]
if _host_1 is None:
_host_1 = "http://localhost:11434"
plpy.warning(f"defaulting Ollama host to: {_host_1}")
import json
args = {}
if _keep_alive is not None:
args["keep_alive"] = _keep_alive
if _options is not None:
args["options"] = {k: v for k, v in json.loads(_options).items()}
if _system is not None:
args["system"] = _system
if _template is not None:
args["template"] = _template
if _context is not None:
args["context"] = _context
_images_1 = None
if _images is not None:
import base64
_images_1 = []
for image in _images:
_images_1.append(base64.b64encode(image).decode('utf-8'))
args["images"] = _images_1
from ollama import Client
client = Client(host=_host_1)
resp = client.generate(_model, _prompt, stream=False, **args)
return json.dumps(resp)
$func$
language plpython3u volatile parallel safe security invoker
set search_path to pg_catalog, pg_temp
;
-------------------------------------------------------------------------------
-- ollama_chat_complete
-- https://github.com/ollama/ollama/blob/main/docs/api.md#generate-a-chat-completion
create function @extschema@.ollama_chat_complete
( _model text
, _messages jsonb
, _host text default null
, _keep_alive float8 default null
, _options jsonb default null
) returns jsonb
as $func$
_host_1 = _host
if _host_1 is None:
r = plpy.execute("select pg_catalog.current_setting('ai.ollama_host', true) as ollama_host")
if len(r) >= 0:
_host_1 = r[0]["ollama_host"]
if _host_1 is None:
_host_1 = "http://localhost:11434"
plpy.warning(f"defaulting Ollama host to: {_host_1}")
import json
import base64
args = {}
if _keep_alive is not None:
args["keep_alive"] = _keep_alive
if _options is not None:
args["options"] = {k: v for k, v in json.loads(_options).items()}
_messages_1 = json.loads(_messages)
if not isinstance(_messages_1, list):
plpy.error("_messages is not an array")
# the python api expects bytes objects for images
# decode the base64 encoded images into raw binary
for message in _messages_1:
if 'images' in message:
decoded = [base64.b64decode(image) for image in message["images"]]
message["images"] = decoded
from ollama import Client
client = Client(host=_host_1)
resp = client.chat(_model, _messages_1, stream=False, **args)
return json.dumps(resp)
$func$
language plpython3u volatile parallel safe security invoker
set search_path to pg_catalog, pg_temp
;