|
| 1 | +import * as Models from '../../../src/types'; |
| 2 | +import {ConversationalRag} from "../../../src/resources/rag/conversationalRag"; |
| 3 | +import { APIClient } from '../../../src/APIClient'; |
| 4 | +import { AI21Error } from '../../../src/errors'; |
| 5 | + |
| 6 | + |
| 7 | +class MockAPIClient extends APIClient { |
| 8 | + public post = jest.fn(); |
| 9 | + } |
| 10 | + |
| 11 | + |
| 12 | +describe('Completions', () => { |
| 13 | + let convRag: ConversationalRag; |
| 14 | + let mockClient: MockAPIClient; |
| 15 | + const dummyAPIKey = "test-api-key"; |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + mockClient = new MockAPIClient({ |
| 19 | + baseURL: 'https://api.example.com', |
| 20 | + maxRetries: 3, |
| 21 | + timeout: 5000, |
| 22 | + }); |
| 23 | + |
| 24 | + convRag = new ConversationalRag(mockClient); |
| 25 | + }); |
| 26 | + |
| 27 | + afterEach(() => { |
| 28 | + jest.clearAllMocks(); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should create a chat completion with answer_in_context false when relevant context is not found', async () => { |
| 32 | + const body = {messages: [{ role: 'user', content: 'What is the meaning of life?' }]} |
| 33 | + |
| 34 | + const options: Models.RequestOptions = { headers: { 'Authorization': `Bearer ${dummyAPIKey}` } }; |
| 35 | + const expectedResponse: Models.ConversationalRagResponse = { |
| 36 | + id: '1e242c08-96e6-4592-b5ba-53065e30e865', |
| 37 | + choices: [ |
| 38 | + { |
| 39 | + role: 'assistant', |
| 40 | + content: 'I couldn’t find any relevant information for: “What is the meaning of life?“. Please try to rephrase your question or ask about a different topic.' |
| 41 | + } |
| 42 | + ], |
| 43 | + search_queries: [ 'What is the meaning of life?' ], |
| 44 | + context_retrieved: true, |
| 45 | + answer_in_context: false, |
| 46 | + sources: [] |
| 47 | + } |
| 48 | + |
| 49 | + mockClient.post.mockResolvedValue(expectedResponse); |
| 50 | + |
| 51 | + const response = await convRag.create(body, options); |
| 52 | + |
| 53 | + expect(mockClient.post).toHaveBeenCalledWith( |
| 54 | + '/conversational-rag', |
| 55 | + { body, ...options } |
| 56 | + ); |
| 57 | + expect(response).toEqual(expectedResponse); |
| 58 | + }); |
| 59 | + |
| 60 | + |
| 61 | + it('should create a chat completion with answer_in_context true and sources when relevant context is found', async () => { |
| 62 | + const body = {messages: [{ role: 'user', content: 'What is the OOO policy in our office?' }]} |
| 63 | + |
| 64 | + const options: Models.RequestOptions = { headers: { 'Authorization': `Bearer ${dummyAPIKey}` } }; |
| 65 | + const expectedResponse: Models.ConversationalRagResponse = { |
| 66 | + id: '1e242c08-96e6-4592-b5ba-53065e30e865', |
| 67 | + choices: [ |
| 68 | + { |
| 69 | + role: 'assistant', |
| 70 | + content: 'Staff must notify the office of planned or unexpected absences, set automated OOO messages, and designate a backup to handle urgent matters, ensuring uninterrupted client service. All case files and tasks must be updated and accessible to the backup as needed.' |
| 71 | + } |
| 72 | + ], |
| 73 | + search_queries: [ 'What is the OOO policy in our office?' ], |
| 74 | + context_retrieved: true, |
| 75 | + answer_in_context: true, |
| 76 | + sources: [ |
| 77 | + { |
| 78 | + text: 'Employees must inform the office of any absence, activate OOO messages, and assign a backup to address urgent needs. Ensure all files and tasks are updated for seamless client service.', |
| 79 | + file_id: '4d20257a-9aee-47f9-9f57-bbf812955ee0', |
| 80 | + file_name: 'policies/OOO_policy.pdf', |
| 81 | + score: 0.85128105, |
| 82 | + order: 92, |
| 83 | + public_url: null, |
| 84 | + labels: [] |
| 85 | + } |
| 86 | + ] |
| 87 | + } |
| 88 | + |
| 89 | + mockClient.post.mockResolvedValue(expectedResponse); |
| 90 | + |
| 91 | + const response = await convRag.create(body, options); |
| 92 | + |
| 93 | + expect(mockClient.post).toHaveBeenCalledWith( |
| 94 | + '/conversational-rag', |
| 95 | + { body, ...options } |
| 96 | + ); |
| 97 | + expect(response).toEqual(expectedResponse); |
| 98 | + }); |
| 99 | +}); |
0 commit comments