Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

call endpoint via a langchain chatapp #17

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
4 changes: 3 additions & 1 deletion examples/rag-question-answer/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
"@carbon/react": "^1.37.0",
"@graphql-tools/utils": "^10.1.3",
"@vitejs/plugin-react": "^4.0.4",
"chatwxflows": "file:./chatwxflows-0.1.0.tgz",
"graphql": "^16.8.1",
"graphql-request": "^6.1.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"vite": "^4.4.9",
"watzen": "^0.1.10",
"wxflows": "^0.1.0"
"wxflows": "^0.1.1",
"zod": "^3.24.1"
},
"devDependencies": {
"sass": "^1.66.1"
Expand Down
2 changes: 1 addition & 1 deletion examples/rag-question-answer/app/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function App() {
throw new Error('Something went wrong')
}
} catch (e) {
console.error('Something went wrong')
console.error('Something went wrong', e)
} finally {
setIsLoading(false)
}
Expand Down
33 changes: 14 additions & 19 deletions examples/rag-question-answer/app/src/callWxflows.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,29 @@
import wxflows from 'wxflows'
import { ChatWXFlows } from 'chatwxflows'
import { calculatorTool } from './tools';

export async function getAnswer(question) {
const model = new wxflows({
const chatModel = new ChatWXFlows({
n: 4,
endpoint: import.meta.env.VITE_WXFLOWS_ENDPOINT,
apikey: import.meta.env.VITE_WXFLOWS_APIKEY,
})

const schema = await model.generate()

// Make sure these match your values in `wxflows.toml`
const flowName = 'myRag'
const collection = import.meta.env.VITE_WXFLOWS_COLLECTION

const result = await model.ragAnswer({
schema,
flowName,
flowName: 'myRag',
variables: {
n: 5,
question,
aiEngine: 'WATSONX',
model: 'ibm/granite-13b-chat-v2',
collection,
collection: 'watsonxdocs',
parameters: {
max_new_tokens: 1000,
temperature: 0.7,
stop_sequences: ['\n\n'],
},
searchEngine: 'GETTINGSTARTED',
},
})
}
});

const llmWithTools = chatModel.bindTools([calculatorTool]);

return result?.data?.[flowName]?.out?.modelResponse?.results[0]?.generated_text;
}
const res = await llmWithTools.invoke(question);

return res.content
}
49 changes: 49 additions & 0 deletions examples/rag-question-answer/app/src/tools.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { tool } from "@langchain/core/tools";
import { z } from "zod";

export const calculatorSchema = z.object({
operation: z
.enum(["add", "subtract", "multiply", "divide"])
.describe("The type of operation to execute."),
number1: z.number().describe("The first number to operate on."),
number2: z.number().describe("The second number to operate on."),
});

export const calculatorTool = tool(async ({ operation, number1, number2 }) => {
if (operation === "add") {
return `${number1 + number2}`;
} else if (operation === "subtract") {
return `${number1 - number2}`;
} else if (operation === "multiply") {
return `${number1 * number2}`;
} else if (operation === "divide") {
return `${number1 / number2}`;
} else {
throw new Error("Invalid operation.");
}
}, {
name: "calculator",
description: "Can perform mathematical operations.",
schema: calculatorSchema,
});


export const getCalculatorToolML = async (calculatorTool: any, userMessage: string, operation: string) => {
// Extract operation and numbers using a regex
const match = userMessage.match(/What is (\d+) \* (\d+)/);
if (!match) {
throw new Error("Could not parse the question.");
}

const number1 = parseFloat(match[1]);
const number2 = parseFloat(match[2]);

// Invoke the calculator tool
const result = await calculatorTool.invoke({
operation,
number1,
number2,
});

return `The result of ${number1} * ${number2} is ${result}.`;
}