edreisMD / plugnplai

🎸 Integrating AI plugins to LLMs
https://plugnplai.com
MIT License
231 stars 17 forks source link

Update Plugins Class a new function "def functions():" that returns the json object to be used in the openAI API functions parameter (step 1) describing the active plugins in this format. This is an structured alternative of plugins.prompt. #160

Closed edreisMD closed 1 year ago

edreisMD commented 1 year ago

Developers can now describe functions to gpt-4-0613 and gpt-3.5-turbo-0613, and have the model intelligently choose to output a JSON object containing arguments to call those functions. This is a new way to more reliably connect GPT's capabilities with external tools and APIs.

These models have been fine-tuned to both detect when a function needs to be called (depending on the user’s input) and to respond with JSON that adheres to the function signature. Function calling allows developers to more reliably get structured data back from the model. For example, developers can:

Create chatbots that answer questions by calling external tools (e.g., like ChatGPT Plugins) Convert queries such as “Email Anya to see if she wants to get coffee next Friday” to a function call like send_email(to: string, body: string), or “What’s the weather like in Boston?” to get_current_weather(location: string, unit: 'celsius' | 'fahrenheit').

Convert natural language into API calls or database queries Convert “Who are my top ten customers this month?” to an internal API call such as get_customers_by_revenue(start_date: string, end_date: string, limit: int), or “How many orders did Acme, Inc. place last month?” to a SQL query using sql_query(query: string).

Extract structured data from text Define a function called extract_people_data(people: [{name: string, birthday: string, location: string}]), to extract all people mentioned in a Wikipedia article.

These use cases are enabled by new API parameters in our /v1/chat/completions endpoint, functions and function_call, that allow developers to describe functions to the model via JSON Schema, and optionally ask it to call a specific function. Get started with our developer documentation and add evals if you find cases where function calling could be improved

Function calling example What’s the weather like in Boston right now?

Step 1· OpenAI API Call the model with functions and the user’s input

Request 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"] } } ] }'

Response

"id": "chatcmpl-123", ... "choices": [{ "index": 0, "message": { "role": "assistant", "content": null, "function_call": { "name": "get_current_weather", "arguments": "{ \"location\": \"Boston, MA\"}" } }, "finish_reason": "function_call" }] }

Step 2· Third party API Use the model response to call your API

Request curl https://weatherapi.com/...

Response { "temperature": 22, "unit": "celsius", "description": "Sunny" }

Step 3· OpenAI API Send the response back to the model to summarize

Request 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?"}, {"role": "assistant", "content": null, "function_call": {"name": "get_current_weather", "arguments": "{ \"location\": \"Boston, MA\"}"}}, {"role": "function", "name": "get_current_weather", "content": "{\"temperature\": "22", \"unit\": \"celsius\", \"description\": \"Sunny\"}"} ], "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"] } } ] }'

Response { "id": "chatcmpl-123", ... "choices": [{ "index": 0, "message": { "role": "assistant", "content": "The weather in Boston is currently sunny with a temperature of 22 degrees Celsius.", }, "finish_reason": "stop" }] }

The weather in Boston is currently sunny with a temperature of 22 degrees Celsius.

sweep-ai[bot] commented 1 year ago

Hey @edreisMD,

I've started working on your request. The plan is to add a new function to the Plugins class that will return a JSON object describing the active plugins. This will be used in the OpenAI API functions parameter and will serve as a structured alternative to plugins.prompt.

Give me a minute!

Some code snippets I looked at (click to expand). If some file is missing from here, you can mention the path in the ticket description. https://github.com/edreisMD/plugnplai/blob/ac5cc79cf494703ad2abcb7ff33ddb1127dd3f4f/examples/old_gpt4_plugins_step_by_step.ipynb#L569-L580 https://github.com/edreisMD/plugnplai/blob/ac5cc79cf494703ad2abcb7ff33ddb1127dd3f4f/examples/plugins_step_by_step.ipynb#L518-L529 https://github.com/edreisMD/plugnplai/blob/ac5cc79cf494703ad2abcb7ff33ddb1127dd3f4f/docs/examples/plugins_step_by_step.ipynb#L569-L580 https://github.com/edreisMD/plugnplai/blob/ac5cc79cf494703ad2abcb7ff33ddb1127dd3f4f/docs/modules/plugins.md#L24-L60 https://github.com/edreisMD/plugnplai/blob/ac5cc79cf494703ad2abcb7ff33ddb1127dd3f4f/examples/langchain_chatgpt_example.ipynb#L1-L53

I'm a bot that handles simple bugs and feature requests but I might make mistakes. Please be kind!