Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make generate.json compatible with OpenAI's function calling format #1376

Open
rlouf opened this issue Jan 14, 2025 · 3 comments
Open

Make generate.json compatible with OpenAI's function calling format #1376

rlouf opened this issue Jan 14, 2025 · 3 comments

Comments

@rlouf
Copy link
Member

rlouf commented Jan 14, 2025

OpenAI uses a small variation on the JSON Schema specification, and we need to make our interface compatible.

@punitvara
Copy link

@rlouf Can I pick up this task ?

@rlouf
Copy link
Member Author

rlouf commented Jan 17, 2025

By all means!

punitvara added a commit to punitvara/outlines that referenced this issue Jan 18, 2025
User should pass OpenAI style dictionary of function and
it should be internally converted to same json schema as
callable function (dottxt-ai#1376)
@punitvara
Copy link

@rlouf Can you check if this is what you expected ?

I tried to run few demos mentioned in Readme. I could see model gets downloaded but nothing happen after that. I also wrote code(pasted below) to test this PR. But I couldn't run it locally. Can you guide me please ?

import outlines

import requests

def get_weather(latitude, longitude):
    print ("get weather ")
    response = requests.get(f"https://api.open-meteo.com/v1/forecast?latitude={latitude}&longitude={longitude}&current=temperature_2m,wind_speed_10m&hourly=temperature_2m,relative_humidity_2m,wind_speed_10m")
    data = response.json()
    return data['current']['temperature_2m']

function_def = {
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Get current temperature for provided coordinates in celsius.",
        "parameters": {
            "type": "object",
            "properties": {
                "latitude": {"type": "number"},
                "longitude": {"type": "number"}
            },
            "required": ["latitude", "longitude"],
            "additionalProperties": False
        },
        "strict": True
    }
}


model = outlines.models.transformers("microsoft/Phi-3-mini-4k-instruct")

generator = outlines.generate.json(model, function_def)

prompt = """"Given the coordinates for Paris, France:
Latitude: 48.8566
Longitude: 2.3522
Generate the JSON to get the weather."""

result = generator(prompt)

print (result)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants