-
-
Notifications
You must be signed in to change notification settings - Fork 704
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
647 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Using the Chatcompletion | ||
|
||
To get started with this api we must first instantiate a `ChatCompletion` object and build the api call | ||
by piping messages and functions to it. | ||
|
||
::: openai_function_call.dsl.completion | ||
|
||
## Messages Types | ||
|
||
The basis of a message is defined as a `dataclass`. However we provide helper functions and classes that provide additional functionality in the form of templates. | ||
|
||
::: openai_function_call.dsl.messages.base | ||
|
||
## Helper Messages / Templates | ||
|
||
::: openai_function_call.dsl.messages.messages | ||
|
||
::: openai_function_call.dsl.messages.user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# MultiTask | ||
|
||
We define a helper function `MultiTask` that dynamitcally creates a new schema that has a task attribute defined as a list of the task subclass, it including some prebuild prompts and allows us to avoid writing some extra code. | ||
|
||
::: openai_function_call.dsl.multitask |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
# Using the pipeline | ||
|
||
The pipeapi is some syntactic sugar to help build prompts in a readable way that avoids having to remember best practices around wording and structure. Examples include adding tips, tagging data with xml, or even including the chain of thought prompt as an assistant message. | ||
|
||
### Example Pipeline | ||
|
||
```python | ||
from openai_function_call import OpenAISchema, dsl | ||
from pydantic import Field | ||
|
||
|
||
class SearchQuery(OpenAISchema): | ||
query: str = Field( | ||
..., | ||
description="Detailed, comprehensive, and specific query to be used for semantic search", | ||
) | ||
|
||
|
||
SearchResponse = dsl.MultiTask( | ||
subtask_class=SearchQuery, | ||
) | ||
|
||
|
||
task = ( | ||
dsl.ChatCompletion(name="Segmenting Search requests example") | ||
| dsl.SystemTask(task="Segment search results") | ||
| dsl.TaggedMessage( | ||
content="can you send me the data about the video investment and the one about spot the dog?", | ||
tag="query", | ||
) | ||
| dsl.TipsMessage( | ||
tips=[ | ||
"Expand query to contain multiple forms of the same word (SSO -> Single Sign On)", | ||
"Use the title to explain what the query should return, but use the query to complete the search", | ||
"The query should be detailed, specific, and cast a wide net when possible", | ||
] | ||
) | ||
| SearchResponse | ||
) | ||
search_request = task.create() # type: ignore | ||
assert isinstance(search_request, SearchResponse) | ||
print(search_request.json(indent=2)) | ||
``` | ||
|
||
Output | ||
|
||
```json | ||
{ | ||
"tasks": [ | ||
{ | ||
"query": "data about video investment" | ||
}, | ||
{ | ||
"query": "data about spot the dog" | ||
} | ||
] | ||
} | ||
``` | ||
|
||
## Inspecting the API Call | ||
|
||
To make it easy for you to understand what this api is doing we default only construct the kwargs for the chat completion call. | ||
|
||
```python | ||
print(task.kwargs) | ||
``` | ||
|
||
```json | ||
{ | ||
"messages": [ | ||
{ | ||
"role": "system", | ||
"content": "You are a world class state of the art algorithm capable of correctly completing the following task: `Segment search results`." | ||
}, | ||
{ | ||
"role": "user", | ||
"content": "Consider the following data:\n\n<query>can you send me the data about the video investment and the one about spot the dog?</query>" | ||
}, | ||
{ | ||
"role": "user", | ||
"content": "Here are some tips to help you complete the task:\n\n* Expand query to contain multiple forms of the same word (SSO -> Single Sign On)\n* Use the title to explain what the query should return, but use the query to complete the search\n* The query should be detailed, specific, and cast a wide net when possible" | ||
} | ||
], | ||
"functions": [ | ||
{ | ||
"name": "MultiSearchQuery", | ||
"description": "Correctly segmented set of search queries", | ||
"parameters": { | ||
"type": "object", | ||
"properties": { | ||
"tasks": { | ||
"description": "Correctly segmented list of `SearchQuery` tasks", | ||
"type": "array", | ||
"items": { | ||
"$ref": "#/definitions/SearchQuery" | ||
} | ||
} | ||
}, | ||
"definitions": { | ||
"SearchQuery": { | ||
"type": "object", | ||
"properties": { | ||
"query": { | ||
"description": "Detailed, comprehensive, and specific query to be used for semantic search", | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"query" | ||
] | ||
} | ||
}, | ||
"required": [ | ||
"tasks" | ||
] | ||
} | ||
} | ||
], | ||
"function_call": { | ||
"name": "MultiSearchQuery" | ||
}, | ||
"max_tokens": 1000, | ||
"temperature": 0.1, | ||
"model": "gpt-3.5-turbo-0613" | ||
} | ||
``` |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from fastapi import FastAPI | ||
from openai_function_call import OpenAISchema | ||
import openai_function_call.dsl as dsl | ||
from pydantic import BaseModel, Field | ||
|
||
app = FastAPI(title="Example Application using openai_function_call") | ||
|
||
|
||
class SearchRequest(BaseModel): | ||
body: str | ||
|
||
|
||
class SearchQuery(OpenAISchema): | ||
title: str = Field(..., description="Question that the query answers") | ||
query: str = Field( | ||
..., | ||
description="Detailed, comprehensive, and specific query to be used for semantic search", | ||
) | ||
|
||
|
||
SearchResponse = dsl.MultiTask( | ||
subtask_class=SearchQuery, | ||
description="Correctly segmented set of search queries", | ||
) | ||
|
||
|
||
@app.post("/search", response_model=SearchResponse) | ||
async def search(request: SearchRequest): | ||
task = ( | ||
dsl.ChatCompletion(name="Segmenting Search requests example") | ||
| dsl.SystemTask(task="Segment search results") | ||
| dsl.TaggedMessage(content=request.body, tag="query") | ||
| dsl.TipsMessage( | ||
tips=[ | ||
"Expand query to contain multiple forms of the same word (SSO -> Single Sign On)", | ||
"Use the title to explain what the query should return, but use the query to complete the search", | ||
"The query should be detailed, specific, and cast a wide net when possible", | ||
] | ||
) | ||
| SearchRequest | ||
) | ||
return await task.acreate() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from openai_function_call import OpenAISchema, dsl | ||
from pydantic import Field | ||
|
||
|
||
class SearchQuery(OpenAISchema): | ||
query: str = Field( | ||
..., | ||
description="Detailed, comprehensive, and specific query to be used for semantic search", | ||
) | ||
|
||
|
||
SearchResponse = dsl.MultiTask( | ||
subtask_class=SearchQuery, | ||
description="Correctly segmented set of search queries", | ||
) | ||
|
||
|
||
task = ( | ||
dsl.ChatCompletion(name="Segmenting Search requests example") | ||
| dsl.SystemTask(task="Segment search results") | ||
| dsl.TaggedMessage( | ||
content="can you send me the data about the video investment and the one about spot the dog?", | ||
tag="query", | ||
) | ||
| dsl.TipsMessage( | ||
tips=[ | ||
"Expand query to contain multiple forms of the same word (SSO -> Single Sign On)", | ||
"Use the title to explain what the query should return, but use the query to complete the search", | ||
"The query should be detailed, specific, and cast a wide net when possible", | ||
] | ||
) | ||
| SearchResponse | ||
) | ||
import pprint | ||
|
||
import json | ||
|
||
print(json.dumps(task.kwargs, indent=1)) | ||
""" | ||
{ | ||
"tasks": [ | ||
{ | ||
"query": "data about video investment" | ||
}, | ||
{ | ||
"query": "data about spot the dog" | ||
} | ||
] | ||
} | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from .function_calls import OpenAISchema, openai_function, openai_schema | ||
from .dsl.multitask import MultiTask | ||
|
||
__all__ = ["OpenAISchema", "openai_function", "openai_schema"] | ||
__all__ = ["OpenAISchema", "openai_function", "MultiTask", "openai_schema"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
from .completion import ChatCompletion | ||
from .multitask import MultiTask | ||
from .messages import * | ||
from .multitask import MultiTask | ||
|
||
__all__ = ["ChatCompletion", "MultiTask", "messages"] |
Oops, something went wrong.