arcstep / langchain_zhipuai

为了兼容Langchain,根据zhipu官方HTTP的API重新实现的Python SDK
50 stars 7 forks source link

调用生成结构化数据的时候报了一个 Could not parse function call: 'function_call'的错 #11

Closed liangz168 closed 2 days ago

liangz168 commented 4 days ago
import os

from langchain_core.prompts import PromptTemplate, FewShotPromptTemplate
from langchain_experimental.synthetic_data import create_data_generation_chain
from langchain_experimental.tabular_synthetic_data.openai import create_openai_data_generator
from langchain_experimental.tabular_synthetic_data.prompts import SYNTHETIC_FEW_SHOT_PREFIX, SYNTHETIC_FEW_SHOT_SUFFIX
from langchain_zhipu import ChatZhipuAI
from langchain_core.pydantic_v1 import BaseModel

model = ChatZhipuAI(
    model='GLM-4V'
)

# 1、定义数据模型
class MedicalBilling(BaseModel):
    patient_id: str  # 患者ID,整数类型
    patient_name: str  # 患者姓名,字符串类型
    diagnosis_code: str  # 诊断代码,字符串类型
    procedure_code: str  # 程序代码,字符串类型
    total_charge: str  # 总费用,浮点数类型
    insurance_claim_amount: str  # 保险索赔金额,浮点数类型

# 2、 提供一些样例数据,给AI
examples = [
    {
        "example": "Patient ID: 123456, Patient Name: 张娜, Diagnosis Code: J20.9, Procedure Code: 99203, Total Charge: $500, Insurance Claim Amount: $350"
    },
    {
        "example": "Patient ID: 789012, Patient Name: 王兴鹏, Diagnosis Code: M54.5, Procedure Code: 99213, Total Charge: $150, Insurance Claim Amount: $120"
    },
    {
        "example": "Patient ID: 345678, Patient Name: 刘晓辉, Diagnosis Code: E11.9, Procedure Code: 99214, Total Charge: $300, Insurance Claim Amount: $250"
    },
]

example_prompt = PromptTemplate(input_variables=['example'], template='{example}')
prompt_template = FewShotPromptTemplate(
    prefix=SYNTHETIC_FEW_SHOT_PREFIX,
    suffix=SYNTHETIC_FEW_SHOT_SUFFIX,
    examples=examples,
    example_prompt=example_prompt,
    input_variables=['subject', 'extra']
)
generator = create_openai_data_generator(
    output_schema=MedicalBilling,
    llm=model,
    prompt=prompt_template
)
res2 = generator.generate(
    subject='医疗账单',
    extra='名字可以是随机的,最好使用比较生僻的名字。',
    runs=1  # 生成数据数量,
)
print(f'res2:{res2}')

错误: Traceback (most recent call last): File "/opt/anaconda3/lib/python3.12/site-packages/langchain_core/output_parsers/openai_functions.py", line 43, in parse_result func_call = copy.deepcopy(message.additional_kwargs["function_call"])


KeyError: 'function_call'
arcstep commented 3 days ago

langchain 0.3 版本之后已经升级了对 pydantic2 的支持,与智谱官方已经兼容,所以我就不再多此一举维护这个包了。

您可以直接使用智谱官方的 API,或者使用 OpenAI 兼容的API 访问智谱。

另外,推荐使用我的 illufly 开发框架,比 langchain 更加简洁好用 :)

liangz168 commented 2 days ago

感谢,我试试