openai / openai-cookbook

Examples and guides for using the OpenAI API
https://cookbook.openai.com
MIT License
59.57k stars 9.48k forks source link

[FEATURE] more Pythonic way of populating function params #722

Closed Clement-Lelievre closed 11 months ago

Clement-Lelievre commented 1 year ago

Suggestion: how about leveraging Python dunders to better fill in the functions parameter of the request rather than hardcoding it? It'd be more Pythonic and maintainable. (This assumes that the function signature contains a docstring and type hints (so, is incompatible with lambdas).)

This could also be implemented in the function-calling guide

SupreethRao99 commented 1 year ago

you can pass a pydantic schema to the function calling like this:


import openai
from pydantic import BaseModel, Field

class GeoSchema(BaseModel):
    latitude: float = Field(..., description="The latitude component")
    longitude : float = Field(..., description="The longitude component")

result = openai.ChatCompletetion.create(
        model="gpt-3.5-turbo",
        messages = [
          {"role": "system" , "content": "You are a helpful Tour planning AI"},
          {"role": "user", "content": "come up with a random location"}
       ],
        functions=[{
            "name": "random_place_finder", # this is your function name
            "description": "gives the latitude and longitude of a random place on earth",
            "parameters": GeoSchema.model_json_schema()
        }],
        function_call={"name": "random_place_finder"},
        temperature=1)
Clement-Lelievre commented 1 year ago

Hi @SupreethRao99

thanks for the reply.

This looks to me more like an alternative/way to complement my proposal than a definitive enhancement, as your proposal still hardcodes the function name and description.

github-actions[bot] commented 11 months ago

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 10 days.

github-actions[bot] commented 11 months ago

This issue was closed because it has been stalled for 10 days with no activity.