langchain-ai / langchainjs

🦜🔗 Build context-aware reasoning applications 🦜🔗
https://js.langchain.com/docs/
MIT License
12.4k stars 2.1k forks source link

Make use of Function Calling & other updates to OpenAI API #1630

Closed danny-avila closed 1 year ago

danny-avila commented 1 year ago

The latest update to the GPT-4-0613 and GPT-3.5-turbo-0613 models introduces a new feature: Function Calling. This feature allows us to describe functions to the models, which can then intelligently output a JSON object containing arguments to call those functions. This provides a more reliable way to connect GPT's capabilities with external tools and APIs.

Details:

The models have been fine-tuned to detect when a function needs to be called based on user input and respond with JSON that adheres to the function signature. This allows usto get structured data back from the model more reliably. Use cases include:

The feature is enabled by new API parameters in the /v1/chat/completions endpoint, functions and function_call, which allow developers to describe functions to the model via JSON Schema and optionally ask it to call a specific function.

Example:

For instance, a query like "What’s the weather like in Boston?" can be converted to a function call like get_current_weather(location: string, unit: 'celsius' | 'fahrenheit').

curl https://api.openai.com/v1/chat/completions -u :$OPENAI_API_KEY -H 'Content-Type: application/json' -d '{
  "model": "gpt-3.5-turbo-0613",
  "messages": [
    {"role": "user", "content": "What is the weather like in Boston?"}
  ],
  "functions": [
    {
      "name": "get_current_weather",
      "description": "Get the current weather in a given location",
      "parameters": {
        "type": "object",
        "properties": {
          "location": {
            "type": "string",
            "description": "The city and state, e.g. San Francisco, CA"
          },
          "unit": {
            "type": "string",
            "enum": ["celsius", "fahrenheit"]
          }
        },
        "required": ["location"]
      }
    }
  ]
}'

Source

Motivation

I'm adding this issue mostly to flag and track this OpenAI release and kick off a forum for discussion. There were additions concerning context length and newer versions of models, but I'm mainly highlighting the feature most relevant to the core of langchain.

Your contribution

Can potentially add PRs but haven't contributed here previously.

jedwards1230 commented 1 year ago

Langchain for python has this implemented. Here's a reference.

PR

Example

danny-avila commented 1 year ago

I see it's already being worked on in #1631, awesome!

muhammadfaizan commented 1 year ago

anyone working on it from TS side?