Как предоставить дополнительную информацию о значениях перечисления LLM в LangChain ⇐ Python

Программы на Python
Anonymous
Как предоставить дополнительную информацию о значениях перечисления LLM в LangChain

Сообщение Anonymous »

Я пытаюсь извлечь структурированную информацию с помощью LLM, скажем, GPT-4, используя LangChain в Python. Моя цель — классифицировать компании, связывая их с помощью тегов.
Мой выходной класс имеет тип:

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

from langchain_core.pydantic_v1 import BaseModel

class Company(BaseModel):
industry: list[Industry]
customer: list[Customer]
Пока все хорошо. Проблема в том, что некоторые теги могут быть несколько конкретными, и я хотел бы передать LLM дополнительную информацию, чтобы помочь ей сделать выбор между вариантами. Используя Enum из aenum, как описано здесь, я могу добавить, например. строки документации для значений перечисления:

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

from aenum import Enum

class Industry(Enum):
_init_ = 'value __doc__'
it = "Information Technology", "All kinds of computer stuff"
agriculture = "Agriculture", "Farming, irrigation, fertilizers etc."

class Customer(Enum):
_init_ = 'value __doc__'
B2C = "B2C", "Companies selling directly to consumers"
B2B = "B2B", "Companies selling to other businesses"
Теперь у меня есть значения и несколько полезных объяснений, однако нет прямого способа передать их в LLM.
Если я использую файл . with_structured_output() или PydanticOutputParser им не удается передать строки документации от членов перечисления:

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

from langchain_core.output_parsers import PydanticOutputParser

parser = PydanticOutputParser(pydantic_object=Company)

parser.get_format_instructions()
# 'The output should be formatted as a JSON instance that conforms to the JSON schema below.
# As an example, for the schema {"properties": {"foo": {"title": "Foo", "description": "a list of strings", "type": "array", "items": {"type": "string"}}}, "required": ["foo"]}
# the object {"foo": ["bar", "baz"]} is a well-formatted instance of the schema. The object {"properties": {"foo": ["bar", "baz"]}} is not well-formatted.
# Here is the output schema:
# ```
# {"properties": {"industry": {"type": "array", "items": {"$ref": "#/definitions/Industry"}}, "customer": {"type": "array", "items": {"$ref": "#/definitions/Customer"}}}, "required": ["industry", "customer"], "definitions": {"Industry": {"title": "Industry", "description": "An enumeration.", "enum": ["Information Technology", "Agriculture"]}, "Customer": {"title": "Customer", "description": "An enumeration.", "enum": ["B2C", "B2B"]}}}
#```'
В качестве обходного пути я, конечно, могу написать специальную подсказку, в которой будут подробно описаны строки документации, но мне просто было любопытно, нашел ли кто-нибудь более простой способ сделать это.

Подробнее здесь: https://stackoverflow.com/questions/785 ... -langchain

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