-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
956f630
commit 2d17673
Showing
18 changed files
with
1,125 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
7 changes: 7 additions & 0 deletions
7
Playground/OKPlayground.xcodeproj/project.xcworkspace/contents.xcworkspacedata
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
Playground/OKPlayground.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>IDEDidComputeMac32BitWarning</key> | ||
<true/> | ||
</dict> | ||
</plist> |
23 changes: 23 additions & 0 deletions
23
Playground/OKPlayground.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"pins" : [ | ||
{ | ||
"identity" : "swift-docc-plugin", | ||
"kind" : "remoteSourceControl", | ||
"location" : "https://github.com/apple/swift-docc-plugin.git", | ||
"state" : { | ||
"revision" : "26ac5758409154cc448d7ab82389c520fa8a8247", | ||
"version" : "1.3.0" | ||
} | ||
}, | ||
{ | ||
"identity" : "swift-docc-symbolkit", | ||
"kind" : "remoteSourceControl", | ||
"location" : "https://github.com/apple/swift-docc-symbolkit", | ||
"state" : { | ||
"revision" : "b45d1f2ed151d057b54504d653e0da5552844e34", | ||
"version" : "1.0.0" | ||
} | ||
} | ||
], | ||
"version" : 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// OKPlaygroundApp.swift | ||
// OKPlayground | ||
// | ||
// Created by Kevin Hermawan on 08/06/24. | ||
// | ||
|
||
import SwiftUI | ||
|
||
@main | ||
struct OKPlaygroundApp: App { | ||
@State private var viewModel = ViewModel() | ||
|
||
var body: some Scene { | ||
WindowGroup { | ||
AppView() | ||
.environment(viewModel) | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
Playground/OKPlayground/Preview Content/Preview Assets.xcassets/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Playground/OKPlayground/Resources/Assets.xcassets/AccentColor.colorset/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"colors" : [ | ||
{ | ||
"idiom" : "universal" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
Playground/OKPlayground/Resources/Assets.xcassets/AppIcon.appiconset/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"idiom" : "universal", | ||
"platform" : "ios", | ||
"size" : "1024x1024" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
Playground/OKPlayground/Resources/Assets.xcassets/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// ViewModel.swift | ||
// OKPlayground | ||
// | ||
// Created by Kevin Hermawan on 09/06/24. | ||
// | ||
|
||
import Foundation | ||
import OllamaKit | ||
|
||
@Observable | ||
final class ViewModel { | ||
var ollamaKit = OllamaKit() | ||
|
||
var isReachable = false | ||
var models = [String]() | ||
|
||
func reachable() async { | ||
self.isReachable = await ollamaKit.reachable() | ||
} | ||
|
||
func fetchModels() async { | ||
let response = try? await ollamaKit.models() | ||
guard let models = response?.models.map({ $0.name }) else { return } | ||
|
||
self.models = models | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// | ||
// AppView.swift | ||
// OKPlayground | ||
// | ||
// Created by Kevin Hermawan on 08/06/24. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct AppView: View { | ||
@Environment(ViewModel.self) private var viewModel | ||
|
||
var body: some View { | ||
NavigationStack { | ||
Form { | ||
Section { | ||
Text("Reachable") | ||
.badge(viewModel.isReachable ? "Yes" : "No") | ||
} | ||
|
||
Section("Models") { | ||
NavigationLink("Model List") { | ||
ModelListView() | ||
} | ||
|
||
NavigationLink("Model Info") { | ||
ModelInfoView() | ||
} | ||
|
||
NavigationLink("Copy Model") { | ||
CopyModelView() | ||
} | ||
|
||
NavigationLink("Delete Model") { | ||
DeleteModelView() | ||
} | ||
} | ||
|
||
Section("Generation") { | ||
NavigationLink("Chat") { | ||
ChatView() | ||
} | ||
|
||
NavigationLink("Generate") { | ||
GenerateView() | ||
} | ||
} | ||
|
||
Section("Embeddings") { | ||
NavigationLink("Embeddings") { | ||
EmbeddingsView() | ||
} | ||
} | ||
} | ||
.navigationTitle("OKPlayground") | ||
.navigationBarTitleDisplayMode(.inline) | ||
.task { | ||
await viewModel.reachable() | ||
await viewModel.fetchModels() | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// | ||
// ChatView.swift | ||
// OKPlayground | ||
// | ||
// Created by Kevin Hermawan on 09/06/24. | ||
// | ||
|
||
import Combine | ||
import SwiftUI | ||
import OllamaKit | ||
|
||
struct ChatView: View { | ||
@Environment(ViewModel.self) private var viewModel | ||
|
||
@State private var model: String? = nil | ||
@State private var prompt = "" | ||
@State private var response = "" | ||
@State private var cancellables = Set<AnyCancellable>() | ||
|
||
var body: some View { | ||
NavigationStack { | ||
Form { | ||
Section { | ||
Picker("Model", selection: $model) { | ||
ForEach(viewModel.models, id: \.self) { model in | ||
Text(model) | ||
.tag(model as String?) | ||
} | ||
} | ||
|
||
TextField("Prompt", text: $prompt) | ||
} | ||
|
||
Section { | ||
Button("Chat Async", action: actionAsync) | ||
Button("Chat Combine", action: actionCombine) | ||
} | ||
|
||
Section("Response") { | ||
Text(response) | ||
} | ||
} | ||
.navigationTitle("Chat") | ||
.navigationBarTitleDisplayMode(.inline) | ||
.onAppear { | ||
model = viewModel.models.first | ||
} | ||
} | ||
} | ||
|
||
func actionAsync() { | ||
self.response = "" | ||
|
||
guard let model = model else { return } | ||
let messages = [OKChatRequestData.Message(role: .user, content: prompt)] | ||
let data = OKChatRequestData(model: model, messages: messages) | ||
|
||
Task { | ||
for try await chunk in viewModel.ollamaKit.chat(data: data) { | ||
self.response += chunk.message?.content ?? "" | ||
} | ||
} | ||
} | ||
|
||
func actionCombine() { | ||
self.response = "" | ||
|
||
guard let model = model else { return } | ||
let messages = [OKChatRequestData.Message(role: .user, content: prompt)] | ||
let data = OKChatRequestData(model: model, messages: messages) | ||
|
||
viewModel.ollamaKit.chat(data: data) | ||
.sink { completion in | ||
switch completion { | ||
case .finished: | ||
print("Finished") | ||
case .failure(let error): | ||
print("Error:", error.localizedDescription) | ||
} | ||
} receiveValue: { value in | ||
self.response += value.message?.content ?? "" | ||
} | ||
.store(in: &cancellables) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// | ||
// CopyModelView.swift | ||
// OKPlayground | ||
// | ||
// Created by Kevin Hermawan on 08/06/24. | ||
// | ||
|
||
import Combine | ||
import SwiftUI | ||
import OllamaKit | ||
|
||
struct CopyModelView: View { | ||
@Environment(ViewModel.self) private var viewModel | ||
|
||
@State private var model: String? | ||
@State private var destination: String = "" | ||
@State private var cancellables = Set<AnyCancellable>() | ||
|
||
var body: some View { | ||
NavigationStack { | ||
Form { | ||
Section { | ||
Picker("Source", selection: $model) { | ||
ForEach(viewModel.models, id: \.self) { model in | ||
Text(model) | ||
.tag(model as String?) | ||
} | ||
} | ||
|
||
TextField("Destination", text: $destination) | ||
.textInputAutocapitalization(.never) | ||
} | ||
|
||
Section { | ||
Button("Copy Async", action: actionAsync) | ||
Button("Copy Combine", action: actionCombine) | ||
} | ||
} | ||
.navigationTitle("Copy Model") | ||
.navigationBarTitleDisplayMode(.inline) | ||
.onAppear { | ||
model = viewModel.models.first | ||
} | ||
} | ||
} | ||
|
||
private func actionAsync() { | ||
Task { | ||
guard let model = model else { return } | ||
let data = OKCopyModelRequestData(source: model, destination: destination) | ||
|
||
try await viewModel.ollamaKit.copyModel(data: data) | ||
} | ||
} | ||
|
||
private func actionCombine() { | ||
guard let model = model else { return } | ||
let data = OKCopyModelRequestData(source: model, destination: destination) | ||
|
||
viewModel.ollamaKit.copyModel(data: data) | ||
.sink { completion in | ||
switch completion { | ||
case .finished: | ||
print("Finished") | ||
case .failure(let error): | ||
print("Error:", error.localizedDescription) | ||
} | ||
} receiveValue: { value in | ||
print("Value:", value) | ||
} | ||
.store(in: &cancellables) | ||
} | ||
} |
Oops, something went wrong.