mirror of
https://github.com/kennethreitz/instructor.git
synced 2026-06-05 22:50:18 +00:00
Improve doc configs w/ line highlights (#48)
* improve quickstart * add highlights * more docs
This commit is contained in:
@@ -2,8 +2,9 @@
|
||||
|
||||
In this example, we'll demonstrate how to use OpenAI Function Call to ask an AI a question and get back an answer with correct citations. We'll define the necessary data structures using Pydantic and show how to retrieve the citations for each answer.
|
||||
|
||||
!!! note "Motivation"
|
||||
Often times retrival augmented models hallucinate. Wouldn't it be great if each sentence came with citations that were no on the chunk level but at the substring level. Moreover, to ensure the quote exists we can search the string to find the exact quote!
|
||||
!!! tips "Motivation"
|
||||
When using AI models to answer questions, it's important to provide accurate and reliable information with appropriate citations. By including citations for each statement, we can ensure the information is backed by reliable sources and help readers verify the information themselves.
|
||||
|
||||
|
||||
## Defining the Data Structures
|
||||
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
# List of Examples
|
||||
# Function Calls by Example
|
||||
|
||||
Welcome to the examples page. Here you will find detailed information on how to use our code and examples demonstrating various features and functionalities.
|
||||
Welcome to the examples page. Here you will find emails that highlight a range of use cases, on how to use our code and examples demonstrating various features and functionalities.
|
||||
|
||||
## Library
|
||||
## Quick Links
|
||||
|
||||
- [Segmented Search](search.md)
|
||||
- [One shot Query Planning](planning-tasks.md)
|
||||
- [Recursive Schemas](recursive.md)
|
||||
- [Exact Citations](exact_citations.md)
|
||||
- [Automated Dataframe Extraction](autodataframe.md)
|
||||
- [Segmenting search requests into multiple search queries](search.md)
|
||||
- [One shot query planning](planning-tasks.md)
|
||||
- [Using recursive schema](recursive.md)
|
||||
- [Exact citations using regex](exact_citations.md)
|
||||
- [Automated database extraction from text](autodataframe.md)
|
||||
|
||||
## Details
|
||||
|
||||
@@ -18,7 +18,7 @@ In this section, you will find examples demonstrating different aspects of our p
|
||||
|
||||
- [One shot Query Planning](planning-tasks.md): Explore how to plan and decompose a complex query into multiple subqueries in a single request.
|
||||
|
||||
- [Recursive Schemas](recursive.md): Understand how to work with recursive schemas, and why flat is better than nested.
|
||||
- [Recursive Schemas](recursive.md): Understand how to work with recursive schemas, and also why flat is better than nested.
|
||||
|
||||
- [Exact Citations](exact_citations.md): Find out how to generate exact citations by using smart prompting and regular expressions
|
||||
|
||||
|
||||
@@ -1,9 +1,18 @@
|
||||
# Example: Planning and Executing a Query Plan
|
||||
|
||||
In this example, we will demonstrate how to use the OpenAI Function Call `ChatCompletion` model to plan and execute a query plan in a question-answering system. We will define the necessary structures using Pydantic and show how to execute the query plan step-by-step.
|
||||
This example demonstrates how to use the OpenAI Function Call ChatCompletion model to plan and execute a query plan in a question-answering system. By breaking down a complex question into smaller sub-questions with defined dependencies, the system can systematically gather the necessary information to answer the main question.
|
||||
|
||||
!!! note "Motivation"
|
||||
Multishop q/a is iterative and may never end, by trying to plan the queryin one step we know we'll have a finite number of steps. It also helps us break apart questions to figure what we can or cannot answer with available data.
|
||||
!!! tips "Motivation"
|
||||
The goal of this example is to showcase how query planning can be used to handle complex questions, facilitate iterative information gathering, automate workflows, and optimize processes. By leveraging the OpenAI Function Call model, you can design and execute a structured plan to find answers effectively.
|
||||
|
||||
**Use Cases:**
|
||||
|
||||
* Complex question answering
|
||||
* Iterative information gathering
|
||||
* Workflow automation
|
||||
* Process optimization
|
||||
|
||||
With the OpenAI Function Call model, you can customize the planning process and integrate it into your specific application to meet your unique requirements.
|
||||
|
||||
## Defining the Structures
|
||||
|
||||
|
||||
@@ -2,8 +2,10 @@
|
||||
|
||||
In this example, we will demonstrate how to leverage the `MultiTask` and `enum.Enum` features of OpenAI Function Call to segment search queries. We will define the necessary structures using Pydantic and demonstrate how segment query into multiple sub queries and execute them in parallel with `asyncio`.
|
||||
|
||||
!!! note "Motivation"
|
||||
Extracting a list of tasks from text is one of the most common examples of using LLMs for 'intent' I can imagine a system like this powering Siri or Alexa fairly soon.
|
||||
!!! tips "Motivation"
|
||||
|
||||
Extracting a list of tasks from text is a common use case for leveraging language models. This pattern can be applied to various applications, such as virtual assistants like Siri or Alexa, where understanding user intent and breaking down requests into actionable tasks is crucial. In this example, we will demonstrate how to use OpenAI Function Call to segment search queries and execute them in parallel.
|
||||
|
||||
|
||||
## Defining the Structures
|
||||
|
||||
@@ -57,7 +59,7 @@ MultiSearch = MultiTask(Search)
|
||||
|
||||
To segment a search query, we will use the base openai api. We can define a function that takes a string and returns segmented search queries using the `MultiSearch` class.
|
||||
|
||||
```python
|
||||
```python hl_lines="7 8"
|
||||
import openai
|
||||
|
||||
def segment(data: str) -> MultiSearch:
|
||||
@@ -104,6 +106,4 @@ The output will be:
|
||||
```
|
||||
Searching for `Please send me the video from last week about the investment case study` with query `Please send me the video from last week about the investment case study` using `SearchType.VIDEO`
|
||||
Searching for `also documents about your GDPR policy?` with query `also documents about your GDPR policy?` using `SearchType.EMAIL`
|
||||
```
|
||||
|
||||
This demonstrates how to use the Prompt Pipeline to segment search queries and execute them asynchronously.
|
||||
```
|
||||
+63
-50
@@ -1,42 +1,66 @@
|
||||
# Welcome to OpenAI Function Call
|
||||
# OpenAI Function Calls Quick Start Guide
|
||||
|
||||
OpenAI Function Call is a library that provides a minimal and non-intrusive extension to the `Pydantic.BaseModel` class called `OpenAISchema`. It offers two main methods: `openai_schema` for generating the correct schema and `from_response` for creating an instance of the class from the completion result.
|
||||
|
||||
The library primarily focuses on showcasing examples and providing a helper class, so I'll keep the example as a simple structured extraction.
|
||||
|
||||
If OpenAI is like a chef's knife for code, I aim to provide you with a nice handle and a little booklet of cutting techniques. OpenAI Function Call leverages the data validation capabilities of the Pydantic library to handle output parsing in a structured and reliable manner.
|
||||
|
||||
If you have any feedback or need assistance, feel free to leave an issue or reach out to me on [Twitter](https://twitter.com/jxnlco).
|
||||
|
||||
If you're looking for a more comprehensive solution with batteries included, I highly recommend [MarvinAI](https://www.askmarvin.ai/). MarvinAI provides a high-level API but doesn't offer as much access to prompting.
|
||||
|
||||
!!! tip "Just rip it out!"
|
||||
If you don't want to install dependencies, you can literally take the `function_calls.py` file from the library's source code and add it to your own codebase. You can find the [source code here](https://github.com/jxnl/openai_function_call/blob/main/openai_function_call/function_calls.py).
|
||||
Welcome to the quick start guide for OpenAI Function Call! This guide will walk you through the installation process and provide examples demonstrating the usage of function calls and schemas with OpenAI and Pydantic.
|
||||
|
||||
## Installation
|
||||
|
||||
You can install OpenAI Function Call using pip:
|
||||
To get started with OpenAI Function Call, you need to install it using pip. Run the following command in your terminal:
|
||||
|
||||
```sh
|
||||
```bash
|
||||
pip install openai_function_call
|
||||
```
|
||||
|
||||
## Usage
|
||||
## Quick Start
|
||||
|
||||
Below are some examples that demonstrate the usage of function calls and schemas with OpenAI and Pydantic. In subsequent documentation, we will explore more creative use cases.
|
||||
This quick start guide contains the follow sections
|
||||
|
||||
### Example 1: Extraction
|
||||
1. Defining a schema
|
||||
2. Adding Additional Prompting
|
||||
3. Calling the ChatCompletion
|
||||
4. deserializing back to the instance
|
||||
|
||||
Prompts are now sourced from docstrings and field descriptions, so it's important to write clear and descriptive documentation for your schemas.
|
||||
OpenAI Function Call allows you to leverage OpenAI's powerful language models for function calls and schema extraction. This guide provides a quick start for using OpenAI Function Call.
|
||||
|
||||
### Section 1: Defining a Schema
|
||||
|
||||
To begin, let's define a schema using OpenAI Function Call. A schema describes the structure of the input and output data for a function. In this example, we'll define a simple schema for a `User` object:
|
||||
|
||||
```python
|
||||
import openai
|
||||
from openai_function_call import OpenAISchema
|
||||
|
||||
class UserDetails(OpenAISchema):
|
||||
name: str
|
||||
age: int
|
||||
```
|
||||
|
||||
In this schema, we define a `UserDetails` class that extends `OpenAISchema`. We declare two fields, `name` and `age`, of type `str` and `int` respectively. It's important to note that since OpenAI models do not understand annotations or extra metadata like descriptions, we keep the definition clean without docstrings or field descriptions.
|
||||
|
||||
### Section 2: Adding Additional Prompting
|
||||
|
||||
To enhance the performance of the OpenAI language model, you can add additional prompting in the form of docstrings and field descriptions. They can provide context and guide the model on how to process the data.
|
||||
|
||||
```python hl_lines="5 6"
|
||||
from openai_function_call import OpenAISchema
|
||||
from pydantic import Field
|
||||
|
||||
class UserDetails(OpenAISchema):
|
||||
"""Details of a user"""
|
||||
"Correctly extracted user information"
|
||||
name: str = Field(..., description="User's full name")
|
||||
age: int
|
||||
```
|
||||
|
||||
In this updated schema, we use the `Field` class from `pydantic` to add descriptions to the `name` field. The description provides information about the field, giving even more context to the language model.
|
||||
|
||||
### Section 3: Calling the ChatCompletion
|
||||
|
||||
With the schema defined, let's proceed with calling the `ChatCompletion` API using the defined schema and messages.
|
||||
|
||||
```python hl_lines="11 12 15"
|
||||
from openai_function_call import OpenAISchema
|
||||
from pydantic import Field
|
||||
|
||||
class UserDetails(OpenAISchema):
|
||||
"Correctly extracted user information"
|
||||
name: str = Field(..., description="User's full name")
|
||||
age: int
|
||||
|
||||
@@ -49,39 +73,28 @@ completion = openai.ChatCompletion.create(
|
||||
{"role": "user", "content": "My name is John Doe and I'm 30 years old."},
|
||||
],
|
||||
)
|
||||
|
||||
user_details = UserDetails.from_response(completion)
|
||||
print(user_details) # UserDetails(name='John Doe', age=30)
|
||||
```
|
||||
|
||||
### Example 2: Function Calls
|
||||
In this example, we make a call to the `ChatCompletion` API by providing the model name (`gpt-3.5-turbo-0613`) and a list of messages. The messages consist of a system message and a user message. The system message sets the context by requesting user details, while the user message provides the input with the user's name and age.
|
||||
|
||||
```python
|
||||
import openai
|
||||
from openai_function_call import openai_function
|
||||
Note that we have omitted the additional parameters that can be included in the API request, such as `temperature`, `max_tokens`, and `n`. These parameters can be customized according to your requirements.
|
||||
|
||||
@openai_function
|
||||
def sum(a:int, b:int) -> int:
|
||||
"""Sum description adds a + b"""
|
||||
return a + b
|
||||
### Section 4: Deserializing Back to the Instance
|
||||
|
||||
completion = openai.ChatCompletion.create(
|
||||
model="gpt-3.5-turbo-0613",
|
||||
temperature=0,
|
||||
functions=[sum.openai_schema],
|
||||
function_call={"name": sum.openai_schema["name"]},
|
||||
messages=[
|
||||
{
|
||||
"role": "system",
|
||||
"content": "You must use the `sum` function instead of adding yourself.",
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": "What is 6+3",
|
||||
},
|
||||
],
|
||||
)
|
||||
To deserialize the response from the `ChatCompletion` API back into an instance of the `UserDetails` class, we can use the `from_response` method.
|
||||
|
||||
result = sum.from_response(completion)
|
||||
print(result) # 9
|
||||
```python hl_lines="1"
|
||||
user = UserDetails.from_response(response)
|
||||
print(user.name) # Output: John Doe
|
||||
print(user.age) # Output: 30
|
||||
```
|
||||
|
||||
By calling `UserDetails.from_response`, we create an instance of the `UserDetails` class using the response from the API call. Subsequently, we can access the extracted user details through the `name` and `age` attributes of the `user` object.
|
||||
|
||||
## Next Steps
|
||||
|
||||
This quick start guide provided you with a basic understanding of how to use OpenAI Function Call for schema extraction and function calls. You can now explore more advanced use cases and creative applications of this library.
|
||||
|
||||
If you have any questions, feel free to leave an issue or reach out to the library's author on [Twitter](https://twitter.com/jxnlco). For a more comprehensive solution with additional features, consider checking out [MarvinAI](https://www.askmarvin.ai/).
|
||||
|
||||
To see more examples of how we can create interesting models check out some [examples](examples/index.md)
|
||||
+16
-10
@@ -1,10 +1,18 @@
|
||||
site_name: OpenAI Function call
|
||||
site_name: OpenAI Function Call Library
|
||||
site_description: Enhancing OpenAI function calling with Pydantic
|
||||
repo_name: openai_function_call
|
||||
repo_url: https://github.com/jxnl/openai_function_call
|
||||
site_url: https://openai-function-call.onrender.com/
|
||||
theme:
|
||||
name: material
|
||||
icon:
|
||||
repo: fontawesome/brands/github
|
||||
features:
|
||||
- navigation.instant
|
||||
- navigation.tabs
|
||||
- navigation.tabs.sticky
|
||||
- navigation.expand
|
||||
- content.code.annotate
|
||||
plugins:
|
||||
- social
|
||||
- mkdocstrings:
|
||||
@@ -14,8 +22,6 @@ plugins:
|
||||
members_order: alphabetical
|
||||
allow_inspection: true
|
||||
show_bases: true
|
||||
repo_url: https://github.com/jxnl/openai_function_call
|
||||
site_url: https://openai-function-call.onrender.com/
|
||||
markdown_extensions:
|
||||
- pymdownx.critic
|
||||
- pymdownx.caret
|
||||
@@ -39,10 +45,10 @@ nav:
|
||||
- "MultiTask Schema": "multitask.md"
|
||||
- "Introduction: Pipeline API": "pipeline-example.md"
|
||||
- "Message Templates": "chat-completion.md"
|
||||
- Examples:
|
||||
- 'Example applications': 'examples/index.md'
|
||||
- 'Example: Segmented Search': 'examples/search.md'
|
||||
- 'Example: One shot Query Planning': 'examples/planning-tasks.md'
|
||||
- 'Example: Recursive Schemas': 'examples/recursive.md'
|
||||
- 'Example: Exact Citations': 'examples/exact_citations.md'
|
||||
- 'Example: Automated Dataframe Extraction': "examples/autodataframe.md"
|
||||
- Use Cases:
|
||||
- 'Overview': 'examples/index.md'
|
||||
- 'Segmented Search': 'examples/search.md'
|
||||
- 'One shot Query Planning': 'examples/planning-tasks.md'
|
||||
- 'Recursive Schemas': 'examples/recursive.md'
|
||||
- 'Exact Citations': 'examples/exact_citations.md'
|
||||
- 'Automated Dataframe Extraction': "examples/autodataframe.md"
|
||||
@@ -39,8 +39,7 @@ def MultiTask(
|
||||
then the description is set to `Correct segmentation of `{subtask_class.__name__}` tasks`
|
||||
|
||||
Returns:
|
||||
OpenAISchema: A new class that can be used to segment multiple tasks
|
||||
|
||||
schema (OpenAISchema): A new class that can be used to segment multiple tasks
|
||||
"""
|
||||
task_name = subtask_class.__name__ if name is None else name
|
||||
|
||||
|
||||
Reference in New Issue
Block a user