universal-llm-client 4.1.0 โ 4.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +139 -103
- package/LICENSE +21 -21
- package/README.md +591 -591
- package/dist/ai-model.js.map +1 -1
- package/dist/auditor.js.map +1 -1
- package/dist/client.js.map +1 -1
- package/dist/http.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/interfaces.d.ts +20 -0
- package/dist/interfaces.d.ts.map +1 -1
- package/dist/interfaces.js.map +1 -1
- package/dist/mcp.js.map +1 -1
- package/dist/providers/anthropic.js.map +1 -1
- package/dist/providers/google.d.ts.map +1 -1
- package/dist/providers/google.js +2 -0
- package/dist/providers/google.js.map +1 -1
- package/dist/providers/index.js.map +1 -1
- package/dist/providers/ollama.js.map +1 -1
- package/dist/providers/openai.js.map +1 -1
- package/dist/router.js.map +1 -1
- package/dist/stream-decoder.js.map +1 -1
- package/dist/structured-output.d.ts +24 -1
- package/dist/structured-output.d.ts.map +1 -1
- package/dist/structured-output.js +58 -5
- package/dist/structured-output.js.map +1 -1
- package/dist/tools.js.map +1 -1
- package/dist/zod-adapter.js.map +1 -1
- package/package.json +115 -116
- package/src/ai-model.ts +0 -350
- package/src/auditor.ts +0 -213
- package/src/client.ts +0 -402
- package/src/debug/debug-google-streaming.ts +0 -97
- package/src/debug/debug-tool-execution.ts +0 -86
- package/src/debug/test-lmstudio-tools.ts +0 -155
- package/src/demos/README.md +0 -47
- package/src/demos/basic/universal-llm-examples.ts +0 -161
- package/src/demos/mcp/astrid-memory-demo.ts +0 -295
- package/src/demos/mcp/astrid-persona-memory.ts +0 -357
- package/src/demos/mcp/mcp-mongodb-demo.ts +0 -275
- package/src/demos/mcp/simple-astrid-memory.ts +0 -148
- package/src/demos/mcp/simple-mcp-demo.ts +0 -68
- package/src/demos/mcp/working-mcp-demo.ts +0 -62
- package/src/demos/model-alias-demo.ts +0 -0
- package/src/demos/tools/RAG_MEMORY_INTEGRATION.md +0 -267
- package/src/demos/tools/astrid-memory-demo.ts +0 -270
- package/src/demos/tools/astrid-production-memory-clean.ts +0 -785
- package/src/demos/tools/astrid-production-memory.ts +0 -558
- package/src/demos/tools/basic-translation-test.ts +0 -66
- package/src/demos/tools/chromadb-similarity-tuning.ts +0 -390
- package/src/demos/tools/clean-multilingual-conversation.ts +0 -209
- package/src/demos/tools/clean-translation-test.ts +0 -119
- package/src/demos/tools/clean-universal-multilingual-test.ts +0 -131
- package/src/demos/tools/complete-rag-demo.ts +0 -369
- package/src/demos/tools/complete-tool-demo.ts +0 -132
- package/src/demos/tools/demo-tool-calling.ts +0 -124
- package/src/demos/tools/dynamic-language-switching-test.ts +0 -251
- package/src/demos/tools/hybrid-thinking-test.ts +0 -154
- package/src/demos/tools/memory-integration-test.ts +0 -420
- package/src/demos/tools/multilingual-memory-system.ts +0 -802
- package/src/demos/tools/ondemand-translation-demo.ts +0 -655
- package/src/demos/tools/production-tool-demo.ts +0 -245
- package/src/demos/tools/revolutionary-multilingual-test.ts +0 -151
- package/src/demos/tools/rigorous-language-analysis.ts +0 -218
- package/src/demos/tools/test-universal-memory-system.ts +0 -126
- package/src/demos/tools/translation-integration-guide.ts +0 -346
- package/src/demos/tools/universal-memory-system.ts +0 -560
- package/src/http.ts +0 -247
- package/src/index.ts +0 -160
- package/src/interfaces.ts +0 -657
- package/src/mcp.ts +0 -345
- package/src/providers/anthropic.ts +0 -762
- package/src/providers/google.ts +0 -620
- package/src/providers/index.ts +0 -8
- package/src/providers/ollama.ts +0 -469
- package/src/providers/openai.ts +0 -392
- package/src/router.ts +0 -780
- package/src/stream-decoder.ts +0 -361
- package/src/structured-output.ts +0 -702
- package/src/test-scripts/test-advanced-tools.ts +0 -310
- package/src/test-scripts/test-google-streaming-enhanced.ts +0 -147
- package/src/test-scripts/test-google-streaming.ts +0 -63
- package/src/test-scripts/test-google-system-prompt-comprehensive.ts +0 -189
- package/src/test-scripts/test-mcp-config.ts +0 -28
- package/src/test-scripts/test-mcp-connection.ts +0 -29
- package/src/test-scripts/test-system-message-positions.ts +0 -163
- package/src/test-scripts/test-system-prompt-improvement-demo.ts +0 -83
- package/src/test-scripts/test-tool-calling.ts +0 -231
- package/src/tests/ai-model.test.ts +0 -1614
- package/src/tests/auditor.test.ts +0 -224
- package/src/tests/http.test.ts +0 -200
- package/src/tests/interfaces.test.ts +0 -117
- package/src/tests/providers/google.test.ts +0 -660
- package/src/tests/providers/ollama.test.ts +0 -954
- package/src/tests/providers/openai.test.ts +0 -1122
- package/src/tests/router.test.ts +0 -254
- package/src/tests/stream-decoder.test.ts +0 -179
- package/src/tests/structured-output.test.ts +0 -1340
- package/src/tests/tools.test.ts +0 -175
- package/src/tools.ts +0 -246
- package/src/zod-adapter.ts +0 -72
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
import {request} from 'undici';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Debug Google streaming by examining raw responses
|
|
5
|
-
*/
|
|
6
|
-
async function debugGoogleStreaming() {
|
|
7
|
-
console.log('๐ Debugging Google Generative AI Streaming...\n');
|
|
8
|
-
|
|
9
|
-
const apiKey = 'AIzaSyBDbo7iVNEuCcRNTgDIgRrkGpFKisXXnm0';
|
|
10
|
-
const model = 'gemma-3-4b-it';
|
|
11
|
-
const endpoint = `https://generativelanguage.googleapis.com/v1beta/models/${model}:streamGenerateContent`;
|
|
12
|
-
|
|
13
|
-
// First, let's try the raw Google API call to see what the response looks like
|
|
14
|
-
const url = new URL(endpoint);
|
|
15
|
-
url.searchParams.set('key', apiKey);
|
|
16
|
-
|
|
17
|
-
const requestBody = {
|
|
18
|
-
contents: [
|
|
19
|
-
{
|
|
20
|
-
role: 'user',
|
|
21
|
-
parts: [{ text: 'Count from 1 to 3 briefly.' }]
|
|
22
|
-
}
|
|
23
|
-
],
|
|
24
|
-
generationConfig: {
|
|
25
|
-
responseMimeType: 'text/plain'
|
|
26
|
-
}
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
console.log('๐ก Making raw request to:', url.toString());
|
|
30
|
-
console.log('๐ฆ Request body:', JSON.stringify(requestBody, null, 2));
|
|
31
|
-
|
|
32
|
-
try {
|
|
33
|
-
const response = await request(url.toString(), {
|
|
34
|
-
method: 'POST',
|
|
35
|
-
headers: {
|
|
36
|
-
'Content-Type': 'application/json',
|
|
37
|
-
'Accept': 'text/event-stream',
|
|
38
|
-
},
|
|
39
|
-
body: JSON.stringify(requestBody)
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
console.log('๐ Response status:', response.statusCode);
|
|
43
|
-
console.log('๐ Response headers:', response.headers);
|
|
44
|
-
|
|
45
|
-
if (response.statusCode >= 400) {
|
|
46
|
-
const errorText = await response.body.text();
|
|
47
|
-
console.error('โ Error response:', errorText);
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
console.log('\n๐ Raw streaming response:');
|
|
52
|
-
console.log('---');
|
|
53
|
-
|
|
54
|
-
const decoder = new TextDecoder();
|
|
55
|
-
let chunkCount = 0;
|
|
56
|
-
|
|
57
|
-
for await (const chunk of response.body) {
|
|
58
|
-
chunkCount++;
|
|
59
|
-
const text = decoder.decode(chunk, { stream: true });
|
|
60
|
-
console.log(`[Raw Chunk ${chunkCount}]:`, JSON.stringify(text));
|
|
61
|
-
|
|
62
|
-
// Parse each line
|
|
63
|
-
const lines = text.split('\n').filter(line => line.trim());
|
|
64
|
-
for (const line of lines) {
|
|
65
|
-
console.log(`[Line]: "${line}"`);
|
|
66
|
-
|
|
67
|
-
if (line.startsWith('data: ')) {
|
|
68
|
-
const data = line.slice(6);
|
|
69
|
-
console.log(`[Data]: "${data}"`);
|
|
70
|
-
|
|
71
|
-
if (data !== '[DONE]') {
|
|
72
|
-
try {
|
|
73
|
-
const parsed = JSON.parse(data);
|
|
74
|
-
console.log('[Parsed JSON]:', JSON.stringify(parsed, null, 2));
|
|
75
|
-
|
|
76
|
-
// Check Google's response structure
|
|
77
|
-
if (parsed.candidates?.[0]?.content?.parts?.[0]?.text) {
|
|
78
|
-
console.log('[Content]:', parsed.candidates[0].content.parts[0].text);
|
|
79
|
-
}
|
|
80
|
-
} catch (parseError) {
|
|
81
|
-
console.log('[Parse Error]:', parseError);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
console.log('---');
|
|
89
|
-
console.log(`โ
Raw test completed with ${chunkCount} chunks`);
|
|
90
|
-
|
|
91
|
-
} catch (error) {
|
|
92
|
-
console.error('โ Raw test failed:', error);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
// Run the debug test
|
|
97
|
-
debugGoogleStreaming();
|
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Debug tool calling execution
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { AIModelFactory, ToolBuilder } from './index';
|
|
6
|
-
|
|
7
|
-
async function debugToolCalling() {
|
|
8
|
-
console.log('๐ Debug Tool Calling Execution\n');
|
|
9
|
-
|
|
10
|
-
const model = AIModelFactory.createOllamaChatModel('qwen3:8b');
|
|
11
|
-
|
|
12
|
-
// Simple calculator tool
|
|
13
|
-
const calcTool = ToolBuilder.createTool<{ expression: string }>(
|
|
14
|
-
'calculator',
|
|
15
|
-
'Calculate mathematical expressions',
|
|
16
|
-
{
|
|
17
|
-
properties: {
|
|
18
|
-
expression: { type: 'string', description: 'Math expression to evaluate' }
|
|
19
|
-
},
|
|
20
|
-
required: ['expression']
|
|
21
|
-
},
|
|
22
|
-
(args) => {
|
|
23
|
-
console.log('๐ง TOOL EXECUTING: calculator with', args);
|
|
24
|
-
try {
|
|
25
|
-
const result = Function(`"use strict"; return (${args.expression})`)();
|
|
26
|
-
return { expression: args.expression, result, answer: `${args.expression} = ${result}` };
|
|
27
|
-
} catch (error) {
|
|
28
|
-
return { expression: args.expression, error: 'Invalid expression', result: null };
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
);
|
|
32
|
-
|
|
33
|
-
model.registerTool(calcTool.name, calcTool.description, calcTool.parameters, calcTool.handler);
|
|
34
|
-
|
|
35
|
-
try {
|
|
36
|
-
await model.ensureReady();
|
|
37
|
-
|
|
38
|
-
console.log('๐ Test 1: Manual Tool Call Detection\n');
|
|
39
|
-
|
|
40
|
-
const response1 = await model.chat([
|
|
41
|
-
{ role: 'user', content: 'What is 5 + 3?' }
|
|
42
|
-
], {}, { tool_choice: 'auto' });
|
|
43
|
-
|
|
44
|
-
console.log('Response content:', response1.content);
|
|
45
|
-
console.log('Tool calls detected:', response1.tool_calls?.length || 0);
|
|
46
|
-
|
|
47
|
-
if (response1.tool_calls) {
|
|
48
|
-
console.log('Tool call details:');
|
|
49
|
-
response1.tool_calls.forEach((call, index) => {
|
|
50
|
-
console.log(` ${index + 1}. ${call.function.name}: ${call.function.arguments}`);
|
|
51
|
-
console.log(` Raw tool call:`, JSON.stringify(call, null, 2));
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
// Manual tool execution
|
|
55
|
-
console.log('\n๐ง Manual Tool Execution:');
|
|
56
|
-
for (const toolCall of response1.tool_calls) {
|
|
57
|
-
try {
|
|
58
|
-
const args = JSON.parse(toolCall.function.arguments);
|
|
59
|
-
console.log('Parsed arguments:', args);
|
|
60
|
-
const result = await calcTool.handler(args);
|
|
61
|
-
console.log('Tool result:', result);
|
|
62
|
-
} catch (error) {
|
|
63
|
-
console.log('Tool execution error:', error);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
console.log('\n๐ Test 2: Automatic Tool Execution\n');
|
|
69
|
-
|
|
70
|
-
const response2 = await model.chatWithTools([
|
|
71
|
-
{ role: 'user', content: 'Calculate 10 * 7 for me' }
|
|
72
|
-
]);
|
|
73
|
-
|
|
74
|
-
console.log('Automatic response:', response2.content);
|
|
75
|
-
|
|
76
|
-
} catch (error) {
|
|
77
|
-
console.error('โ Debug failed:', error);
|
|
78
|
-
} finally {
|
|
79
|
-
model.dispose();
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// Run the debug
|
|
84
|
-
if (require.main === module) {
|
|
85
|
-
debugToolCalling().catch(console.error);
|
|
86
|
-
}
|
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Debug test specifically for LM Studio tool calling with OpenAI API
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
async function testLMStudioTools() {
|
|
6
|
-
console.log('๐ง Testing LM Studio Tool Calling with OpenAI API\n');
|
|
7
|
-
|
|
8
|
-
// Create LM Studio model using OpenAI-compatible API
|
|
9
|
-
const model = AIModelFactory.createOpenAIChatModel(
|
|
10
|
-
'qwen/qwen3-8b', // Tool-trained model
|
|
11
|
-
'http://192.168.100.136:1234/v1' // LM Studio URL
|
|
12
|
-
);
|
|
13
|
-
|
|
14
|
-
// Create simple test tools (same as our router test)
|
|
15
|
-
const testTools = [
|
|
16
|
-
ToolBuilder.createTool<{ expression: string }>(
|
|
17
|
-
'calculate',
|
|
18
|
-
'Perform mathematical calculations',
|
|
19
|
-
{
|
|
20
|
-
properties: {
|
|
21
|
-
expression: {
|
|
22
|
-
type: 'string',
|
|
23
|
-
description: 'Mathematical expression to evaluate'
|
|
24
|
-
}
|
|
25
|
-
},
|
|
26
|
-
required: ['expression']
|
|
27
|
-
},
|
|
28
|
-
(args) => {
|
|
29
|
-
try {
|
|
30
|
-
const result = eval(args.expression); // Safe for testing
|
|
31
|
-
console.log(`๐ง Tool executed: calculate(${args.expression}) = ${result}`);
|
|
32
|
-
return `The result of ${args.expression} is ${result}`;
|
|
33
|
-
} catch (error) {
|
|
34
|
-
console.log(`โ Tool error: calculate(${args.expression}) failed`);
|
|
35
|
-
return `Error calculating ${args.expression}: ${error}`;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
),
|
|
39
|
-
|
|
40
|
-
ToolBuilder.createTool<{ location: string }>(
|
|
41
|
-
'get_weather',
|
|
42
|
-
'Get weather information',
|
|
43
|
-
{
|
|
44
|
-
properties: {
|
|
45
|
-
location: {
|
|
46
|
-
type: 'string',
|
|
47
|
-
description: 'City and state/country'
|
|
48
|
-
}
|
|
49
|
-
},
|
|
50
|
-
required: ['location']
|
|
51
|
-
},
|
|
52
|
-
(args) => {
|
|
53
|
-
console.log(`๐ง Tool executed: get_weather(${args.location})`);
|
|
54
|
-
return `The weather in ${args.location} is sunny and 72ยฐF.`;
|
|
55
|
-
}
|
|
56
|
-
)
|
|
57
|
-
];
|
|
58
|
-
|
|
59
|
-
// Register tools
|
|
60
|
-
model.registerTools(testTools);
|
|
61
|
-
|
|
62
|
-
console.log('๐ Registered tools:', testTools.map(t => t.name).join(', '));
|
|
63
|
-
console.log('');
|
|
64
|
-
|
|
65
|
-
try {
|
|
66
|
-
await model.ensureReady();
|
|
67
|
-
console.log('โ
LM Studio model ready\n');
|
|
68
|
-
|
|
69
|
-
// Test 1: Basic calculation
|
|
70
|
-
console.log('๐งฎ Test 1: Basic Calculation');
|
|
71
|
-
console.log('Question: What is 15 multiplied by 23?');
|
|
72
|
-
|
|
73
|
-
const response1 = await model.chatWithTools([
|
|
74
|
-
{ role: 'user', content: 'What is 15 multiplied by 23? Use the calculate tool.' }
|
|
75
|
-
]);
|
|
76
|
-
|
|
77
|
-
console.log('Response:', response1.message?.content || 'No content');
|
|
78
|
-
console.log('Usage:', response1.usage);
|
|
79
|
-
console.log('');
|
|
80
|
-
|
|
81
|
-
// Test 2: Weather query
|
|
82
|
-
console.log('๐ค๏ธ Test 2: Weather Query');
|
|
83
|
-
console.log('Question: Weather in San Francisco');
|
|
84
|
-
|
|
85
|
-
const response2 = await model.chatWithTools([
|
|
86
|
-
{ role: 'user', content: 'What is the weather in San Francisco, CA?' }
|
|
87
|
-
]);
|
|
88
|
-
|
|
89
|
-
console.log('Response:', response2.message?.content || 'No content');
|
|
90
|
-
console.log('');
|
|
91
|
-
|
|
92
|
-
// Test 3: Manual tool call inspection (without auto-execution)
|
|
93
|
-
console.log('๐ Test 3: Manual Tool Call Inspection');
|
|
94
|
-
console.log('Question: Calculate 100 / 4 (without auto-execution)');
|
|
95
|
-
|
|
96
|
-
const response3 = await model.chat([
|
|
97
|
-
{ role: 'user', content: 'Calculate 100 divided by 4 using the calculate tool.' }
|
|
98
|
-
], {}, {
|
|
99
|
-
tool_choice: 'auto',
|
|
100
|
-
executeTools: false // Don't auto-execute
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
console.log('Response content:', response3.message?.content || 'No content');
|
|
104
|
-
console.log('Tool calls detected:', response3.message?.tool_calls?.length || 0);
|
|
105
|
-
|
|
106
|
-
if (response3.message?.tool_calls) {
|
|
107
|
-
response3.message.tool_calls.forEach((call, i) => {
|
|
108
|
-
console.log(` Tool ${i + 1}: ${call.function.name}(${call.function.arguments})`);
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
console.log('');
|
|
112
|
-
|
|
113
|
-
// Test 4: Different tool_choice settings
|
|
114
|
-
console.log('๐ฏ Test 4: Different tool_choice Settings');
|
|
115
|
-
|
|
116
|
-
const testCases = [
|
|
117
|
-
{ choice: 'auto', desc: 'Auto (let model decide)' },
|
|
118
|
-
{ choice: 'required', desc: 'Required (force tool use)' },
|
|
119
|
-
{ choice: { type: 'function', function: { name: 'calculate' } }, desc: 'Specific tool' }
|
|
120
|
-
];
|
|
121
|
-
|
|
122
|
-
for (const testCase of testCases) {
|
|
123
|
-
console.log(`Testing tool_choice: ${testCase.desc}`);
|
|
124
|
-
try {
|
|
125
|
-
const response = await model.chat([
|
|
126
|
-
{ role: 'user', content: 'What is 50 + 50?' }
|
|
127
|
-
], {}, {
|
|
128
|
-
tool_choice: testCase.choice as any,
|
|
129
|
-
executeTools: false
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
console.log(` Content: ${response.message?.content?.substring(0, 100) || 'No content'}${(response.message?.content?.length || 0) > 100 ? '...' : ''}`);
|
|
133
|
-
console.log(` Tool calls: ${response.message?.tool_calls?.length || 0}`);
|
|
134
|
-
|
|
135
|
-
} catch (error) {
|
|
136
|
-
console.log(` Error: ${(error as Error).message}`);
|
|
137
|
-
}
|
|
138
|
-
console.log('');
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
} catch (error) {
|
|
142
|
-
console.error('โ Test failed:', (error as Error).message);
|
|
143
|
-
console.error('Stack:', (error as Error).stack);
|
|
144
|
-
} finally {
|
|
145
|
-
model.dispose();
|
|
146
|
-
console.log('๐ Test completed!\n');
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
// Run the test
|
|
151
|
-
if (require.main === module) {
|
|
152
|
-
testLMStudioTools().catch(console.error);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
export { testLMStudioTools };
|
package/src/demos/README.md
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
# Universal LLM Client Demos
|
|
2
|
-
|
|
3
|
-
This directory contains various demonstration files for the Universal LLM Client.
|
|
4
|
-
|
|
5
|
-
## Directory Structure
|
|
6
|
-
|
|
7
|
-
### `/basic`
|
|
8
|
-
|
|
9
|
-
Basic usage examples and simple demonstrations.
|
|
10
|
-
|
|
11
|
-
### `/tools`
|
|
12
|
-
|
|
13
|
-
Tool calling demonstrations and examples:
|
|
14
|
-
|
|
15
|
-
- `demo-tool-calling.ts` - Basic tool calling demo
|
|
16
|
-
- `complete-tool-demo.ts` - Comprehensive tool calling examples
|
|
17
|
-
- `production-tool-demo.ts` - Production-ready tool calling implementation
|
|
18
|
-
|
|
19
|
-
### `/mcp`
|
|
20
|
-
|
|
21
|
-
Model Context Protocol (MCP) integration demos:
|
|
22
|
-
|
|
23
|
-
- `simple-mcp-demo.ts` - Clean MCP MongoDB demo (recommended)
|
|
24
|
-
- `mcp-mongodb-demo.ts` - Detailed MCP MongoDB integration
|
|
25
|
-
- `working-mcp-demo.ts` - Working MCP implementation example
|
|
26
|
-
|
|
27
|
-
## Running Demos
|
|
28
|
-
|
|
29
|
-
From the root directory (`universal-llm-client/`):
|
|
30
|
-
|
|
31
|
-
```bash
|
|
32
|
-
# Tool calling demos
|
|
33
|
-
bun run demos/tools/production-tool-demo.ts
|
|
34
|
-
|
|
35
|
-
# MCP demos (requires MongoDB MCP server)
|
|
36
|
-
bun run demos/mcp/simple-mcp-demo.ts
|
|
37
|
-
|
|
38
|
-
# Basic usage
|
|
39
|
-
bun run demos/basic/[demo-file].ts
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
## Requirements
|
|
43
|
-
|
|
44
|
-
- Node.js/Bun runtime
|
|
45
|
-
- Configured Ollama instance (for Ollama provider)
|
|
46
|
-
- MongoDB MCP server (for MCP demos)
|
|
47
|
-
- API keys for external providers (OpenAI, Google)
|
|
@@ -1,161 +0,0 @@
|
|
|
1
|
-
import { AIModelFactory } from "./factory";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Example: Complete AI Application Setup
|
|
5
|
-
*
|
|
6
|
-
* This example shows how to set up a complete AI application
|
|
7
|
-
* with both chat and embedding capabilities using the factory.
|
|
8
|
-
*/
|
|
9
|
-
export async function createAIApplicationExample() {
|
|
10
|
-
|
|
11
|
-
console.log('\n๐๏ธ Example: Setting up a complete AI application...\n');
|
|
12
|
-
|
|
13
|
-
// Method 1: Using the factory for easy setup
|
|
14
|
-
const aiSetup = AIModelFactory.createCompleteSetup({
|
|
15
|
-
ollama: {
|
|
16
|
-
chatModel: 'gemma3:4b-it-qat',
|
|
17
|
-
embeddingModel: 'snowflake-arctic-embed2:latest',
|
|
18
|
-
url: 'http://localhost:11434'
|
|
19
|
-
},
|
|
20
|
-
openai: {
|
|
21
|
-
chatModel: 'google/gemma-3-4b',
|
|
22
|
-
embeddingModel: 'text-embedding-snowflake-arctic-embed-l-v2.0',
|
|
23
|
-
url: 'http://localhost:1234/v1'
|
|
24
|
-
// apiKey: 'your-api-key-here' // For real OpenAI API
|
|
25
|
-
},
|
|
26
|
-
google: {
|
|
27
|
-
chatModel: 'gemma-3-4b-it',
|
|
28
|
-
apiKey: 'AIzaSyBDbo7iVNEuCcRNTgDIgRrkGpFKisXXnm0'
|
|
29
|
-
}
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
// Method 2: Individual setup for specific use cases
|
|
33
|
-
const chatModel = AIModelFactory.createOllamaChatModel('gemma3:4b-it-qat');
|
|
34
|
-
const embeddingModel = AIModelFactory.createOllamaEmbeddingModel('snowflake-arctic-embed2:latest');
|
|
35
|
-
|
|
36
|
-
// Method 3: Google-specific setup
|
|
37
|
-
const googleChatModel = AIModelFactory.createGoogleChatModel(
|
|
38
|
-
'gemma-3-4b-it',
|
|
39
|
-
'AIzaSyBDbo7iVNEuCcRNTgDIgRrkGpFKisXXnm0'
|
|
40
|
-
);
|
|
41
|
-
|
|
42
|
-
// Example usage patterns:
|
|
43
|
-
|
|
44
|
-
console.log('๐ค Testing Google Generative AI...');
|
|
45
|
-
try {
|
|
46
|
-
const googleResponse = await googleChatModel.chat([
|
|
47
|
-
{ role: 'user', content: 'What is the capital of France?' }
|
|
48
|
-
]);
|
|
49
|
-
console.log('Google Response:', googleResponse.content);
|
|
50
|
-
} catch (error) {
|
|
51
|
-
console.error('Google API Error:', error);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
console.log('\n๐ Testing Google Streaming...');
|
|
55
|
-
try {
|
|
56
|
-
const streamingGenerator = googleChatModel.chatStream([
|
|
57
|
-
{ role: 'user', content: 'Count from 1 to 5, explaining each number briefly.' }
|
|
58
|
-
]);
|
|
59
|
-
|
|
60
|
-
for await (const chunk of streamingGenerator) {
|
|
61
|
-
process.stdout.write(chunk);
|
|
62
|
-
}
|
|
63
|
-
console.log('\nโ
Google streaming completed');
|
|
64
|
-
} catch (error) {
|
|
65
|
-
console.error('Google Streaming Error:', error);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
// 1. Simple chat
|
|
69
|
-
const chatResponse = await aiSetup.ollama.chat.chat([
|
|
70
|
-
{ role: 'user', content: 'What is machine learning?' }
|
|
71
|
-
]);
|
|
72
|
-
|
|
73
|
-
// 2. Document embedding for search
|
|
74
|
-
const documents = [
|
|
75
|
-
'Machine learning is a subset of AI',
|
|
76
|
-
'Neural networks are inspired by the brain',
|
|
77
|
-
'TypeScript is a typed superset of JavaScript'
|
|
78
|
-
];
|
|
79
|
-
|
|
80
|
-
const embeddings = await Promise.all(
|
|
81
|
-
documents.map(doc => aiSetup.ollama.embedding.embed(doc))
|
|
82
|
-
);
|
|
83
|
-
|
|
84
|
-
// 3. Cross-provider comparison including Google
|
|
85
|
-
const comparisons = await Promise.all([
|
|
86
|
-
aiSetup.ollama?.chat?.chat([{ role: 'user', content: 'What is the capital of France?' }]).catch(() => null),
|
|
87
|
-
aiSetup.openai?.chat?.chat([{ role: 'user', content: 'What is the capital of France?' }]).catch(() => null),
|
|
88
|
-
aiSetup.google?.chat?.chat([{ role: 'user', content: 'What is the capital of France?' }]).catch(() => null)
|
|
89
|
-
]);
|
|
90
|
-
|
|
91
|
-
return {
|
|
92
|
-
aiSetup,
|
|
93
|
-
chatModel,
|
|
94
|
-
embeddingModel,
|
|
95
|
-
googleChatModel,
|
|
96
|
-
examples: {
|
|
97
|
-
chatResponse,
|
|
98
|
-
embeddings,
|
|
99
|
-
comparisons: {
|
|
100
|
-
ollama: comparisons[0],
|
|
101
|
-
openai: comparisons[1],
|
|
102
|
-
google: comparisons[2]
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Test Google API specifically
|
|
110
|
-
*/
|
|
111
|
-
export async function testGoogleAPI() {
|
|
112
|
-
console.log('๐งช Testing Google Generative AI API...\n');
|
|
113
|
-
|
|
114
|
-
const googleModel = AIModelFactory.createGoogleChatModel(
|
|
115
|
-
'gemma-3-4b-it',
|
|
116
|
-
'AIzaSyBDbo7iVNEuCcRNTgDIgRrkGpFKisXXnm0'
|
|
117
|
-
);
|
|
118
|
-
|
|
119
|
-
try {
|
|
120
|
-
// Test basic chat
|
|
121
|
-
console.log('๐ฌ Basic chat test...');
|
|
122
|
-
const response = await googleModel.chat([
|
|
123
|
-
{ role: 'user', content: 'Hello! Can you tell me about TypeScript?' }
|
|
124
|
-
]);
|
|
125
|
-
console.log('Response:', response.content);
|
|
126
|
-
|
|
127
|
-
// Test streaming
|
|
128
|
-
console.log('\n๐ Streaming test...');
|
|
129
|
-
const streamResponse = googleModel.chatStream([
|
|
130
|
-
{ role: 'user', content: 'Tell me a short story about a robot learning to code.' }
|
|
131
|
-
]);
|
|
132
|
-
|
|
133
|
-
let fullResponse = '';
|
|
134
|
-
for await (const chunk of streamResponse) {
|
|
135
|
-
process.stdout.write(chunk);
|
|
136
|
-
fullResponse += chunk;
|
|
137
|
-
}
|
|
138
|
-
console.log('\nโ
Streaming completed');
|
|
139
|
-
|
|
140
|
-
// Test conversation with context
|
|
141
|
-
console.log('\n๐ญ Conversation with context...');
|
|
142
|
-
const conversationResponse = await googleModel.chat([
|
|
143
|
-
{ role: 'user', content: 'What is the capital of France?' },
|
|
144
|
-
{ role: 'assistant', content: 'The capital of France is Paris.' },
|
|
145
|
-
{ role: 'user', content: 'What is its population?' }
|
|
146
|
-
]);
|
|
147
|
-
console.log('Conversation Response:', conversationResponse.content);
|
|
148
|
-
|
|
149
|
-
console.log('\nโ
All Google API tests completed successfully!');
|
|
150
|
-
return { success: true, model: googleModel };
|
|
151
|
-
|
|
152
|
-
} catch (error) {
|
|
153
|
-
console.error('โ Google API test failed:', error);
|
|
154
|
-
return { success: false, error };
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
// Uncomment to run the examples
|
|
159
|
-
// testGoogleAPI().then(result => {
|
|
160
|
-
// console.log('Google API test complete:', result.success);
|
|
161
|
-
// });
|