|
| 1 | +const { BedrockChat } = require('@langchain/community/chat_models/bedrock'); |
| 2 | +const { HumanMessage, SystemMessage, BaseMessage } = require('@langchain/core/messages'); |
| 3 | +const { BedrockEmbeddings } = require('@langchain/community/embeddings/bedrock'); |
| 4 | +const { MemoryVectorStore } = require('langchain/vectorstores/memory'); |
| 5 | + |
| 6 | +class BedrockClaudeGenerate { |
| 7 | + constructor(creds) { |
| 8 | + this.model = new BedrockChat({ |
| 9 | + model: 'anthropic.claude-3-sonnet-20240229-v1:0', |
| 10 | + region: 'us-west-2', |
| 11 | + // endpointUrl: "custom.amazonaws.com", |
| 12 | + credentials: creds, |
| 13 | + modelKwargs: { |
| 14 | + anthropic_version: 'bedrock-2023-05-31', |
| 15 | + }, |
| 16 | + }); |
| 17 | + |
| 18 | + this.embeddings = new BedrockEmbeddings({ |
| 19 | + region: 'us-west-2', |
| 20 | + credentials: creds, |
| 21 | + model: 'amazon.titan-embed-text-v2:0', // Default value |
| 22 | + }); |
| 23 | + } |
| 24 | + |
| 25 | + async filter_functions(functions, instruction) { |
| 26 | + const documents = functions.map((f) => { |
| 27 | + const { parameters, ...fDescription } = f.function; |
| 28 | + return JSON.stringify(fDescription); |
| 29 | + }); |
| 30 | + |
| 31 | + const vectorStore = await MemoryVectorStore.fromTexts(documents, [], this.embeddings); |
| 32 | + // 128 (max no of functions accepted by openAI function calling) |
| 33 | + const retrievedDocuments = await vectorStore.similaritySearch(instruction, 10); |
| 34 | + var selectedFunctions = []; |
| 35 | + retrievedDocuments.forEach((document) => { |
| 36 | + const pDocument = JSON.parse(document.pageContent); |
| 37 | + const findF = functions.find( |
| 38 | + (f) => f.function.name === pDocument.name && f.function.description === pDocument.description, |
| 39 | + ); |
| 40 | + if (findF) { |
| 41 | + selectedFunctions = selectedFunctions.concat(findF); |
| 42 | + } |
| 43 | + }); |
| 44 | + return selectedFunctions; |
| 45 | + } |
| 46 | + |
| 47 | + async process_user_instruction(functions, instruction) { |
| 48 | + //console.log(functions.map((f) => f.function.name)); |
| 49 | + // Define the function call format |
| 50 | + const fn = `{"name": "function_name"}`; |
| 51 | + |
| 52 | + // Prepare the function string for the system prompt |
| 53 | + const fnStr = functions.map((f) => JSON.stringify(f)).join('\n'); |
| 54 | + |
| 55 | + // Define the system prompt |
| 56 | + const systemPrompt = ` |
| 57 | + You are a helpful assistant with access to the following functions: |
| 58 | +
|
| 59 | + ${fnStr} |
| 60 | +
|
| 61 | + To use these functions respond with, only output function names, ignore arguments needed by those functions: |
| 62 | +
|
| 63 | + <multiplefunctions> |
| 64 | + <functioncall> ${fn} </functioncall> |
| 65 | + <functioncall> ${fn} </functioncall> |
| 66 | + ... |
| 67 | + </multiplefunctions> |
| 68 | +
|
| 69 | + Edge cases you must handle: |
| 70 | + - If there are multiple functions that can fullfill user request, list them all. |
| 71 | + - If there are no functions that match the user request, you will respond politely that you cannot help. |
| 72 | + - If the user has not provided all information to execute the function call, choose the best possible set of values. Only, respond with the information requested and nothing else. |
| 73 | + - If asked something that cannot be determined with the user's request details, respond that it is not possible to fulfill the request and explain why. |
| 74 | + `; |
| 75 | + |
| 76 | + // Prepare the messages for the language model |
| 77 | + const messages = [new SystemMessage({ content: systemPrompt }), new HumanMessage({ content: instruction })]; |
| 78 | + |
| 79 | + // Invoke the language model and get the completion |
| 80 | + const completion = await this.model.invoke(messages); |
| 81 | + const content = completion.content.trim(); |
| 82 | + |
| 83 | + // Extract function calls from the completion |
| 84 | + const extractedFunctions = this.extractFunctionCalls(content); |
| 85 | + |
| 86 | + console.log(extractedFunctions); |
| 87 | + |
| 88 | + return extractedFunctions; |
| 89 | + } |
| 90 | + |
| 91 | + extractFunctionCalls(completion) { |
| 92 | + let content = typeof completion === 'string' ? completion : completion.content; |
| 93 | + |
| 94 | + // Multiple functions lookup |
| 95 | + const mfnPattern = /<multiplefunctions>(.*?)<\/multiplefunctions>/s; |
| 96 | + const mfnMatch = content.match(mfnPattern); |
| 97 | + |
| 98 | + // Single function lookup |
| 99 | + const singlePattern = /<functioncall>(.*?)<\/functioncall>/s; |
| 100 | + const singleMatch = content.match(singlePattern); |
| 101 | + |
| 102 | + let functions = []; |
| 103 | + |
| 104 | + if (!mfnMatch && !singleMatch) { |
| 105 | + // No function calls found |
| 106 | + return null; |
| 107 | + } else if (mfnMatch) { |
| 108 | + // Multiple function calls found |
| 109 | + const multiplefn = mfnMatch[1]; |
| 110 | + const fnMatches = [...multiplefn.matchAll(/<functioncall>(.*?)<\/functioncall>/gs)]; |
| 111 | + for (let fnMatch of fnMatches) { |
| 112 | + const fnText = fnMatch[1].replace(/\\/g, ''); |
| 113 | + try { |
| 114 | + functions.push(JSON.parse(fnText)); |
| 115 | + } catch { |
| 116 | + // Ignore invalid JSON |
| 117 | + } |
| 118 | + } |
| 119 | + } else { |
| 120 | + // Single function call found |
| 121 | + const fnText = singleMatch[1].replace(/\\/g, ''); |
| 122 | + try { |
| 123 | + functions.push(JSON.parse(fnText)); |
| 124 | + } catch { |
| 125 | + // Ignore invalid JSON |
| 126 | + } |
| 127 | + } |
| 128 | + return functions; |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +module.exports = BedrockClaudeGenerate; |
0 commit comments