编写取名程序实践LLM
经过环境配置,我们跑通了和LLM的链接,有了这些基础的设置,我们可以尝试使用LLM进行一次编程操作。比如,为一个生产彩色袜子的公司取名的程序:
from langchain_community.chat_models import ChatTongyi
from langchain_community.llms import Tongyi
llm = Tongyi()
chatllm = ChatTongyi()
text = "What would be a good company name for a company that makes colorful socks?"
# llm.predict(text)
# Use invoke() instead of predict()
result = llm.invoke(text)
print(result)
chatResult = chatllm.predict(text)
print(chatResult)
运行结果:

由于predict在新版langchain里不支持,所以将predict替换为invoke()即可
替换后代码:
from langchain_community.chat_models import ChatTongyi
from langchain_community.llms import Tongyi
llm = Tongyi()
chatllm = ChatTongyi()
text = "What would be a good company name for a company that makes colorful socks?"
# llm.predict(text)
# Use invoke() instead of predict()
result = llm.invoke(text)
print(result)
chatResult = chatllm.invoke(text)
print(chatResult.content)
注意,chatResult接受的是mesaage类型,print打印其中的content即为我们所获取的回答:


1073

被折叠的 条评论
为什么被折叠?



