cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
GenAI Insight Hub
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Trouble Using Foundation Models API with Instructor

Gromduraz
New Contributor II

Hello,

I'm attempting to use the instructor library for validating the output of a foundation model (DBRX in this case). Here's my code:

 

import instructor
from instructor.client import Instructor
from pydantic import BaseModel
from openai import OpenAI

class User(BaseModel):
    name: str
    age: int

client = instructor.from_openai(
    OpenAI(
        api_key=mykey,
        base_url=myendpoint,
    )
)


chat_completion = client.chat.completions.create(
        messages=[
            {
                "role": "system",
                "content": "You are an AI assistant",
            },
            {"role": "user", "content": "Create a user"},
        ],
        model="databricks-dbrx-instruct",
        response_model=User,
        max_tokens=4000,
        temperature=0.8,
        tool_choice="auto",
    )

print(chat_completion.choices[0].message.content)

 

When I run this I get:

 

BadRequestError: Error code: 400 - {'error_code': 'BAD_REQUEST', 'message': 'Bad request: json: unknown field "tool_choice"\n'}

 

I assume the conflict here is that the instructor library was designed to send it's requests directly to OpenAI's servers, while we're using the Databricks Foundation Models API and it's passing along the "tool_choice" field which the endpoint isn't expecting? 

Am I missing something obvious? Can I get this to run as-is or would this require a change to the functionality of the Foundation Models API to run? Are there alternatives to Instructor that I should try? Thanks!

2 REPLIES 2

Kaniz
Community Manager
Community Manager

Hi @GromdurazIt appears that youโ€™re encountering an issue with the Instructor library while trying to validate the output of a foundation model (specifically, the DBRX model).

  • The error message you received indicates that the field โ€œtool_choiceโ€ is unknown in the context of the Databricks Foundation Models API.
  • This suggests that the tool_choice parameter is not recognized by the Databricks API, leading to the BadRequestError.
  • Remove the โ€œtool_choiceโ€ Parameter:
    • Since the Databricks Foundation Models API does not expect the โ€œtool_choiceโ€ field, try removing it from your request.
  • Check Databricks API Documentation:
    • Review the Databricks Foundation Models API documentation to ensure that youโ€™re using the correct parameters and options.
    • Verify whether there are any specific requirements or limitations related to the model youโ€™re using.
  • Alternative Libraries:
  •  I hope this helps you troubleshoot the issue, and feel free to explore alternative options if needed...

Gromduraz
New Contributor II

Reviewing my posted code I should mention that the 'tool_choice' argument I'm passing is a default argument of the OpenAI client object. The code still breaks in the same way if I remove that argument from the function call. I think this is out of my hands to fix as it would require either a change to what instructor sends during the payload to DBX or how DBX interprets the payload. 

I did however find a workaround using LangChain's PydanticValidator. That seemed to work although it's not as direct of an implementation as instructor. 

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.