|
ollama介绍在本地启动并运行大型语言模型。运行Llama3、Phi3、Mistral、Gemma和其他型号。Llama3MetaLlama3是MetaInc.开发的一系列最先进的模型,提供8B和70B参数大小(预训练或指令调整)。Llama3指令调整模型针对对话/聊天用例进行了微调和优化,并且在常见基准测试中优于许多可用的开源聊天模型。安装pipinstallollama高性价比GPU资源:https://www.ucloud.cn/site/active/gpu.html?ytag=gpu_wenzhang_tongyong_shemei用法importollamaresponse=ollama.chat(model='llama2',messages=[{'role':'user','content':'Whyistheskyblue?',},])print(response['message']['content'])流式响应可以通过设置stream=True、修改函数调用以返回Python生成器来启用响应流,其中每个部分都是流中的一个对象。importollamastream=ollama.chat(model='llama2',messages=[{'role':'user','content':'Whyistheskyblue?'}],stream=True,)forchunkinstream:print(chunk['message']['content'],end='',flush=True)应用程序编程接口OllamaPython库的API是围绕OllamaRESTAPI设计的聊天ollama.chat(model='llama2', messages=[{'role': 'user', 'content': 'Why is the sky blue?'}])新增ollama.generate(model='llama2', prompt='Why is the sky blue?')列表ollama.list()展示ollama.show('llama2')创建modelfile='''FROMllama2SYSTEMYouaremariofromsupermariobros.'''ollama.create(model='example',modelfile=modelfile)复制ollama.copy('llama2','user/llama2')删除ollama.delete('llama2')Pullollama.pull('llama2')pushollama.push('user/llama2')嵌入ollama.embeddings(model='llama2', prompt='The sky is blue because of rayleigh scattering')定制客户端可以使用以下字段创建自定义客户端:host:要连接的Ollama主机timeout:请求超时时间fromollamaimportClientclient=Client(host='http://localhost:11434')response=client.chat(model='llama2',messages=[{'role':'user','content':'Whyistheskyblue?',},])异步客户端importasynciofromollamaimportAsyncClientasyncdefchat():message={'role':'user','content':'Whyistheskyblue?'}response=awaitAsyncClient().chat(model='llama2',messages=[message])asyncio.run(chat())设置stream=True修改函数以返回Python异步生成器:importasynciofromollamaimportAsyncClientasyncdefchat():message={'role':'user','content':'Whyistheskyblue?'}asyncforpartinawaitAsyncClient().chat(model='llama2',messages=[message],stream=True):print(part['message']['content'],end='',flush=True)asyncio.run(chat())错误如果请求返回错误状态或在流式传输时检测到错误,则会引发错误。model='does-not-yet-exist'tryllama.chat(model)exceptollama.ResponseErrorase:print('Error:',e.error)ife.status_code==404llama.pull(model)
|
|