ChatGLM3 icon indicating copy to clipboard operation
ChatGLM3 copied to clipboard

使用langchain官方的LLMChain样例报404错误

Open jjbiji opened this issue 2 years ago • 5 comments

System Info / 系統信息

cuda:V11.8.89 python:3.8.10 transformers:4.36.0

Who can help? / 谁可以帮助到您?

No response

Information / 问题信息

  • [x] The official example scripts / 官方的示例脚本
  • [ ] My own modified scripts / 我自己修改的脚本和任务

Reproduction / 复现过程

  1. 正常启动openai_api_demo
  2. 使用langchain官方使用LLMChain连接ChatGLM样例运行程序,代码与该url中的代码相同:https://python.langchain.com/docs/integrations/llms/chatglm 代码如下:
from langchain.chains import LLMChain
from langchain.llms import ChatGLM
from langchain.prompts import PromptTemplate

# import os

template = """{question}"""
prompt = PromptTemplate(template=template, input_variables=["question"])

# default endpoint_url for a local deployed ChatGLM api server
endpoint_url = "http://127.0.0.1:8000"

# direct access endpoint in a proxied environment
# os.environ['NO_PROXY'] = '127.0.0.1'

llm = ChatGLM(
    endpoint_url=endpoint_url,
    max_token=80000,
    history=[
        ["我将从美国到中国来旅游,出行前希望了解中国的城市", "欢迎问我任何问题。"]
    ],
    top_p=0.9,
    model_kwargs={"sample_model_args": False},
)

# turn on with_history only when you want the LLM object to keep track of the conversation history
# and send the accumulated context to the backend model api, which make it stateful. By default it is stateless.
# llm.with_history = True


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

question = "北京和上海两座城市有什么不同?"

llm_chain.run(question)
  1. 运行报错如下:
Traceback (most recent call last):
  File "test1.py", line 35, in <module>
    llm_chain.run(question)
  File "/root/miniconda3/lib/python3.8/site-packages/langchain/chains/base.py", line 507, in run
    return self(args[0], callbacks=callbacks, tags=tags, metadata=metadata)[
  File "/root/miniconda3/lib/python3.8/site-packages/langchain/chains/base.py", line 312, in __call__
    raise e
  File "/root/miniconda3/lib/python3.8/site-packages/langchain/chains/base.py", line 306, in __call__
    self._call(inputs, run_manager=run_manager)
  File "/root/miniconda3/lib/python3.8/site-packages/langchain/chains/llm.py", line 103, in _call
    response = self.generate([inputs], run_manager=run_manager)
  File "/root/miniconda3/lib/python3.8/site-packages/langchain/chains/llm.py", line 115, in generate
    return self.llm.generate_prompt(
  File "/root/miniconda3/lib/python3.8/site-packages/langchain_core/language_models/llms.py", line 516, in generate_prompt
    return self.generate(prompt_strings, stop=stop, callbacks=callbacks, **kwargs)
  File "/root/miniconda3/lib/python3.8/site-packages/langchain_core/language_models/llms.py", line 666, in generate
    output = self._generate_helper(
  File "/root/miniconda3/lib/python3.8/site-packages/langchain_core/language_models/llms.py", line 553, in _generate_helper
    raise e
  File "/root/miniconda3/lib/python3.8/site-packages/langchain_core/language_models/llms.py", line 540, in _generate_helper
    self._generate(
  File "/root/miniconda3/lib/python3.8/site-packages/langchain_core/language_models/llms.py", line 1069, in _generate
    self._call(prompt, stop=stop, run_manager=run_manager, **kwargs)
  File "/root/miniconda3/lib/python3.8/site-packages/langchain_community/llms/chatglm.py", line 104, in _call
    raise ValueError(f"Failed with response: {response}")
ValueError: Failed with response: <Response [404]>

Expected behavior / 期待表现

正常返回运行结果

jjbiji avatar Dec 25 '23 08:12 jjbiji

endpoint_url = "http://127.0.0.1:8000/v1"

xiaoyaolangzhi avatar Dec 25 '23 10:12 xiaoyaolangzhi

endpoint_url = "http://127.0.0.1:8000/v1"

这样修改也还是不行,错误一样

jjbiji avatar Dec 26 '23 02:12 jjbiji

ChatGLM是啥函数,我记得我们用的是GLM3,而不是langchain官方的

zRzRzRzRzRzRzR avatar Dec 26 '23 07:12 zRzRzRzRzRzRzR

同问,应该怎么在Langchain的这段代码中调用chatglm?

kkckk1110 avatar Dec 26 '23 12:12 kkckk1110

ChatGLM是啥函数,我记得我们用的是GLM3,而不是langchain官方的

那就是LangChain那边的适配没有跟上吧,您可以看下我提供的LangChain官方文档链接,他们的确是怎么写的。

jjbiji avatar Dec 27 '23 06:12 jjbiji

https://python.langchain.com/docs/integrations/llms/chatglm 确实是没有chatglm3的

zRzRzRzRzRzRzR avatar Dec 29 '23 04:12 zRzRzRzRzRzRzR

遇到同样的错误,表示关注。

jsxyhelu avatar Jan 02 '24 23:01 jsxyhelu

俺也是。。如何解决

yoohhwz avatar Jan 03 '24 14:01 yoohhwz

同样问题

gggqyy avatar Jan 05 '24 03:01 gggqyy

GLM3

GLM3的地址请发一下了

jsxyhelu avatar Jan 11 '24 05:01 jsxyhelu

langchian用的是我们自己载入的方法,LLMchain是可以用的呀,404是没有请求到么

zRzRzRzRzRzRzR avatar Jan 11 '24 06:01 zRzRzRzRzRzRzR

鉴于目前langchain调用chatglm的模块有问题,这里对相关内容进行修改,实现一个可用的模块

https://github.com/jsxyhelu/langchain_chatglm3

from langchain_chatglm3 import ChatGLM3
……
llm = ChatGLM3(endpoint_url = "http://192.168.196.211:6006/")
retriever = db.as_retriever()
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
qa = ConversationalRetrievalChain.from_llm(llm, retriever, memory=memory)
response = qa({"question": "大潮分为那几个阶段?"})
answer = response["answer"]
print("RAG回答:", answer)

jsxyhelu avatar Jan 13 '24 07:01 jsxyhelu

鉴于目前langchain调用chatglm的模块有问题,这里对相关内容进行修改,实现一个可用的模块

https://github.com/jsxyhelu/langchain_chatglm3

from langchain_chatglm3 import ChatGLM3
……
llm = ChatGLM3(endpoint_url = "http://192.168.196.211:6006/")
retriever = db.as_retriever()
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
qa = ConversationalRetrievalChain.from_llm(llm, retriever, memory=memory)
response = qa({"question": "大潮分为那几个阶段?"})
answer = response["answer"]
print("RAG回答:", answer)

大佬你好, 请问这段代码中 langchain 版本是哪个, 另外想将ConversationalRetrievalChain.from_llm(llm, ....)方法中的 llm 替换为 ChatGLM4 的 http 请求, 不知道是否可行, 可行的话具体应该怎么改写, 谢谢

EricSUID avatar Jan 31 '24 03:01 EricSUID

鉴于目前langchain调用chatglm的模块有问题,这里对相关内容进行修改,实现一个可用的模块 https://github.com/jsxyhelu/langchain_chatglm3

from langchain_chatglm3 import ChatGLM3
……
llm = ChatGLM3(endpoint_url = "http://192.168.196.211:6006/")
retriever = db.as_retriever()
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
qa = ConversationalRetrievalChain.from_llm(llm, retriever, memory=memory)
response = qa({"question": "大潮分为那几个阶段?"})
answer = response["answer"]
print("RAG回答:", answer)

大佬你好, 请问这段代码中 langchain 版本是哪个, 另外想将ConversationalRetrievalChain.from_llm(llm, ....)方法中的 llm 替换为 ChatGLM4 的 http 请求, 不知道是否可行, 可行的话具体应该怎么改写, 谢谢

langchain应该就是直接版本;然后ChatGLM4的使用,我建议可以看看zhipuai,它应该有相关东西的。我这里就是调用的chatglm3

jsxyhelu avatar Jan 31 '24 03:01 jsxyhelu

看一下源码 在chatglm3中,我的是chatglm36b 里面有个文件是langchain_openai_api.py ,里面就说了作为客户端 endpoint_url = "http://127.0.0.1:8000/v1/chat/completions" 你改成这个就可以了

leizhengtao520 avatar Mar 22 '24 07:03 leizhengtao520