Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

providers.vertex.test.ts 3.8 KB

You have to be logged in to leave a comment. Sign In
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
147
148
149
  1. import type { JSONClient } from 'google-auth-library/build/src/auth/googleauth';
  2. import { getCache, isCacheEnabled } from '../src/cache';
  3. import { VertexChatProvider } from '../src/providers/vertex';
  4. import * as vertexUtil from '../src/providers/vertexUtil';
  5. jest.mock('../src/cache', () => ({
  6. getCache: jest.fn().mockReturnValue({
  7. get: jest.fn(),
  8. set: jest.fn(),
  9. }),
  10. isCacheEnabled: jest.fn(),
  11. }));
  12. jest.mock('../src/providers/vertexUtil', () => ({
  13. ...jest.requireActual('../src/providers/vertexUtil'),
  14. getGoogleClient: jest.fn(),
  15. }));
  16. describe('VertexChatProvider.callGeminiApi', () => {
  17. let provider: VertexChatProvider;
  18. beforeEach(() => {
  19. provider = new VertexChatProvider('gemini-pro', {
  20. config: {
  21. context: 'test-context',
  22. examples: [{ input: 'example input', output: 'example output' }],
  23. stopSequence: ['\n'],
  24. temperature: 0.7,
  25. maxOutputTokens: 100,
  26. topP: 0.9,
  27. topK: 40,
  28. },
  29. });
  30. jest.mocked(getCache).mockReturnValue({
  31. get: jest.fn(),
  32. set: jest.fn(),
  33. wrap: jest.fn(),
  34. del: jest.fn(),
  35. reset: jest.fn(),
  36. store: {} as any,
  37. });
  38. jest.mocked(isCacheEnabled).mockReturnValue(true);
  39. });
  40. afterEach(() => {
  41. jest.clearAllMocks();
  42. });
  43. it('should call the Gemini API and return the response', async () => {
  44. const mockResponse = {
  45. data: [
  46. {
  47. candidates: [{ content: { parts: [{ text: 'response text' }] } }],
  48. usageMetadata: {
  49. totalTokenCount: 10,
  50. promptTokenCount: 5,
  51. candidatesTokenCount: 5,
  52. },
  53. },
  54. ],
  55. };
  56. jest.spyOn(vertexUtil, 'getGoogleClient').mockResolvedValue({
  57. client: {
  58. request: jest.fn().mockResolvedValue(mockResponse),
  59. } as unknown as JSONClient,
  60. projectId: 'test-project-id',
  61. });
  62. const response = await provider.callGeminiApi('test prompt');
  63. expect(response).toEqual({
  64. cached: false,
  65. output: 'response text',
  66. tokenUsage: {
  67. total: 10,
  68. prompt: 5,
  69. completion: 5,
  70. },
  71. });
  72. });
  73. it('should return cached response if available', async () => {
  74. const mockCachedResponse = {
  75. cached: true,
  76. output: 'cached response text',
  77. tokenUsage: {
  78. total: 10,
  79. prompt: 5,
  80. completion: 5,
  81. },
  82. };
  83. jest.mocked(getCache().get).mockResolvedValue(JSON.stringify(mockCachedResponse));
  84. const response = await provider.callGeminiApi('test prompt');
  85. expect(response).toEqual({
  86. ...mockCachedResponse,
  87. tokenUsage: {
  88. ...mockCachedResponse.tokenUsage,
  89. cached: mockCachedResponse.tokenUsage.total,
  90. },
  91. });
  92. });
  93. it('should handle API call errors', async () => {
  94. const mockError = new Error('something went wrong');
  95. jest.spyOn(vertexUtil, 'getGoogleClient').mockResolvedValue({
  96. client: {
  97. request: jest.fn().mockRejectedValue(mockError),
  98. } as unknown as JSONClient,
  99. projectId: 'test-project-id',
  100. });
  101. const response = await provider.callGeminiApi('test prompt');
  102. expect(response).toEqual({
  103. error: `API call error: Error: something went wrong`,
  104. });
  105. });
  106. it('should handle API response errors', async () => {
  107. const mockResponse = {
  108. data: [
  109. {
  110. error: {
  111. code: 400,
  112. message: 'Bad Request',
  113. },
  114. },
  115. ],
  116. };
  117. jest.spyOn(vertexUtil, 'getGoogleClient').mockResolvedValue({
  118. client: {
  119. request: jest.fn().mockResolvedValue(mockResponse),
  120. } as unknown as JSONClient,
  121. projectId: 'test-project-id',
  122. });
  123. const response = await provider.callGeminiApi('test prompt');
  124. expect(response).toEqual({
  125. error: 'Error 400: Bad Request',
  126. });
  127. });
  128. });
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...