使用本地模型的代理
如果你对使用OpenAI或其他远程模型感到满意,可以跳过这一部分,但许多人对使用自己运行的模型感兴趣。最简单的方法是通过我们的朋友在Ollama的出色工作,他们提供了一个易于使用的客户端,可以为你下载、安装并运行越来越多的模型。
安装Ollama
他们在主页上为Mac、Linux和Windows提供了一键安装程序。
选择并运行一个模型
由于我们将进行代理工作,我们需要一个非常强大的模型,但最大的模型在笔记本电脑上很难运行。我们认为mixtral 8x7b是一个在性能和资源之间的良好平衡,但llama3也是一个很好的选择。你可以通过运行以下命令来运行Mixtral:
ollama run mixtral:8x7b
第一次运行时,它还会自动为你下载和安装模型,这可能需要一些时间。
切换到本地代理
要切换到Mixtral,你需要引入Ollama集成:
pip install llama-index-llms-ollama
然后修改你的依赖项以引入Ollama而不是OpenAI:
from llama_index.llms.ollama import Ollama
最后,将Mixtral初始化为你的LLM:
llm = Ollama(model="mixtral:8x7b", request_timeout=120.0)
再次提问
response = agent.chat("What is 20+(2*4)? Calculate step by step.")
输出与OpenAI的输出不同(第一次尝试时会出错),但Mixtral最终得到了正确的答案:
Thought: The current language of the user is: English. The user wants to calculate the value of 20+(2*4). I need to break down this task into subtasks and use appropriate tools to solve each subtask.
Action: multiply
Action Input: {'a': 2, 'b': 4}
Observation: 8
Thought: The user has calculated the multiplication part of the expression, which is (2*4), and got 8 as a result. Now I need to add this value to 20 by using the 'add' tool.
Action: add
Action Input: {'a': 20, 'b': 8}
Observation: 28
Thought: The user has calculated the sum of 20+(2*4) and got 28 as a result. Now I can answer without using any more tools.
Answer: The solution to the expression 20+(2*4) is 28.
The solution to the expression 20+(2*4) is 28.
查看仓库以了解这个最终代码的样子。
如果你愿意,现在可以继续使用本地模型进行教程的其余部分。我们将继续使用OpenAI,因为我们接下来将为你介绍如何为代理添加RAG(检索增强生成)。