使用langchain调用ollama部署的模型

1
2
3
4
5
6
7
8
from langchain_community.llms import ollama
host = "127.0.0.1"
port = "11434"
URL_ADDRESS = f"http://{host}:{port}"
model = "qwen2.5"
llm = ollama.Ollama(model=model, base_url=URL_ADDRESS, temperature=0)
res = llm.invoke(message)
print(res)

使用requests调用ollama部署的模型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
host = "127.0.0.1"
port = "11434"
url = f"http://{host}:{port}/api/chat"
model = "qwen2.5"
headers = {"Content-Type": "application/json"}
data = {
"model":model,#模型选择
"options": {
"temperature":0.#为0表示不让模型自由发挥,输出结果相对较固定,>0的话,输出的结果会比较放飞自我
},
"stream":False,#流式输出
"messages": [
{
"role": "system",
"content":"你是谁?"
}
]#对话列表
}
response=requests.post(url,json=data,headers=headers,timeout=60)
res=response.json()
print(res)