11 个版本
0.2.0 | 2024 年 6 月 13 日 |
---|---|
0.1.9 | 2024 年 4 月 17 日 |
0.1.8 | 2024 年 3 月 30 日 |
0.1.7 | 2024 年 2 月 24 日 |
0.1.2 | 2023 年 11 月 21 日 |
#20 in 机器学习
3,733 每月下载量
用于 14 个 crate (13 个直接使用)
91KB
2K SLoC
Ollama-rs
一个简单易用的库,用于与 Ollama API 交互。
它是根据 Ollama API 文档制作的。
安装
将 ollama-rs 添加到您的 Cargo.toml 文件中
[dependencies]
ollama-rs = "0.2.0"
初始化 Ollama
// By default it will connect to localhost:11434
let ollama = Ollama::default();
// For custom values:
let ollama = Ollama::new("https://127.0.0.1".to_string(), 11434);
使用方法
您可以自由查看 聊天机器人示例,该示例展示了如何使用此库在不到 50 行代码中创建一个简单的聊天机器人。您还可以查看一些 其他示例。
这些示例为了简单起见,错误处理较差,但您应该在代码中正确处理错误。
完成生成
let model = "llama2:latest".to_string();
let prompt = "Why is the sky blue?".to_string();
let res = ollama.generate(GenerationRequest::new(model, prompt)).await;
if let Ok(res) = res {
println!("{}", res.response);
}
输出: 天空之所以呈现蓝色,是因为一种称为瑞利散射的现象...
完成生成(流式传输)
需要 stream
功能。
let model = "llama2:latest".to_string();
let prompt = "Why is the sky blue?".to_string();
let mut stream = ollama.generate_stream(GenerationRequest::new(model, prompt)).await.unwrap();
let mut stdout = tokio::io::stdout();
while let Some(res) = stream.next().await {
let responses = res.unwrap();
for resp in responses {
stdout.write(resp.response.as_bytes()).await.unwrap();
stdout.flush().await.unwrap();
}
}
与上面的输出相同,但以流式传输方式。
完成生成(向模型传递选项)
let model = "llama2:latest".to_string();
let prompt = "Why is the sky blue?".to_string();
let options = GenerationOptions::default()
.temperature(0.2)
.repeat_penalty(1.5)
.top_k(25)
.top_p(0.25);
let res = ollama.generate(GenerationRequest::new(model, prompt).options(options)).await;
if let Ok(res) = res {
println!("{}", res.response);
}
输出: 1. 太阳发出白光:太阳主要由 ...
列出本地模型
let res = ollama.list_local_models().await.unwrap();
返回一个包含 Model
结构体的向量。
显示模型信息
let res = ollama.show_model_info("llama2:latest".to_string()).await.unwrap();
返回一个 ModelInfo
结构体。
创建模型
let res = ollama.create_model(CreateModelRequest::path("model".into(), "/tmp/Modelfile.example".into())).await.unwrap();
返回一个表示模型创建最终状态的 CreateModelStatus
结构体。
创建模型(流式传输)
需要 stream
功能。
let mut res = ollama.create_model_stream(CreateModelRequest::path("model".into(), "/tmp/Modelfile.example".into())).await.unwrap();
while let Some(res) = res.next().await {
let res = res.unwrap();
// Handle the status
}
返回一个 CreateModelStatusStream
,它将流式传输模型创建的每个状态更新。
复制模型
let _ = ollama.copy_model("mario".into(), "mario_copy".into()).await.unwrap();
删除模型
let _ = ollama.delete_model("mario_copy".into()).await.unwrap();
生成嵌入
let prompt = "Why is the sky blue?".to_string();
let res = ollama.generate_embeddings("llama2:latest".to_string(), prompt, None).await.unwrap();
返回一个包含嵌入(浮点数向量)的 GenerateEmbeddingsResponse
结构体。
函数调用
let tools = vec![Arc::new(Scraper::new())];
let parser = Arc::new(NousFunctionCall::new());
let message = ChatMessage::user("What is the current oil price?".to_string());
let res = ollama.send_function_call(
FunctionCallRequest::new(
"adrienbrault/nous-hermes2pro:Q8_0".to_string(),
tools,
vec![message],
),
parser,
).await.unwrap();
使用给定的工具(如搜索网络)来寻找答案,返回一个包含答案的 ChatMessageResponse
。
依赖项
~4–19MB
~287K SLoC