Передача Enum в качестве инструмента клиенту: «TypeError: объект типа ModelMetaclass не является сериализуемым JSON»Python

Программы на Python
Anonymous
Передача Enum в качестве инструмента клиенту: «TypeError: объект типа ModelMetaclass не является сериализуемым JSON»

Сообщение Anonymous »

Я пытаюсь вызвать инструмент в langchain.
Функция принимает входные данные IntEnum. Он бросает игральную кость и возвращает случайное целое число.

Код: Выделить всё

import random
from enum import IntEnum

from dotenv import load_dotenv
from langchain.tools import Tool, tool
from langchain_openai import AzureChatOpenAI
from pydantic import BaseModel, Field

load_dotenv()

class Dice(IntEnum):
"""
Roll a D&D dice. A d4 dice has 4 sides and thus
rolling a d4 dice will return a value from 1 through 4.
"""

d4 = 4
d6 = 6
d8 = 8
d10 = 10
d12 = 12
d20 = 20
d100 = 100

def roll_dice(dice: Dice) -> int:
"""
Simulates rolling a dice with a specified number of sides.

Parameters:
dice (Dice): A dice to roll.

Returns:
int: The result of the dice roll.
"""
return random.randint(1, dice.value)

class RollDiceInput(BaseModel):
dice: Dice

def get_function_description(func):
"""Extract docstrings from the functions"""
return func.__doc__.strip()

# Define the tool with the input schema
roll_dice_tool = Tool(
name="roll_dice",
description=get_function_description(roll_dice),
args_schema=RollDiceInput,
func=roll_dice,
)

# Define the prompt
prompt = """
Roll a dice in Dungeons and Dragons.
"""

client = AzureChatOpenAI()

# Call the GPT-4 API with descriptions extracted from docstrings
response = client.invoke(
model="gpt-4o",
input=prompt,
tools=[roll_dice_tool],
tool_choice={
"type": "function",
"function": {"name": "roll_dice"},
},  # forces the model to call the `roll_dice` function
)
Выдает ошибку:

Код: Выделить всё

TypeError: Object of type ModelMetaclass is not JSON serializable
.
Кажется, не удалось сериализовать функцию.
Как мне правильно передать и определить инструмент дляroll_dice code> функция?
langchain 0.3.0
langchain-openai 0.2.0

Подробнее здесь: https://stackoverflow.com/questions/790 ... lmetaclass

Вернуться в «Python»