Tools always modified the req and res from script how to stop #6102
Replies: 1 comment 3 replies
-
To prevent tools from modifying the request and response objects in your LangChain setup, you can leverage the
Here’s an example of how you can implement this: Step 1: Define CallbacksCreate a callback manager to handle the request and response: import { CallbackManager } from 'langchain/core/callbacks';
const callbackManager = new CallbackManager({
handleToolStart: (tool, input) => {
console.log(`Tool ${tool.name} started with input:`, input);
},
handleToolEnd: (tool, output) => {
console.log(`Tool ${tool.name} ended with output:`, output);
},
handleToolError: (tool, error) => {
console.error(`Tool ${tool.name} encountered an error:`, error);
},
}); Step 2: Configure Callbacks in ToolsPass the callback manager to your tools when you register them: import { DynamicStructuredTool } from '@langchain/core/tools';
import { z } from 'zod';
const exampleTool = new DynamicStructuredTool({
name: 'exampleTool',
description: 'This is an example tool',
schema: z.object({
input: z.string(),
}),
func: async ({ input }) => {
return `Processed input: ${input}`;
},
callbacks: callbackManager,
}); Step 3: Use Callbacks in ServiceEnsure your service uses these tools with the defined callbacks: import { Injectable, OnModuleInit } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { ChatOpenAI } from '@langchain/openai';
import { createOpenAIFunctionsAgent, AgentExecutor } from 'langchain/agents';
import { DynamicStructuredTool } from '@langchain/core/tools';
@Injectable()
export class LangChainService implements OnModuleInit {
private llm: ChatOpenAI;
private agent: any;
private agentExecutor: AgentExecutor;
private tools: any[] = [];
constructor(private configService: ConfigService) {}
async onModuleInit() {
const apiKey = this.configService.get<string>('SIMPLE_LLM_API_KEY');
const model = this.configService.get<string>('SIMPLE_LLM_MODEL');
this.llm = new ChatOpenAI({
model,
temperature: 0,
apiKey,
});
const prompt = await pull<ChatPromptTemplate>(
'hwchase17/openai-functions-agent',
);
this.agent = await createOpenAIFunctionsAgent({
llm: this.llm as any,
tools: this.tools,
prompt,
});
this.agentExecutor = new AgentExecutor({
agent: this.agent,
tools: this.tools,
});
}
registerTool(method: Function, metadata: ToolMetadata) {
const { name, description, schema } = metadata;
const tool = new DynamicStructuredTool({
name,
description,
schema,
func: async (input: any): Promise<any> => {
return await method(input);
},
callbacks: callbackManager,
});
this.tools.push(tool);
}
getAgentExecutor() {
return this.agentExecutor;
}
} Step 4: Invoke Agent with CallbacksWhen invoking the agent, ensure the callbacks are passed: import { Controller, Post, Body, Get } from '@nestjs/common';
import { LangChainService } from './langchain.service';
@Controller('langchain')
export class LangChainController {
constructor(private readonly langChainService: LangChainService) {}
@Get()
getHello(): string {
return "Hello World";
}
@Post('invoke')
async invokeAgent(@Body('query') input: any) {
const agentExecutor = this.langChainService.getAgentExecutor();
const result = await agentExecutor.invoke(
{ input },
{ callbacks: callbackManager }
);
return result.output;
}
} By using the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
Description
when i send the query based on the tool description is finds well that what function to run but when the request came tool modifed the req as well as when the response came tool again modified the response I dont want that these modified the req and res how to do this
System Info
ok
Beta Was this translation helpful? Give feedback.
All reactions