Welcome to the world of response parsing for LLM agents! This guide will show you how to help your LLM agents get exactly the data they need from GraphQL responses. It's like giving your AI agents a data extraction superpower! 🤖
When LLM agents interact with GraphQL APIs, they need clean, structured data to:
Without proper parsing, LLMs might struggle with:
Here's how we can help our LLM agents get exactly what they need:
import { GraphqlAgentTool } from "graphql-agent-tool";
const graphqlCapitalTool = new GraphqlAgentTool({
name: "getCountryCapital",
purpose: "Retrieves the capital city of a country...",
url: "https://countries.trevorblades.com",
query: `
query GetCountryCapital($countryCode: ID!) {
country(code: $countryCode) {
capital
}
}
`,
responseParser: (response: unknown) => {
const { data } = response as { data: { country: { capital: string } } };
return {
capital: data.country.capital,
// Add metadata to help LLM understand the context
context: "This is the capital city of the requested country",
type: "string"
};
},
}).getTool();
Let's say your LLM needs to make decisions based on country information:
responseParser: (response: unknown) => {
const { data } = response as {
data: {
country: {
capital: string,
population: number,
languages: { name: string }[]
}
}
};
return {
capital: data.country.capital,
population: data.country.population,
languages: data.country.languages.map(lang => lang.name),
// Add decision-making context for the LLM
context: {
type: "country_information",
purpose: "Use this data to answer questions about the country",
fields: {
capital: "The capital city of the country",
population: "Total population count",
languages: "List of official languages"
}
}
};
}
Ready to explore more? Check out:
Happy LLM-powered parsing! 🎉