LangChain Demo | 如何调用stackoverflow并结合ReAct回答代码相关问题

背景

楼主决定提升与LLM交互的质量,之前是直接prompt->answer的范式,现在我希望能用上ReAct策略和能够检索StackOverflow,让同一款LLM发挥出更大的作用。

难点

  1. 怎样调用StackOverflow

step1 pip install stackspi

step 2

python 复制代码
from langchain.agents import load_tools

tools = load_tools(
    ["stackexchange"],
    llm=llm
)

注:stackoverflow是stackexchange的子网站

  1. 交互次数太多token输入超出了llm限制

approach 1 使用ConversationSummaryBufferMemory

这种记忆方式会把之前的对话内容总结一下,限制在设定的token个数内

python 复制代码
from langchain.memory import ConversationSummaryBufferMemory

memory = ConversationSummaryBufferMemory(
    llm = llm, # 这里的llm的作用是总结
    max_token_limit=4097,
    memory_key="chat_history"
)

approach 2 设置参数max_iterations

python 复制代码
agent = ZeroShotAgent(
    llm_chain=llm_chain, 
    tools=tools, 
    max_iterations=4, # 限制最大交互次数,防止token超过上限
    verbose=True
)
  1. llm总是回复无法回答

很多教程把温度设置成0,说是为了得到最准确的答案,但是我发现这样设置,agent会变得特别谨慎,直接说它不知道,温度调高以后问题解决了。

测试问题

What parts does a JUnit4 unit test case consist of?

代码

python 复制代码
from constants import PROXY_URL,KEY

import warnings
warnings.filterwarnings("ignore")

import langchain
langchain.debug = True

from langchain.agents import load_tools
from langchain.chat_models import ChatOpenAI

from langchain.agents import AgentExecutor, ZeroShotAgent
from langchain.chains import LLMChain
from langchain.memory import ConversationSummaryBufferMemory

llm = ChatOpenAI(
    temperature=0.7, # 如果参数调得很低,会导致LLM特别谨慎,最后不给答案
    model_name="gpt-3.5-turbo-0613", 
    openai_api_key=KEY,
    openai_api_base=PROXY_URL
)

memory = ConversationSummaryBufferMemory(
    llm = llm, # 这里的llm的作用是总结
    max_token_limit=4097,
    memory_key="chat_history"
)

prefix = """You should be a proficient and helpful assistant in java unit testing with JUnit4 framework. You have access to the following tools:"""
suffix = """Begin!"

{chat_history}
Question: {input}
{agent_scratchpad}"""

tools = load_tools(
    ["stackexchange"],
    llm=llm
)

prompt = ZeroShotAgent.create_prompt(
    tools,
    prefix=prefix,
    suffix=suffix,
    input_variables=["input", "chat_history", "agent_scratchpad"],
) # 这里集成了ReAct

llm_chain = LLMChain(llm=llm, prompt=prompt)

agent = ZeroShotAgent(
    llm_chain=llm_chain, 
    tools=tools, 
    max_iterations=4, # 限制最大交互次数,防止token超过上限
    verbose=True
)

agent_chain = AgentExecutor.from_agent_and_tools(
    agent=agent, 
    tools=tools, 
    verbose=True, 
    memory=memory
)

def ask_agent(question):
    answer = agent_chain.run(input=question)
    return answer

def main():
    test_question = "What parts does a JUnit4 unit test case consist of?"
    test_answer = ask_agent(test_question)
    return test_answer

if __name__ == "__main__":
    main()

最后输出

chain/end 1:chain:AgentExecutor 75.12s Exiting Chain run with output:

{

"output": "A JUnit4 unit test case consists of the following parts:\n1.

Test class: This is a class that contains the test methods.\n2. Test methods: These are the methods that contain the actual test code. They are annotated with the @Test annotation.\n3. Assertions: These are used to verify

the expected behavior of the code being tested. JUnit provides various assertion methods for this purpose.\n4. Annotations: JUnit provides several annotations that can be used to configure the test case, such as @Before, @After, @BeforeClass, and @AfterClass.\n\nOverall, a JUnit4 unit test case

is a class that contains test methods with assertions, and can be configured using annotations."

}

相关推荐
破烂三角裤9 小时前
AI 没有记忆?我给儿童故事 Agent 搭了一套“长期记忆”系统
langchain
A.说学逗唱的Coke9 小时前
【大模型专题】大模型怎么选(2026 年 10 月版):从 20 个模型里挑对那 3 个,以及 Agent 按角色分模型的实操矩阵
ai agent·claude code·大模型选型·模型路由·多模型编排
云和数据.ChenGuang11 小时前
光哥说Python 异常深度解析:从异常模型到工程化实践
java·前端·人工智能·python·langchain
吴名氏.13 小时前
Java转AI Agent教程(第一周)让模型先跑起来
人工智能·python·ai·ai agent
Maiko Star14 小时前
【HM-LC】LangChain RAG查询优化详解:查询重写、混合检索与重排序实战
langchain·rag
2501_9130613415 小时前
MCP核心能力
langchain
小林ixn16 小时前
从线性 RAG 到 Agentic RAG:用 LangGraph 搭一个会判断、会纠错、会兜底的检索闭环
langchain·llm·agent
yz_aiks16 小时前
chrome-use 使用教程:让 Claude Code 操控已登录的 Chrome(禅道、ONES 实测)
chrome·自动化·大模型·ai agent
kyrie_sakura16 小时前
大模型学习19 大模型学习 - LangChain2_Retrieval(RAG-文档处理-向量处理检索-Milvus)
学习·langchain·milvus
承渊政道18 小时前
【大模型应用开发:基于GPT-4和ChatGPT】(使用LangChain框架和插件增强LLM的功能)
chatgpt·langchain·llm·框架·大模型应用开发