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

Added GraphQLClientTool #3091

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open

Conversation

mishushakov
Copy link
Contributor

I have added a GraphQL client tool to execute graphql requests.

Example:

import { z } from "zod";
import { RunnableSequence } from "../../schema/runnable/index.js";
import { PromptTemplate } from "../../prompts/index.js";
import { OpenAI } from "../../llms/openai.js";
import { StructuredOutputParser } from "../../output_parsers/index.js";
import { GraphQLClientTool } from "../graphql.js";

const parser = StructuredOutputParser.fromZodSchema(
  z.object({
    query: z.string(),
    variables: z.object({}),
  })
);

const chain = RunnableSequence.from([
  PromptTemplate.fromTemplate(
    "Make a GraphQL query to get the country code of Germany.\n{format_instructions}\n{schema}"
  ),
  new OpenAI({ temperature: 0 }),
  parser,
  new GraphQLClientTool({
    endpoint: "https://countries.trevorblades.com",
  }),
]);

const response = await chain.invoke({
  schema: `
      type Continent {
        code: ID!
        countries: [Country!]!
        name: String!
      }

      input ContinentFilterInput {
        code: StringQueryOperatorInput
      }

      type Country {
        awsRegion: String!
        capital: String
        code: ID!
        continent: Continent!
        currencies: [String!]!
        currency: String
        emoji: String!
        emojiU: String!
        languages: [Language!]!
        name(lang: String): String!
        native: String!
        phone: String!
        phones: [String!]!
        states: [State!]!
        subdivisions: [Subdivision!]!
      }

      input CountryFilterInput {
        code: StringQueryOperatorInput
        continent: StringQueryOperatorInput
        currency: StringQueryOperatorInput
        name: StringQueryOperatorInput
      }

      type Language {
        code: ID!
        name: String!
        native: String!
        rtl: Boolean!
      }

      input LanguageFilterInput {
        code: StringQueryOperatorInput
      }

      type Query {
        continent(code: ID!): Continent
        continents(filter: ContinentFilterInput = {}): [Continent!]!
        countries(filter: CountryFilterInput = {}): [Country!]!
        country(code: ID!): Country
        language(code: ID!): Language
        languages(filter: LanguageFilterInput = {}): [Language!]!
      }

      type State {
        code: String
        country: Country!
        name: String!
      }

      input StringQueryOperatorInput {
        eq: String
        in: [String!]
        ne: String
        nin: [String!]
        regex: String
      }

      type Subdivision {
        code: ID!
        emoji: String
        name: String!
      }
      `,
  format_instructions: parser.getFormatInstructions(),
});

console.log(response);

@dosubot dosubot bot added the auto:enhancement A large net-new component, integration, or chain. Use sparingly. The largest features label Oct 29, 2023
@@ -0,0 +1,41 @@
import { z } from "zod";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR adds a new HTTP request using the fetch function. Please review this change to ensure it aligns with the project's requirements and best practices.

Copy link
Collaborator

@jacoblee93 jacoblee93 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this! Similar to your Python interpreter one we've started flagging to folks that the LLM could generate arbitrary mutations, and that this should live under experimental with a security warning. Would you mind moving?

@jacoblee93 jacoblee93 self-assigned this Oct 31, 2023
@jacoblee93 jacoblee93 added the close PRs that need one or two touch-ups to be ready label Oct 31, 2023
@mishushakov
Copy link
Contributor Author

Sure! Will follow up on both some time this week

@mishushakov
Copy link
Contributor Author

I have moved the tool under tools/experimental. Where should I put the docs and the warning now?

@qnm
Copy link

qnm commented Nov 30, 2023

@mishushakov This works pretty well for my purposes, however when I use it as part of a structured-chat-zero-shot-react-description agent, only query is passed to the tool, never variables. I ended up setting variables to optional in the type definition to work around this.

Unfortunately, I only started using langchain a couple of days ago so I don't really know if that's expected for that type of agent, or if something else is required. Any help is appreciated!

@mishushakov
Copy link
Contributor Author

Glad to hear that! Variables aren't required, so I should probably make it optional as you suggested 😀

@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Nov 30, 2023
Copy link

vercel bot commented Nov 30, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
langchainjs-api-refs ❌ Failed (Inspect) Dec 27, 2023 11:16am
langchainjs-docs ❌ Failed (Inspect) Dec 27, 2023 11:16am

@jacoblee93
Copy link
Collaborator

Looks good - sounds like you're going to address @qnm's feedback though?

@mishushakov
Copy link
Contributor Author

Yep and I believe I'd need to update the unit test here as well

@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:L This PR changes 100-499 lines, ignoring generated files. labels Dec 1, 2023
@mishushakov
Copy link
Contributor Author

I have updated the tool, tests and moved it to experimental/tools now
Anything else needed?

Copy link
Collaborator

@bracesproul bracesproul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this! Looks good to me once my comments have been implemented!

@mishushakov
Copy link
Contributor Author

Hello and Merry Christmas! I have done so in the latest commit

@mishushakov
Copy link
Contributor Author

Yo @bracesproul & @jacoblee93, would you be able to review? Thank you so much 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto:enhancement A large net-new component, integration, or chain. Use sparingly. The largest features close PRs that need one or two touch-ups to be ready size:M This PR changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants