Skip to content

Commit c23bb64

Browse files
committed
feat: update OllamaTools to include age function and integrate chat role handling
1 parent a0e7b8c commit c23bb64

File tree

2 files changed

+59
-18
lines changed

2 files changed

+59
-18
lines changed

app/ollama-tools/ollama-tools.js

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
1-
import {OllamaModule} from "./../../src/modules/ollama.js";
1+
import {OllamaModule, ChatRoles} from "./../../src/modules/ollama.js";
22

33
const tools = [
44
{
55
"type": "function",
66
"function": {
7-
"name": "example_function",
8-
"description": "This is an example function",
9-
"parameters": [
10-
{
11-
"type": "object",
12-
"properties": {
13-
"option": {
14-
"type": "string",
15-
"description": "The value to return",
16-
"enum": ["option1", "option2"]
17-
}
7+
"name": "age",
8+
"description": "Return the age of a person",
9+
"parameters": {
10+
"type": "object",
11+
"properties": {
12+
"personAge": {
13+
"type": "integer",
14+
"description": "The age of the person"
1815
}
19-
}
20-
],
16+
},
17+
"required": ["personAge"]
18+
}
2119
}
2220
}
23-
]
21+
]
2422

2523
export default class OllamaToolsView extends HTMLElement {
2624
static tag = "ollama-tools-view";
@@ -51,8 +49,50 @@ export default class OllamaToolsView extends HTMLElement {
5149
}
5250
}
5351

54-
callFunction(event) {
55-
alert("click")
52+
async callFunction(event) {
53+
const result = [];
54+
55+
const message = await OllamaModule.create_message({
56+
text: "Do you know how old the person is?"
57+
});
58+
59+
const callResult = await OllamaModule.chat({
60+
"model": "llama3.2",
61+
"tools": tools,
62+
"messages": [ message ],
63+
"stream": false
64+
})
65+
66+
for await (const data of callResult) {
67+
result.push(data);
68+
}
69+
70+
const toolsJson = JSON.parse(result[0]);
71+
const functionDefinition = toolsJson.message.tool_calls[0];
72+
const functionName = functionDefinition.function.name;
73+
const functionArgs = functionDefinition.function.arguments;
74+
75+
console.log(functionName, functionArgs);
76+
77+
const functionCallMessage = await OllamaModule.create_message({
78+
role: ChatRoles.TOOL,
79+
text: `22`
80+
});
81+
82+
const finalResult = await OllamaModule.chat({
83+
"model": "llama3.2",
84+
"tools": tools,
85+
"messages": [ message, functionCallMessage ],
86+
"stream": false
87+
});
88+
89+
result.length = 0;
90+
91+
for await (const data of finalResult) {
92+
result.push(data);
93+
}
94+
95+
this.shadowRoot.querySelector(".container").textContent = JSON.parse(result[0]).message.content;
5696
}
5797
}
5898

src/modules/ollama.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export const ChatRoles = Object.freeze({
4141
USER: "user",
4242
ASSISTANT: "assistant",
4343
SYSTEM: "system",
44+
TOOL: "tool"
4445
});
4546

4647
export class OllamaModule {
@@ -210,7 +211,7 @@ function createUrl(args, path) {
210211
* @returns {Object} - The decorated body
211212
*/
212213
function decorateBody(body, properties, args) {
213-
for (const key in properties) {
214+
for (const key of properties) {
214215
if (key in args) {
215216
body[key] = args[key];
216217
}

0 commit comments

Comments
 (0)