尝试创建 langchain 代理时,我收到超出范围的列表索引

尝试创建 langchain 代理时,我收到超出范围的列表索引

我正在尝试创建一个简单的 langchain 代理,但结果却令人沮丧。文档不是很有帮助,谷歌搜索也没有找到太多有用的东西。

这是源代码

import os
# unit_test_agent.py
from langchain.agents import AgentExecutor
from langchain.memory import ConversationBufferMemory
from langchain.prompts import PromptTemplate
from langchain.llms import OpenAI
from langchain_core.prompt_values import PromptValue,StringPromptValue



class UnitTestAnalysisAgent:
    def __init__(self, openai_api_key = None):

        if openai_api_key == None:
            self.openai_api_key = os.environ.get('OPENAI_API_KEY')
        self.llm = OpenAI(temperature=0, openai_api_key=self.openai_api_key)

        system_prompt = """
You are a unit test analysis assistant. Your role is to analyze provided unit test results and provide insights, suggestions, and feedback on t
he quality of the code and tests. Be thorough and provide detailed analysis and recommendations.
"""
        prompt = PromptTemplate(
            template=system_prompt + "\n\nInput: {input}",
            input_variables=["input"]
        )
        self.agent = AgentExecutor.from_agent_and_tools(
            agent=self.llm,
            tools=[],
            memory=ConversationBufferMemory(),
            agent_prompt=prompt,
            input_keys=["input"]
        )    )

    def run(self, unit_test_results):
        # Prepare the input for the agent
        unit_test_analysis_prompt = "Analyze the following unit test results:\n\n" + unit_test_results
        prompt_value = StringPromptValue(text = unit_test_analysis_prompt)


        # Call the agent and get the analysis
        unit_test_analysis = self.agent.invoke(prompt_value)

        return unit_test_analysis

我从 langchain 获得的相关错误信息是:

File "/src/repos/aiops_demo/src/app/agents/unit_test_agent.py", line 40, in run
    unit_test_analysis = self.agent.invoke(prompt_value)
  File "/src/repos/aiops_demo/.venv/lib/python3.8/site-packages/langchain/chains/base.py", line 133, in invoke
    inputs = self.prep_inputs(input)
  File "/src/repos/aiops_demo/.venv/lib/python3.8/site-packages/langchain/chains/base.py", line 479, in prep_inputs
    inputs = {list(_input_keys)[0]: inputs}
IndexError: list index out of range

我也一直在查看 langchain 的源代码,但进展不大。我真的被困在这里,不知道如何继续修复这个问题。我查看了 langchain API 文档,但它们没有什么帮助。有人能推荐一个更好的信息来源来构建 langchain 应用程序吗?我在 Ubuntu 20.04 服务器上运行它,langchain 版本为 0.1.12

任何帮助均感激不尽。

相关内容