我按照本指南设置自我抹布:

目前我不允许使用 Open AI 模型,因此我一直使用 ChatOllama 模型。我希望开始能够使用“with_structed_output()”函数通过 OllamaFunctions 而不是 ChatOllama 来管道输出。此处演示:

基本上这里是代码:

from langchain_experimental.llms.ollama_functions import OllamaFunctions


from langchain_core.prompts import PromptTemplate
from langchain_core.pydantic_v1 import BaseModel, Field


# Schema for structured response
class Person(BaseModel):
    name: str = Field(description="The person's name", required=True)
    height: float = Field(description="The person's height", required=True)
    hair_color: str = Field(description="The person's hair color")


# Prompt template
prompt = PromptTemplate.from_template(
    """Alex is 5 feet tall. 
Claudia is 1 feet taller than Alex and jumps higher than him. 
Claudia is a brunette and Alex is blonde.

Human: {question}
AI: """
)

# Chain
llm = OllamaFunctions(model="phi3", format="json", temperature=0)
structured_llm = llm.with_structured_output(Person)
chain = prompt | structured_llm

我遇到两个错误,使我陷入了死胡同。第一个是:

ValidationError: 1 validation error for OllamaFunctions
__root__
  langchain_community.chat_models.ollama.ChatOllama() got multiple values for keyword argument 'format' (type=type_error)

所以我
llm = OllamaFunctions(model="phi3", format="json", temperature=0)
改为
llm = OllamaFunctions(model="phi3", temperature=0)

这至少让我进入下一行。然后, with_structed_output(Person) 行失败并出现错误:

File ~/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages/langchain_core/language_models/base.py:208, in BaseLanguageModel.with_structured_output(self, schema, **kwargs)
    204 def with_structured_output(
    205     self, schema: Union[Dict, Type[BaseModel]], **kwargs: Any
    206 ) -> Runnable[LanguageModelInput, Union[Dict, BaseModel]]:
    207     """Implement this if there is a way of steering the model to generate responses that match a given schema."""  # noqa: E501
--> 208     raise NotImplementedError()

NotImplementedError:

我不知道从这里该去哪里。任何事情都会有所帮助。谢谢!


2 个回答
2

我遇到了和你一样的问题。在上检查代码并将其与通过 pip 安装的代码进行比较后,似乎缺少一大块应该支持 .with_structed_output() 的代码。我把上面的代码替换成了git上的代码,看起来效果还不错。我相信一旦他们更新 langchain_experimental 的 pip 包,这个问题就会得到解决。

Hobakjuk 发现问题:llama_functions 的 pip、github、webdoc 版本不同步。这需要临时解决方法,直到更新。

解决方法包括:

  1. 复制代码内容

  2. 创建一个本地ollama_functions.py文件,ctrl+v 将代码粘贴到其中

  3. 在你的Python代码中,然后通过替换导入“修补”的本地库

    from langchain_experimental.llms.ollama_functions import OllamaFunctions


    from ollama_functions import OllamaFunctions

跟踪您的代码