Yarik
Yarik

Reputation: 11

Open-ai pydantic ModuleNotFoundError

Trying to use open ai 1.43 library in termux through pydroid3 but getting moduleNotFound error

Since I cannot install openai directly in pydroid3 since it needs a rust to be installed.i decided to copy the sitepackages of termux to a shared storage so it can be accessed by pydroid3.i imported the open ai library but encouraged this error.(While the module is there)

Here is my tracebackcall Traceback (most recent call last): File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in start(fakepyfile,mainpyfile) File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, File "/sdcard/openai/pydantic_core/init.py", line 6, in from ._pydantic_core import ( ModuleNotFoundError: No module named 'pydantic_core._pydantic_core'

[Program finished]

Upvotes: 1

Views: 137

Answers (1)

Install pydantic then provided structured output xml as the class attributes as an input to openai then convert the xml to json then the json to a dictionary and extract out the dictionary values

install pip install pydantic

from pydantic import BaseModel

import xmltodict

class RestaurantResponse(BaseModel):
    city: str
    restaurantName: str
    address: str

def get_restaurant(city: str) -> RestaurantResponse:
    client = OpenAI(api_key=key)
    response =  client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[
            {"role": "user", "content": f"Return only the name and address of the best restaurant in {city}? <context><output><restaurantName></restaurantName><address></address></output></context>"}
        ]
    )
    
    xml_data = response.choices[0].message.content
    #print(xml_data)
    data_dict = xmltodict.parse(xml_data)
    # Convert dictionary to JSON
    json_data = json.dumps(data_dict)
    
    data_dict = json.loads(json_data)

    print(json_data)
    restaurantName = data_dict["context"]["output"]["restaurantName"]
    address = data_dict["context"]["output"]["address"]
    
    restaurant_info = RestaurantResponse(city=city, restaurantName=restaurantName, address=address)
    
    return restaurant_info

weather = get_restaurant("London")
print(weather.json())

Upvotes: 0

Related Questions