Open bhaveshpatel640 opened 2 weeks ago
Hey @bhaveshpatel640! I'm here to help you with any bugs, questions, or contributions you have in mind. Let's dive in and tackle this together!
I found a similar issue where the JSON schema was not passed on to the OpenAI request when using the ReAct agent in LangChain.js. The root cause is that the OpenAIInput
and related interfaces do not include a field for the JSON schema. The OpenAIInput
interface and its related types define various parameters for making requests to OpenAI, but there is no specific field for including a JSON schema in the request [1][2].
To work around this, you can use the JsonToolkit
and JsonSpec
classes to create agents that understand and utilize JSON schemas. Here is an example of how to create such an agent:
import * as fs from "fs";
import * as yaml from "js-yaml";
import { OpenAI } from "@langchain/openai";
import { JsonSpec, JsonObject } from "langchain/tools";
import { JsonToolkit, createJsonAgent } from "langchain/agents";
export const run = async () => {
let data: JsonObject;
try {
const yamlFile = fs.readFileSync("openai_openapi.yaml", "utf8");
data = yaml.load(yamlFile) as JsonObject;
if (!data) {
throw new Error("Failed to load OpenAPI spec");
}
} catch (e) {
console.error(e);
return;
}
const toolkit = new JsonToolkit(new JsonSpec(data));
const model = new OpenAI({ temperature: 0 });
const executor = createJsonAgent(model, toolkit);
const input = `What are the required parameters in the request body to the /completions endpoint?`;
console.log(`Executing with input "${input}"...`);
const result = await executor.invoke({ input });
console.log(`Got output ${result.output}`);
console.log(
`Got intermediate steps ${JSON.stringify(
result.intermediateSteps,
null,
2
)}`
);
};
This approach allows for the use of JSON schemas in a structured manner, even though the schema itself is not directly passed in the OpenAI request [3].
Additionally, LangChain.js provides utilities to convert Zod schemas to JSON schemas and use them in tagging chains. Here is an example of how to create a metadata tagger using a Zod schema:
import { z } from "zod";
import { createMetadataTaggerFromZod } from "langchain/src/document_transformers/openai_functions";
const schema = z.object({
title: z.string(),
author: z.string(),
});
const options = {
llm: new ChatOpenAI({ modelName: "gpt-3.5-turbo-0613" }),
// other options...
};
const metadataTagger = createMetadataTaggerFromZod(schema, options);
This demonstrates how LangChain.js can work with JSON schemas indirectly through Zod schemas and other utilities [4].
To continue talking to Dosu, mention @dosu.
Checked other resources
Example Code
Error Message and Stack Trace (if applicable)
No response
Description
// When USE_TOOL_SCHEMA is true
// When USE_TOOL_SCHEMA is False
System Info
node --version
yarn --version