Hitting api #5595
Replies: 1 comment 4 replies
-
Hey @lakshya29154! 👋 I'm here to help you with bugs, questions, and becoming a contributor. Let's squash those bugs together! To create a script that takes a Swagger URL and a user query, finds the best suitable API, and automatically hits the API, you can use the
Here is the modified script: import * as fs from "fs";
import * as yaml from "js-yaml";
import { JsonSpec, JsonObject } from "langchain/tools";
import { createOpenApiAgent, OpenApiToolkit } from "langchain/agents";
import { OpenAI } from "@langchain/openai";
import { API_URL_PROMPT_TEMPLATE, API_RESPONSE_PROMPT_TEMPLATE } from "langchain/src/chains/api/prompts";
import { config } from "dotenv";
config();
const OPENAI_API_KEY = process.env.OPENAI_API_KEY;
/**
* Function to create and execute an OpenAPI agent based on a Swagger URL and user query.
* @param swaggerUrl The URL of the Swagger specification.
* @param userQuery The user query to find the best suitable API.
* @returns The response from the API.
*/
async function executeOpenApiAgent(swaggerUrl: string, userQuery: string) {
try {
// Load the OpenAPI specification from the Swagger URL
const response = await fetch(swaggerUrl);
const data = await response.json() as JsonObject;
if (!data) {
throw new Error("Failed to load OpenAPI spec");
}
// Initialize the language model
const model = new OpenAI({ temperature: 0, apiKey: OPENAI_API_KEY });
// Create the OpenAPI toolkit
const headers = {
"Content-Type": "application/json",
Authorization: `Bearer ${OPENAI_API_KEY}`,
};
const toolkit = new OpenApiToolkit(new JsonSpec(data), model, headers);
// Create the OpenAPI agent
const executor = createOpenApiAgent(model, toolkit);
// Generate the API URL using the prompt template
const apiUrlPrompt = API_URL_PROMPT_TEMPLATE.format({
api_docs: JSON.stringify(data),
question: userQuery,
});
const apiUrlResponse = await model.generate(apiUrlPrompt);
const apiUrl = apiUrlResponse.text.trim();
// Execute the API call
const apiResponse = await fetch(apiUrl, { headers });
const apiResponseData = await apiResponse.json();
// Summarize the API response using the prompt template
const apiResponsePrompt = API_RESPONSE_PROMPT_TEMPLATE.format({
api_docs: JSON.stringify(data),
question: userQuery,
api_url: apiUrl,
api_response: JSON.stringify(apiResponseData),
});
const summaryResponse = await model.generate(apiResponsePrompt);
const summary = summaryResponse.text.trim();
// Return the summarized response
return summary;
} catch (error) {
console.error('Error executing OpenAPI agent:', error);
throw error;
}
}
// Example usage
const swaggerUrl = 'https://petstore.swagger.io/v2/swagger.json';
const userQuery = 'Can you give me the API complete url to Return Pet inventories by status?';
executeOpenApiAgent(swaggerUrl, userQuery)
.then(response => console.log('API Response:', response))
.catch(error => console.error('Error:', error)); This script initializes the OpenAPI agent with the provided Swagger URL and user query, then executes the agent to get the response from the best suitable API. 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
Can someone help me I want to make a script that takes the url of swagger and query of user based on the query it will find the best suitable api and automatically hit the api if it require anything this will be given in prompt like any body needed for api but how can I do this
System Info
I am using langchain 0.1 documentation
Beta Was this translation helpful? Give feedback.
All reactions