|
1 | | -import final_answer_agent_prompt from '../prompts/final_answer_agent'; |
| 1 | +import { randomUUID } from 'crypto'; |
| 2 | +import { AtomaSDK } from 'atoma-sdk'; |
2 | 3 | import { atomaChat } from '../config/atoma'; |
3 | 4 | import Tools from '../tools/aftermath'; |
4 | 5 | import { IntentAgentResponse } from '../@types/interface'; |
5 | | -import { randomUUID } from 'crypto'; |
6 | 6 |
|
7 | 7 | /** |
8 | 8 | * Utility class for processing agent responses and making decisions |
9 | 9 | * Handles the execution of tools and formatting of final responses |
10 | 10 | */ |
11 | 11 | class Utils { |
12 | 12 | private tools: Tools; |
| 13 | + private sdk: AtomaSDK; |
| 14 | + private prompt: string; |
13 | 15 |
|
14 | | - constructor(tools: Tools) { |
15 | | - this.tools = tools; |
| 16 | + constructor(bearerAuth: string, prompt: string) { |
| 17 | + this.tools = new Tools(bearerAuth, prompt); |
| 18 | + this.sdk = new AtomaSDK({ bearerAuth }); |
| 19 | + this.prompt = prompt; |
16 | 20 | } |
17 | 21 |
|
18 | 22 | /** |
19 | | - * Makes decisions based on intent agent response and executes appropriate actions |
20 | | - * @param intent_agent_response - Response from the intent agent |
21 | | - * @param query - Original user query |
22 | | - * @param tools - Optional tools configuration |
23 | | - * @returns Processed and formatted response |
| 23 | + * Process user query and execute appropriate tool |
| 24 | + * @param query - User query |
| 25 | + * @returns Processed response |
24 | 26 | */ |
25 | | - async makeDecision( |
26 | | - intent_agent_response: IntentAgentResponse, |
27 | | - query: string, |
28 | | - tools?: any, |
29 | | - ) { |
30 | | - if (intent_agent_response.success && intent_agent_response.selected_tool) { |
31 | | - return await this.executeTools( |
32 | | - intent_agent_response.selected_tool, |
33 | | - intent_agent_response.tool_arguments, |
34 | | - ); |
35 | | - } else { |
36 | | - return await this.finalAnswer( |
37 | | - intent_agent_response.response, |
| 27 | + async processQuery(query: string): Promise<any> { |
| 28 | + try { |
| 29 | + const selectedTool = await this.tools.selectAppropriateTool(query); |
| 30 | + if (!selectedTool) { |
| 31 | + return this.finalAnswer('No tool found for the query', query); |
| 32 | + } |
| 33 | + |
| 34 | + return this.executeTools(selectedTool.selected_tool || '', selectedTool.tool_arguments || []); |
| 35 | + } catch (error: unknown) { |
| 36 | + console.error('Error processing query:', error); |
| 37 | + return handleError(error, { |
| 38 | + reasoning: 'The system encountered an issue while processing your query', |
38 | 39 | query, |
39 | | - tools, |
40 | | - ); |
| 40 | + }); |
41 | 41 | } |
42 | 42 | } |
43 | 43 |
|
44 | 44 | /** |
45 | | - * Formats and processes the final answer using the AI model |
46 | | - * @param response - Raw response to be formatted |
47 | | - * @param query - Original user query |
48 | | - * @param tools - Optional tools used in processing |
49 | | - * @returns Formatted final response |
| 45 | + * Format final answer |
| 46 | + * @param response - Raw response |
| 47 | + * @param query - Original query |
| 48 | + * @param tools - Tools used |
| 49 | + * @returns Formatted response |
50 | 50 | * @private |
51 | 51 | */ |
52 | | - private async finalAnswer(response: any, query: string, tools?: any) { |
53 | | - const finalPrompt = final_answer_agent_prompt |
| 52 | + private async finalAnswer( |
| 53 | + response: string, |
| 54 | + query: string, |
| 55 | + tools?: string, |
| 56 | + ): Promise<any> { |
| 57 | + const finalPrompt = this.prompt |
54 | 58 | .replace('${query}', query) |
55 | 59 | .replace('${response}', response) |
56 | 60 | .replace('tools', `${tools || null}`); |
57 | 61 |
|
58 | | - const finalAns: any = await atomaChat([ |
59 | | - { |
60 | | - content: finalPrompt, |
61 | | - role: 'assistant', |
62 | | - }, |
63 | | - ]); |
| 62 | + const finalAns: any = await atomaChat( |
| 63 | + this.sdk, |
| 64 | + [ |
| 65 | + { |
| 66 | + content: finalPrompt, |
| 67 | + role: 'assistant', |
| 68 | + }, |
| 69 | + ] |
| 70 | + ); |
64 | 71 | const res = finalAns.choices[0].message.content; |
65 | 72 | console.log(finalPrompt); |
66 | 73 | return JSON.parse(res); |
|
0 commit comments