outlines icon indicating copy to clipboard operation
outlines copied to clipboard

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

Open rlouf opened this issue 10 months ago • 6 comments

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

rlouf avatar Jan 14 '25 09:01 rlouf

@rlouf Can I pick up this task ?

punitvara avatar Jan 17 '25 12:01 punitvara

By all means!

rlouf avatar Jan 17 '25 12:01 rlouf

@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)

punitvara avatar Jan 18 '25 16:01 punitvara

@rlouf It seems like you planned to do this other way. Can I close this PR ? I would like to contribute to resolve other issue. Is there any issue I can pick for first time contributor ?

punitvara avatar Jan 28 '25 04:01 punitvara

What do you mean by planned to do this another way?

rlouf avatar Jan 29 '25 20:01 rlouf

Sorry I misunderstood when you removed tags. Can you review PR draft and let me know if that's what you are expecting ?

punitvara avatar Jan 30 '25 07:01 punitvara