|
1 | | -import {OllamaModule} from "./../../src/modules/ollama.js"; |
| 1 | +import {OllamaModule, ChatRoles} from "./../../src/modules/ollama.js"; |
2 | 2 |
|
3 | 3 | const tools = [ |
4 | 4 | { |
5 | 5 | "type": "function", |
6 | 6 | "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" |
18 | 15 | } |
19 | | - } |
20 | | - ], |
| 16 | + }, |
| 17 | + "required": ["personAge"] |
| 18 | + } |
21 | 19 | } |
22 | 20 | } |
23 | | -] |
| 21 | +] |
24 | 22 |
|
25 | 23 | export default class OllamaToolsView extends HTMLElement { |
26 | 24 | static tag = "ollama-tools-view"; |
@@ -51,8 +49,50 @@ export default class OllamaToolsView extends HTMLElement { |
51 | 49 | } |
52 | 50 | } |
53 | 51 |
|
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; |
56 | 96 | } |
57 | 97 | } |
58 | 98 |
|
|
0 commit comments