admin管理员组文章数量:1037775
iOS开发之如何开发一款Ollama客户端
介绍
- Ollama 是一个开源的本地 AI 模型运行平台,能够在用户的电脑上下载、安装与运行大型语言模型(LLMs)。
- macOS/iOS 可以调用 Ollama 的客户端很多,如 Cherry Studio、Chatbox、ChatX、Enchanted、NextChat 等。如果想要自己动手开发一款原生 App,可以使用 ollama-swift ,它是一个 Swift 编写的开源库,可以与 Ollama API 进行交互,功能包括管理模型、嵌入、生成、聊天、工具等。
开发步骤
- 下载并安装 Ollama。
- 通过 Ollama 命令下载并运行大模型。
ollama run 模型名
- 设置 Ollama 可以对外提供服务。
launchctl setenv OLLAMA_HOST "0.0.0.0"
launchctl setenv OLLAMA_ORIGINS "*"
- 创建 macOS/iOS 项目。
- 添加 ollama-swift。
- 功能开发。
案例
以生成与聊天功能为例。
代码语言:javascript代码运行次数:0运行复制import Ollama
import SwiftUI
struct ContentView: View {
@State private var userInput: String = "什么是SwiftUI?"
@State private var response: String = ""
@State private var isLoading: Bool = false
// 默认http://localhost:11434
let client = Client(host: URL(string: "http://192.168.0.43:11434")!, userAgent: "MyApp/1.0")
@State private var messages: [Chat.Message] = [.system("你是一个得力的全能助手,可以帮我完成很多任务。")]
var body: some View {
VStack(spacing: 16) {
// 顶部输入区域
HStack {
TextField("输入提示词...", text: $userInput)
.textFieldStyle(.roundedBorder)
.font(.system(size: 16))
Button {
response = ""
Task {
do {
// response = try await generate()
// response = try await chatSingleAround()
response = try await chatMultipleAround()
} catch {
debugPrint(error)
}
}
} label: {
Text("开始")
.foregroundStyle(.white)
.padding(.horizontal, 16)
.padding(.vertical, 8)
.background(userInput.isEmpty ? Color.gray : Color.blue)
.cornerRadius(8)
}
.buttonStyle(.borderless)
.disabled(userInput.isEmpty || isLoading)
}
.padding(.horizontal)
.padding(.top)
// 分隔线
Rectangle()
.fill(Color.gray.opacity(0.2))
.frame(height: 1)
// 响应展示区域
if response != "" {
ResponseBubble(text: response)
}
Spacer()
}
if isLoading {
ProgressView()
.progressViewStyle(.circular)
.padding()
}
}
// 生成
func generate() async throws -> String {
isLoading = true
let response = try await client.generate(
model: "qwen:0.5b", // 模型
prompt: userInput,
stream: false
)
isLoading = false
return response.response
}
// 单轮聊天
func chatSingleAround() async throws -> String {
isLoading = true
let response = try await client.chat(
model: "qwen:0.5b",
messages: [
.system("你是一个得力的全能助手,可以帮我完成很多任务。"),
.user(userInput)
]
)
isLoading = false
return response.message.content
}
// 多轮聊天
func chatMultipleAround() async throws -> String {
var response = ""
isLoading = true
// 1
messages.append(.user("什么是SwiftUI?"))
response.append("1.什么是SwiftUI?\n")
let response1 = try await client.chat(
model: "qwen:0.5b",
messages: messages
)
response.append(response1.message.content)
// 2
messages.append(.user("SwiftUI和UIKit有什么区别?"))
response.append("\n\n2.SwiftUI和UIKit有什么区别?\n")
let response2 = try await client.chat(
model: "qwen:0.5b",
messages: messages
)
response.append(response2.message.content)
isLoading = false
return response
}
}
struct ResponseBubble: View {
let text: String
var body: some View {
ScrollView {
VStack(alignment: .leading, spacing: 8) {
Text("AI")
.font(.system(size: 16))
.foregroundColor(.gray)
Text(text)
.font(.system(size: 16))
.lineSpacing(4)
.padding()
.background(Color.blue.opacity(0.1))
.cornerRadius(12)
}
}
.padding(.horizontal)
}
}
效果
- 生成。
- 单轮聊天。
- 多轮聊天。
iOS开发之如何开发一款Ollama客户端
介绍
- Ollama 是一个开源的本地 AI 模型运行平台,能够在用户的电脑上下载、安装与运行大型语言模型(LLMs)。
- macOS/iOS 可以调用 Ollama 的客户端很多,如 Cherry Studio、Chatbox、ChatX、Enchanted、NextChat 等。如果想要自己动手开发一款原生 App,可以使用 ollama-swift ,它是一个 Swift 编写的开源库,可以与 Ollama API 进行交互,功能包括管理模型、嵌入、生成、聊天、工具等。
开发步骤
- 下载并安装 Ollama。
- 通过 Ollama 命令下载并运行大模型。
ollama run 模型名
- 设置 Ollama 可以对外提供服务。
launchctl setenv OLLAMA_HOST "0.0.0.0"
launchctl setenv OLLAMA_ORIGINS "*"
- 创建 macOS/iOS 项目。
- 添加 ollama-swift。
- 功能开发。
案例
以生成与聊天功能为例。
代码语言:javascript代码运行次数:0运行复制import Ollama
import SwiftUI
struct ContentView: View {
@State private var userInput: String = "什么是SwiftUI?"
@State private var response: String = ""
@State private var isLoading: Bool = false
// 默认http://localhost:11434
let client = Client(host: URL(string: "http://192.168.0.43:11434")!, userAgent: "MyApp/1.0")
@State private var messages: [Chat.Message] = [.system("你是一个得力的全能助手,可以帮我完成很多任务。")]
var body: some View {
VStack(spacing: 16) {
// 顶部输入区域
HStack {
TextField("输入提示词...", text: $userInput)
.textFieldStyle(.roundedBorder)
.font(.system(size: 16))
Button {
response = ""
Task {
do {
// response = try await generate()
// response = try await chatSingleAround()
response = try await chatMultipleAround()
} catch {
debugPrint(error)
}
}
} label: {
Text("开始")
.foregroundStyle(.white)
.padding(.horizontal, 16)
.padding(.vertical, 8)
.background(userInput.isEmpty ? Color.gray : Color.blue)
.cornerRadius(8)
}
.buttonStyle(.borderless)
.disabled(userInput.isEmpty || isLoading)
}
.padding(.horizontal)
.padding(.top)
// 分隔线
Rectangle()
.fill(Color.gray.opacity(0.2))
.frame(height: 1)
// 响应展示区域
if response != "" {
ResponseBubble(text: response)
}
Spacer()
}
if isLoading {
ProgressView()
.progressViewStyle(.circular)
.padding()
}
}
// 生成
func generate() async throws -> String {
isLoading = true
let response = try await client.generate(
model: "qwen:0.5b", // 模型
prompt: userInput,
stream: false
)
isLoading = false
return response.response
}
// 单轮聊天
func chatSingleAround() async throws -> String {
isLoading = true
let response = try await client.chat(
model: "qwen:0.5b",
messages: [
.system("你是一个得力的全能助手,可以帮我完成很多任务。"),
.user(userInput)
]
)
isLoading = false
return response.message.content
}
// 多轮聊天
func chatMultipleAround() async throws -> String {
var response = ""
isLoading = true
// 1
messages.append(.user("什么是SwiftUI?"))
response.append("1.什么是SwiftUI?\n")
let response1 = try await client.chat(
model: "qwen:0.5b",
messages: messages
)
response.append(response1.message.content)
// 2
messages.append(.user("SwiftUI和UIKit有什么区别?"))
response.append("\n\n2.SwiftUI和UIKit有什么区别?\n")
let response2 = try await client.chat(
model: "qwen:0.5b",
messages: messages
)
response.append(response2.message.content)
isLoading = false
return response
}
}
struct ResponseBubble: View {
let text: String
var body: some View {
ScrollView {
VStack(alignment: .leading, spacing: 8) {
Text("AI")
.font(.system(size: 16))
.foregroundColor(.gray)
Text(text)
.font(.system(size: 16))
.lineSpacing(4)
.padding()
.background(Color.blue.opacity(0.1))
.cornerRadius(12)
}
}
.padding(.horizontal)
}
}
效果
- 生成。
- 单轮聊天。
- 多轮聊天。
本文标签: iOS开发之如何开发一款Ollama客户端
版权声明:本文标题:iOS开发之如何开发一款Ollama客户端 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/jiaocheng/1748231641a2272803.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论