Skip to content

Commit

Permalink
chore: adds Playground (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinhermawan authored Jun 8, 2024
1 parent 956f630 commit 2d17673
Show file tree
Hide file tree
Showing 18 changed files with 1,125 additions and 0 deletions.
429 changes: 429 additions & 0 deletions Playground/OKPlayground.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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>
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
}
20 changes: 20 additions & 0 deletions Playground/OKPlayground/OKPlaygroundApp.swift
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)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"colors" : [
{
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
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
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
28 changes: 28 additions & 0 deletions Playground/OKPlayground/ViewModels/ViewModel.swift
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
}
}
63 changes: 63 additions & 0 deletions Playground/OKPlayground/Views/AppView.swift
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()
}
}
}
}
85 changes: 85 additions & 0 deletions Playground/OKPlayground/Views/ChatView.swift
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)
}
}
73 changes: 73 additions & 0 deletions Playground/OKPlayground/Views/CopyModelView.swift
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)
}
}
Loading

0 comments on commit 2d17673

Please # to comment.