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
|
- import * as cache from '../../src/cache';
- import { PalmChatProvider } from '../../src/providers/palm';
- import * as vertexUtil from '../../src/providers/vertexUtil';
- jest.mock('../../src/cache', () => ({
- fetchWithCache: jest.fn(),
- }));
- jest.mock('../../src/logger');
- jest.mock('../../src/providers/vertexUtil', () => ({
- maybeCoerceToGeminiFormat: jest.fn(),
- }));
- describe('PalmChatProvider', () => {
- let provider: PalmChatProvider;
- beforeEach(() => {
- provider = new PalmChatProvider('gemini-pro', {
- config: {
- temperature: 0.7,
- maxOutputTokens: 100,
- topP: 0.9,
- topK: 40,
- },
- });
- });
- afterEach(() => {
- jest.clearAllMocks();
- });
- describe('callGemini', () => {
- it('should call the Gemini API and return the response', async () => {
- const mockResponse = {
- data: {
- candidates: [{ content: { parts: [{ text: 'response text' }] } }],
- },
- };
- jest.mocked(cache.fetchWithCache).mockResolvedValue(mockResponse as any);
- jest.mocked(vertexUtil.maybeCoerceToGeminiFormat).mockReturnValue({
- contents: [{ role: 'user', parts: [{ text: 'test prompt' }] }],
- coerced: false,
- systemInstruction: undefined,
- });
- const response = await provider.callGemini('test prompt');
- expect(response).toEqual({
- output: 'response text',
- });
- expect(cache.fetchWithCache).toHaveBeenCalledWith(
- expect.stringContaining('gemini-pro:generateContent'),
- expect.objectContaining({
- method: 'POST',
- body: expect.stringContaining(
- '"contents":[{"role":"user","parts":[{"text":"test prompt"}]}]',
- ),
- }),
- expect.any(Number),
- );
- });
- it('should handle system messages correctly', async () => {
- const mockResponse = {
- data: {
- candidates: [{ content: { parts: [{ text: 'response text' }] } }],
- },
- };
- jest.mocked(cache.fetchWithCache).mockResolvedValue(mockResponse as any);
- jest.mocked(vertexUtil.maybeCoerceToGeminiFormat).mockReturnValue({
- contents: [
- { role: 'system' as any, parts: [{ text: 'system instruction' }] },
- { role: 'user', parts: [{ text: 'user message' }] },
- ],
- coerced: false,
- systemInstruction: undefined,
- } as any);
- const response = await provider.callGemini('test prompt');
- expect(response).toEqual({
- output: 'response text',
- });
- expect(cache.fetchWithCache).toHaveBeenCalledWith(
- expect.stringContaining(
- 'https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=',
- ),
- expect.objectContaining({
- method: 'POST',
- headers: expect.objectContaining({
- 'Content-Type': 'application/json',
- }),
- body: expect.stringContaining(
- '"contents":[{"role":"system","parts":[{"text":"system instruction"}]},{"role":"user","parts":[{"text":"user message"}]}]',
- ),
- }),
- expect.any(Number),
- );
- });
- it('should handle API call errors', async () => {
- jest.mocked(cache.fetchWithCache).mockRejectedValue(new Error('API error'));
- const response = await provider.callGemini('test prompt');
- expect(response).toEqual({
- error: 'API call error: Error: API error',
- });
- });
- it('should handle response schema', async () => {
- provider = new PalmChatProvider('gemini-pro', {
- config: {
- responseSchema: '{"type":"object","properties":{"name":{"type":"string"}}}',
- },
- });
- const mockResponse = {
- data: {
- candidates: [{ content: { parts: [{ text: '{"name":"John"}' }] } }],
- },
- };
- jest.mocked(cache.fetchWithCache).mockResolvedValue(mockResponse as any);
- jest.mocked(vertexUtil.maybeCoerceToGeminiFormat).mockReturnValue({
- contents: [{ role: 'user', parts: [{ text: 'test prompt' }] }],
- coerced: false,
- systemInstruction: undefined,
- });
- await provider.callGemini('test prompt');
- expect(cache.fetchWithCache).toHaveBeenCalledWith(
- expect.any(String),
- expect.objectContaining({
- body: expect.any(String),
- }),
- expect.any(Number),
- );
- });
- });
- });
|