Skip to content

Commit c97a887

Browse files
committed
Merge branch 'main' into feature/make-Combine-optional
2 parents 7571adc + fcf8b3a commit c97a887

18 files changed

+27
-617
lines changed

.github/workflows/deploy-docs.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ jobs:
3131
- name: Build docs
3232
run: swift package --allow-writing-to-directory ./docs generate-documentation --target OllamaKit --disable-indexing --output-path ./docs --transform-for-static-hosting --hosting-base-path OllamaKit
3333

34-
- uses: actions/upload-pages-artifact@v2
34+
- uses: actions/upload-pages-artifact@v3
3535
with:
3636
path: "docs"
3737

38-
- uses: actions/deploy-pages@v2
38+
- uses: actions/deploy-pages@v4
3939
id: deployment

Playground/OKPlayground/Views/ChatView.swift

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// Created by Kevin Hermawan on 09/06/24.
66
//
77

8-
import Combine
98
import SwiftUI
109
import OllamaKit
1110

@@ -16,7 +15,6 @@ struct ChatView: View {
1615
@State private var temperature: Double = 0.5
1716
@State private var prompt = ""
1817
@State private var response = ""
19-
@State private var cancellables = Set<AnyCancellable>()
2018

2119
var body: some View {
2220
NavigationStack {
@@ -46,7 +44,6 @@ struct ChatView: View {
4644

4745
Section {
4846
Button("Chat Async", action: actionAsync)
49-
Button("Chat Combine", action: actionCombine)
5047
}
5148

5249
Section("Response") {
@@ -75,26 +72,4 @@ struct ChatView: View {
7572
}
7673
}
7774
}
78-
79-
func actionCombine() {
80-
self.response = ""
81-
82-
guard let model = model else { return }
83-
let messages = [OKChatRequestData.Message(role: .user, content: prompt)]
84-
var data = OKChatRequestData(model: model, messages: messages)
85-
data.options = OKCompletionOptions(temperature: temperature)
86-
87-
viewModel.ollamaKit.chat(data: data)
88-
.sink { completion in
89-
switch completion {
90-
case .finished:
91-
print("Finished")
92-
case .failure(let error):
93-
print("Error:", error.localizedDescription)
94-
}
95-
} receiveValue: { value in
96-
self.response += value.message?.content ?? ""
97-
}
98-
.store(in: &cancellables)
99-
}
10075
}

Playground/OKPlayground/Views/ChatWithFormatView.swift

Lines changed: 17 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,26 @@
55
// Created by Michel-Andre Chirita on 30/12/2024.
66
//
77

8-
import Combine
98
import OllamaKit
109
import SwiftUI
1110

1211
struct ChatWithFormatView: View {
13-
12+
1413
enum ViewState {
1514
case idle
1615
case loading
1716
case error(String)
1817
}
19-
18+
2019
@Environment(ViewModel.self) private var viewModel
21-
20+
2221
@State private var model: String? = nil
2322
/// TIP: be sure to include "return as JSON" in your prompt
2423
@State private var prompt = "Lists of the 10 biggest countries in the world with their iso code as id, name and capital, return as JSON"
25-
@State private var cancellables = Set<AnyCancellable>()
2624
@State private var viewState: ViewState = .idle
27-
25+
2826
@State private var responseItems: [ResponseItem] = []
29-
27+
3028
var body: some View {
3129
NavigationStack {
3230
Form {
@@ -37,29 +35,28 @@ struct ChatWithFormatView: View {
3735
.tag(model as String?)
3836
}
3937
}
40-
38+
4139
TextField("Prompt", text: $prompt, axis: .vertical)
4240
.lineLimit(5)
4341
}
44-
42+
4543
Section {
4644
Button("Chat Async", action: actionAsync)
47-
Button("Chat Combine", action: actionCombine)
4845
}
49-
46+
5047
switch viewState {
5148
case .idle:
5249
EmptyView()
53-
50+
5451
case .loading:
5552
ProgressView()
5653
.id(UUID())
57-
54+
5855
case .error(let error):
5956
Text(error)
6057
.foregroundStyle(.red)
6158
}
62-
59+
6360
Section("Response") {
6461
ForEach(responseItems) { item in
6562
Text("Country: " + item.country + ", capital: " + item.capital)
@@ -73,16 +70,16 @@ struct ChatWithFormatView: View {
7370
}
7471
}
7572
}
76-
73+
7774
func actionAsync() {
7875
clearResponse()
79-
76+
8077
guard let model = model else { return }
8178
let messages = [OKChatRequestData.Message(role: .user, content: prompt)]
8279
var data = OKChatRequestData(model: model, messages: messages, format: getFormat())
8380
data.options = OKCompletionOptions(temperature: 0) /// TIP: better results with temperature = 0
8481
self.viewState = .loading
85-
82+
8683
Task {
8784
do {
8885
var message: String = ""
@@ -101,38 +98,7 @@ struct ChatWithFormatView: View {
10198
}
10299
}
103100
}
104-
105-
func actionCombine() {
106-
clearResponse()
107-
108-
guard let model = model else { return }
109-
let messages = [OKChatRequestData.Message(role: .user, content: prompt)]
110-
var data = OKChatRequestData(model: model, messages: messages, format: getFormat())
111-
data.options = OKCompletionOptions(temperature: 0) /// TIP: better results with temperature = 0
112-
self.viewState = .loading
113-
114-
var message: String = ""
115-
viewModel.ollamaKit.chat(data: data)
116-
.compactMap { $0.message?.content }
117-
.scan("", { result, nextChunk in
118-
result + nextChunk
119-
})
120-
.sink { completion in
121-
switch completion {
122-
case .finished:
123-
print("Finished")
124-
decodeResponse(message)
125-
self.viewState = .idle
126-
case .failure(let error):
127-
print("Error:", error.localizedDescription)
128-
self.viewState = .error(error.localizedDescription)
129-
}
130-
} receiveValue: { value in
131-
message = value
132-
}
133-
.store(in: &cancellables)
134-
}
135-
101+
136102
private func getFormat() -> OKJSONValue {
137103
return
138104
.object(["type": .string("array"),
@@ -147,7 +113,7 @@ struct ChatWithFormatView: View {
147113
])
148114
])
149115
}
150-
116+
151117
private func decodeResponse(_ content: String) {
152118
do {
153119
guard let data = content.data(using: .utf8) else { return }
@@ -158,7 +124,7 @@ struct ChatWithFormatView: View {
158124
self.viewState = .error(error.localizedDescription)
159125
}
160126
}
161-
127+
162128
private func clearResponse() {
163129
self.responseItems = []
164130
}

Playground/OKPlayground/Views/ChatWithToolsView.swift

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// Created by Kevin Hermawan on 28/07/24.
66
//
77

8-
import Combine
98
import OllamaKit
109
import SwiftUI
1110

@@ -14,7 +13,6 @@ struct ChatWithToolsView: View {
1413

1514
@State private var model: String? = nil
1615
@State private var prompt = ""
17-
@State private var cancellables = Set<AnyCancellable>()
1816

1917
@State private var toolCalledResponse = ""
2018
@State private var argumentsResponse = ""
@@ -37,7 +35,6 @@ struct ChatWithToolsView: View {
3735

3836
Section {
3937
Button("Chat Async", action: actionAsync)
40-
Button("Chat Combine", action: actionCombine)
4138
}
4239

4340
Section("Response") {
@@ -78,33 +75,6 @@ struct ChatWithToolsView: View {
7875
}
7976
}
8077

81-
func actionCombine() {
82-
clearResponses()
83-
84-
guard let model = model else { return }
85-
let messages = [OKChatRequestData.Message(role: .user, content: prompt)]
86-
let data = OKChatRequestData(model: model, messages: messages, tools: getTools())
87-
88-
viewModel.ollamaKit.chat(data: data)
89-
.sink { completion in
90-
switch completion {
91-
case .finished:
92-
print("Finished")
93-
case .failure(let error):
94-
print("Error:", error.localizedDescription)
95-
}
96-
} receiveValue: { value in
97-
if let toolCalls = value.message?.toolCalls {
98-
for toolCall in toolCalls {
99-
if let function = toolCall.function {
100-
setResponses(function)
101-
}
102-
}
103-
}
104-
}
105-
.store(in: &cancellables)
106-
}
107-
10878
private func getTools() -> [OKJSONValue] {
10979
return [
11080
.object([

Playground/OKPlayground/Views/CopyModelView.swift

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// Created by Kevin Hermawan on 08/06/24.
66
//
77

8-
import Combine
98
import SwiftUI
109
import OllamaKit
1110

@@ -14,7 +13,6 @@ struct CopyModelView: View {
1413

1514
@State private var model: String?
1615
@State private var destination: String = ""
17-
@State private var cancellables = Set<AnyCancellable>()
1816

1917
var body: some View {
2018
NavigationStack {
@@ -33,7 +31,6 @@ struct CopyModelView: View {
3331

3432
Section {
3533
Button("Copy Async", action: actionAsync)
36-
Button("Copy Combine", action: actionCombine)
3734
}
3835
}
3936
.navigationTitle("Copy Model")
@@ -52,22 +49,4 @@ struct CopyModelView: View {
5249
try await viewModel.ollamaKit.copyModel(data: data)
5350
}
5451
}
55-
56-
private func actionCombine() {
57-
guard let model = model else { return }
58-
let data = OKCopyModelRequestData(source: model, destination: destination)
59-
60-
viewModel.ollamaKit.copyModel(data: data)
61-
.sink { completion in
62-
switch completion {
63-
case .finished:
64-
print("Finished")
65-
case .failure(let error):
66-
print("Error:", error.localizedDescription)
67-
}
68-
} receiveValue: { value in
69-
print("Value:", value)
70-
}
71-
.store(in: &cancellables)
72-
}
7352
}

Playground/OKPlayground/Views/DeleteModelView.swift

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
// Created by Kevin Hermawan on 08/06/24.
66
//
77

8-
import Combine
98
import SwiftUI
109
import OllamaKit
1110

1211
struct DeleteModelView: View {
1312
@Environment(ViewModel.self) private var viewModel
1413

1514
@State private var model: String? = nil
16-
@State private var cancellables = Set<AnyCancellable>()
1715

1816
var body: some View {
1917
NavigationStack {
@@ -29,7 +27,6 @@ struct DeleteModelView: View {
2927

3028
Section {
3129
Button("Delete Async", action: actionAsync)
32-
Button("Delete Combine", action: actionCombine)
3330
}
3431
}
3532
.navigationTitle("Delete Model")
@@ -48,26 +45,4 @@ struct DeleteModelView: View {
4845
try await viewModel.ollamaKit.deleteModel(data: data)
4946
}
5047
}
51-
52-
private func actionCombine() {
53-
guard let model = model else { return }
54-
let data = OKDeleteModelRequestData(name: model)
55-
56-
viewModel.ollamaKit.deleteModel(data: data)
57-
.sink { completion in
58-
switch completion {
59-
case .finished:
60-
print("Finished")
61-
case .failure(let error):
62-
print("Error:", error.localizedDescription)
63-
}
64-
} receiveValue: { value in
65-
print("Value:", value)
66-
}
67-
.store(in: &cancellables)
68-
}
69-
}
70-
71-
#Preview {
72-
DeleteModelView()
7348
}

0 commit comments

Comments
 (0)