Closed ramon-prieto closed 5 months ago
Taking a look, it looks like some interesting/weird stuff is happening.
gpt-4-turbo-2024-04-09
it works, but it provides a hallucinated due date. This can be resolved by giving due_date: str = ""
the default empty string.Thank you for bringing this to our attention! I will hopefully get fixes for these out soon :)
@ramon-prieto the fix is now released in v0.12.2 :)
Thanks for the quick fix! I'm loving working with mirascope, it's so intuitive and bloat-free compared to other frameworks
I'm glad you're liking the library! Thank you for giving us a try :)
If you ever find any other bugs or have any feature requests, post an issue and we'll get on it asap!
so what's the best practice for prompt to avoid this issue? it's still happening (I guess with longer prompts)
@dearkafka is there a minimal example you could share that's still throwing this error?
My recommendation for "best practice" would be to set retries >= 2
. Not ideal in that this will likely result in two calls instead of one, but it will feed the error back into the follow-up call, and so far I've seen it return the proper response on the second call every time.
Sometimes it's also the prompt itself. There's some prompt engineering techniques you can utilize like I mentioned in my previous comment to try and force tool use, but it doesn't always work.
I believe there's a new call param for OpenAI that you can use for forcing tool use, so that might also be worth a try. I think it is setting tool_choice: "required"
in the call params.
somehow even this is not working for me. yeah, I think prompt engineering should force it. thanks
import os
from typing import Type
from fastapi import FastAPI
from mirascope.openai import OpenAIExtractor
from pydantic import BaseModel
app = FastAPI()
class Book(BaseModel):
title: str
author: str
class BookRecommender(OpenAIExtractor[Book]):
extract_schema: Type[Book] = Book
prompt_template = "Please recommend a {genre} book."
genre: str
@app.post("/")
def root(book_recommender: BookRecommender) -> Book:
"""Generates a book based on provided `genre`."""
return book_recommender.extract(model="gpt-4-turbo")
Looks like this is just gpt
being terrible right now for some reason. I just checked the output for No tool
using logfire
and it was this:
""" Here's a fantasy book recommendation for you:
Title: "The Name of the Wind" Author: Patrick Rothfuss
This book is the first in the "Kingkiller Chronicle" series. It follows the story of Kvothe, a gifted young man who grows up to be the most notorious magician his world has ever known. From his childhood in a traveling troupe of musicians to his years spent as a near-feral orphan in a crime-ridden city, to his daringly brazen yet successful bid to enter a legendary school of magic, Kvothe's life is anything but ordinary. With beautiful prose and rich storytelling, Rothfuss unfolds a compelling narrative that dives deep into the themes of identity, legend, and the nature of truth. """
My suggestion would be to use Anthropic or Groq, they do a much more consistent job of this.
I also tried using tool_choice="required"
except for some reason the stop_reason
is stop
and not tool_calls
:(
I will look into fixing a separate bug issue for this.
yes, tool_choice also fails for me. yep, something happened to gpt-4.
I believe this is due to the addition of tool_choice="required"
and internally enabling more freedom in tool choice if not required (indicating that the prompt needs to force the tool if not through the option, but even that may be iffy).
Working on a fix for tool_choice
in #256 so let's move any further discussion on this there (or a new issue should that fix not resolve something else that comes up).
To Reproduce Steps to reproduce the behavior:
class TaskDetails(BaseModel): due_date: str description: str priority: Literal["low", "medium", "high"]
class TaskExtractor(OpenAIExtractor[TaskDetails]): extract_schema: Type[TaskDetails] = TaskDetails prompt_template = """ Extract the task details from the following task: {task} """ task: str
Expected behavior The extractor should return the TaskDetails model
Stacktrace
Setup mirascope version: 0.12.1 python: 3.12.3