|
| 1 | +import {BaseLlm, BaseLlmConnection, LlmAgent, LLMRegistry, LlmRequest, LlmResponse} from '@google/adk'; |
| 2 | +import {Blob, Content, createModelContent, GenerateContentResponse} from '@google/genai'; |
| 3 | + |
| 4 | +class TestLlmConnection implements BaseLlmConnection { |
| 5 | + async sendHistory(history: Content[]): Promise<void> { |
| 6 | + return Promise.resolve(); |
| 7 | + } |
| 8 | + |
| 9 | + async sendContent(content: Content): Promise<void> {} |
| 10 | + |
| 11 | + async sendRealtime(blob: Blob): Promise<void> {} |
| 12 | + |
| 13 | + async * receive(): AsyncGenerator<LlmResponse, void, void> {} |
| 14 | + |
| 15 | + async close(): Promise<void> {} |
| 16 | +} |
| 17 | + |
| 18 | +class TestLlmModel extends BaseLlm { |
| 19 | + constructor({model}: {model: string}) { |
| 20 | + super({model}); |
| 21 | + } |
| 22 | + |
| 23 | + static override readonly supportedModels = ['test-llm-model']; |
| 24 | + |
| 25 | + async * |
| 26 | + generateContentAsync(llmRequest: LlmRequest, stream?: boolean): |
| 27 | + AsyncGenerator<LlmResponse, void> { |
| 28 | + const generateContentResponse = new GenerateContentResponse(); |
| 29 | + |
| 30 | + generateContentResponse.candidates = |
| 31 | + [{content: createModelContent('test-llm-model-response')}]; |
| 32 | + const candidate = generateContentResponse.candidates[0]; |
| 33 | + |
| 34 | + yield { |
| 35 | + content: candidate.content, |
| 36 | + groundingMetadata: candidate.groundingMetadata, |
| 37 | + usageMetadata: generateContentResponse.usageMetadata, |
| 38 | + finishReason: candidate.finishReason, |
| 39 | + }; |
| 40 | + } |
| 41 | + |
| 42 | + async connect(llmRequest: LlmRequest): Promise<BaseLlmConnection> { |
| 43 | + return new TestLlmConnection(); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +describe('LLMRegistry', () => { |
| 48 | + beforeAll(() => { |
| 49 | + LLMRegistry.register(TestLlmModel); |
| 50 | + }); |
| 51 | + |
| 52 | + it('resolves model to LLM class', () => { |
| 53 | + expect(LLMRegistry.newLlm('test-llm-model')).toBeInstanceOf(TestLlmModel); |
| 54 | + }); |
| 55 | + |
| 56 | + it('resolves the provided as a string model correctly in LlmAgent', () => { |
| 57 | + const agent = new LlmAgent({name: 'test_agent', model: 'test-llm-model'}); |
| 58 | + |
| 59 | + expect(agent.canonicalModel).toBeInstanceOf(TestLlmModel); |
| 60 | + }); |
| 61 | + |
| 62 | + it('resolves the provided as class model correctly in LlmAgent', () => { |
| 63 | + const agent = new LlmAgent({ |
| 64 | + name: 'test_agent', |
| 65 | + model: new TestLlmModel({model: 'test-llm-model'}) |
| 66 | + }); |
| 67 | + |
| 68 | + expect(agent.canonicalModel).toBeInstanceOf(TestLlmModel); |
| 69 | + }) |
| 70 | +}); |
0 commit comments