-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
ollama-language-model.ts
167 lines (152 loc) · 6.2 KB
/
ollama-language-model.ts
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
// *****************************************************************************
// Copyright (C) 2024 TypeFox GmbH.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************
import {
LanguageModel,
LanguageModelParsedResponse,
LanguageModelRequest,
LanguageModelRequestMessage,
LanguageModelResponse,
LanguageModelStreamResponsePart,
ToolRequest
} from '@theia/ai-core';
import { CancellationToken } from '@theia/core';
import { ChatRequest, ChatResponse, Message, Ollama, Options, Tool } from 'ollama';
export const OllamaModelIdentifier = Symbol('OllamaModelIdentifier');
export class OllamaModel implements LanguageModel {
protected readonly DEFAULT_REQUEST_SETTINGS: Partial<Omit<ChatRequest, 'stream' | 'model'>> = {
keep_alive: '15m'
};
readonly providerId = 'ollama';
readonly vendor: string = 'Ollama';
/**
* @param id the unique id for this language model. It will be used to identify the model in the UI.
* @param model the unique model name as used in the Ollama environment.
* @param hostProvider a function to provide the host URL for the Ollama server.
* @param defaultRequestSettings optional default settings for requests made using this model.
*/
constructor(
public readonly id: string,
protected readonly model: string,
protected host: () => string | undefined,
public defaultRequestSettings?: { [key: string]: unknown }
) { }
protected getSettings(request: LanguageModelRequest): Partial<ChatRequest> {
const settings = request.settings ?? this.defaultRequestSettings ?? {};
return {
options: settings as Partial<Options>
};
}
async request(request: LanguageModelRequest, cancellationToken?: CancellationToken): Promise<LanguageModelResponse> {
const settings = this.getSettings(request);
const ollama = this.initializeOllama();
if (request.response_format?.type === 'json_schema') {
return this.handleStructuredOutputRequest(ollama, request);
}
const response = await ollama.chat({
model: this.model,
...this.DEFAULT_REQUEST_SETTINGS,
...settings,
messages: request.messages.map(this.toOllamaMessage),
stream: true,
tools: request.tools?.map(this.toOllamaTool),
});
cancellationToken?.onCancellationRequested(() => {
response.abort();
});
async function* wrapAsyncIterator<T>(inputIterable: AsyncIterable<ChatResponse>): AsyncIterable<LanguageModelStreamResponsePart> {
for await (const item of inputIterable) {
// TODO handle tool calls
yield { content: item.message.content };
}
}
return { stream: wrapAsyncIterator(response) };
}
protected async handleStructuredOutputRequest(ollama: Ollama, request: LanguageModelRequest): Promise<LanguageModelParsedResponse> {
const settings = this.getSettings(request);
const result = await ollama.chat({
...settings,
...this.DEFAULT_REQUEST_SETTINGS,
model: this.model,
messages: request.messages.map(this.toOllamaMessage),
format: 'json',
stream: false,
});
try {
return {
content: result.message.content,
parsed: JSON.parse(result.message.content)
};
} catch (error) {
// TODO use ILogger
console.log('Failed to parse structured response from the language model.', error);
return {
content: result.message.content,
parsed: {}
};
}
}
protected initializeOllama(): Ollama {
const host = this.host();
if (!host) {
throw new Error('Please provide OLLAMA_HOST in preferences or via environment variable');
}
return new Ollama({ host: host });
}
protected toOllamaTool(tool: ToolRequest): Tool {
const transform = (props: Record<string, {
[key: string]: unknown;
type: string;
}> | undefined) => {
if (!props) {
return undefined;
}
const result: Record<string, { type: string, description: string }> = {};
for (const key in props) {
if (Object.prototype.hasOwnProperty.call(props, key)) {
result[key] = {
type: props[key].type,
description: key
};
}
}
return result;
};
return {
type: 'function',
function: {
name: tool.name,
description: tool.description ?? 'Tool named ' + tool.name,
parameters: {
type: tool.parameters?.type ?? 'object',
required: Object.keys(tool.parameters?.properties ?? {}),
properties: transform(tool.parameters?.properties) ?? {}
},
}
};
}
protected toOllamaMessage(message: LanguageModelRequestMessage): Message {
if (message.actor === 'ai') {
return { role: 'assistant', content: message.query || '' };
}
if (message.actor === 'user') {
return { role: 'user', content: message.query || '' };
}
if (message.actor === 'system') {
return { role: 'system', content: message.query || '' };
}
return { role: 'system', content: '' };
}
}