-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add function call signatures * Upgrade basic examples * Add basic function call test * Lint * Raise error on invalid pydantic version * Update README * Update default pydantic * Make fn_calling tests optional * Export ListResponse from main package * Support functions outputs in message type * Return parsed content alongside function * Return raw response in RunResponse * Ignore templating for raw return requests * Fix missing role in tests * Require pydantic2 * Allow templating of function names in prompt * Update docs * Fix typehinting of functions * Fix multiline descriptions
- Loading branch information
1 parent
1b0e09c
commit f8c92a2
Showing
22 changed files
with
1,037 additions
and
212 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
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,62 @@ | ||
import asyncio | ||
from enum import Enum | ||
from json import dumps as json_dumps | ||
from os import getenv | ||
|
||
from dotenv import load_dotenv | ||
from pydantic import BaseModel, Field | ||
|
||
from gpt_json import GPTJSON, GPTMessage, GPTMessageRole | ||
|
||
load_dotenv() | ||
API_KEY = getenv("OPENAI_API_KEY") | ||
|
||
|
||
class UnitType(Enum): | ||
CELSIUS = "celsius" | ||
FAHRENHEIT = "fahrenheit" | ||
|
||
|
||
class GetCurrentWeatherRequest(BaseModel): | ||
location: str = Field(description="The city and state, e.g. San Francisco, CA") | ||
unit: UnitType | None = None | ||
|
||
|
||
class DataPayload(BaseModel): | ||
data: str | ||
|
||
|
||
def get_current_weather(request: GetCurrentWeatherRequest): | ||
""" | ||
Get the current weather in a given location | ||
The rest of the docstring should be omitted. | ||
""" | ||
weather_info = { | ||
"location": request.location, | ||
"temperature": "72", | ||
"unit": request.unit, | ||
"forecast": ["sunny", "windy"], | ||
} | ||
return json_dumps(weather_info) | ||
|
||
|
||
async def runner(): | ||
gpt_json = GPTJSON[DataPayload](API_KEY, functions=[get_current_weather]) | ||
response = await gpt_json.run( | ||
messages=[ | ||
GPTMessage( | ||
role=GPTMessageRole.USER, | ||
content="What's the weather like in Boston, in F?", | ||
), | ||
], | ||
) | ||
|
||
print(response) | ||
assert response.function_call == get_current_weather | ||
assert response.function_arg == GetCurrentWeatherRequest( | ||
location="Boston", unit=UnitType.FAHRENHEIT | ||
) | ||
|
||
|
||
asyncio.run(runner()) |
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
Oops, something went wrong.