赞
踩
crewAI中默认使用的gpt4的模型, 在环境中配置OPENAI_API_KEY
即可使用。 但openai的api毕竟是要花钱的, 况且现在对大陆地区做了封禁, 使用起来不是那么方便。 而Ollama可以方便的运行本地的大模型, 既不用花钱, 又可以在本地进行使用。 crewAI中也支持Ollama使用本地模型。
import os ##默认方式,直接使用gpt4, 需要设置OPENAI_API_KEY # os.environ["OPENAI_API_KEY"] = "Your Key" ###通过Ollama使用本地模型的方式1: os.environ["OPENAI_API_BASE"] = 'http://localhost:11434/v1' ## 这里如果填http://localhost:11434 会报404错误 os.environ["OPENAI_MODEL_NAME"] ='llama3:8b' # Adjust based on available model os.environ["OPENAI_API_KEY"] ='NA' # ###################################### # # ###通过Ollama使用本地模型的方式2: # from langchain.llms import Ollama # # llm = Ollama( # model="llama3:8b", # base_url="http://localhost:11434/") ##不给base_url参数也可以。如果给的话这里写http://localhost:11434/v1 会报错,提示langchain_community.llms.ollama.OllamaEndpointNotFoundError: Ollama call failed with status code 404. Maybe your model is not found and you should pull the model with `ollama pull llama3:8b`. # ############################## from crewai import Agent, Task, Crew research_agent = Agent( role='Researcher', goal='Find and summarize the latest AI news', backstory="""You're a researcher at a large company. You're responsible for analyzing data and providing insights to the business.""", verbose=True, # llm=llm ### 注意使用方式2时, 这里需要手动传入llm ) task = Task( description='Find and summarize the latest AI news', expected_output='A bullet list summary of the top 5 most important AI news', agent=research_agent, tools=[] ) crew = Crew( agents=[research_agent], tasks=[task], verbose=2 ) result = crew.kickoff() print(result)
上面的代码给出了2种不同的使用Ollama的方式,
第一种方式是与openai api使用方式兼容的一种方式, 直接设置OPENAI_API_BASE
, OPENAI_MODEL_NAME
和OPENAI_API_KEY
即可。OPENAI_API_BASE
填Ollama服务的本地地址和端口。 注意,此时虽然没用用openai 的api, 但是OPENAI_API_KEY
也是必不可少的, 可以随便给个值。
第二种方式是显式的用Ollama定义模型, 然后在agent中手动传入llm
参数, 注意base_url
的值。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。