Skip to content

Commit

Permalink
temperature support (#18)
Browse files Browse the repository at this point in the history
* refactoring: Rootview

* add system prompt and topk, topp

* temperature support

* fix build error
  • Loading branch information
enoch1118 authored Mar 5, 2024
1 parent 39a9cd3 commit 4e22a93
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 39 deletions.
20 changes: 9 additions & 11 deletions ollamaGUI/Data/RoomEntity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,14 @@ extension ChatModel {
}
}

#if DEBUG
extension RoomEntity {
static var randomRoom: RoomEntity {
let room = RoomEntity()
room.title = RandomGenerator.randomNames.randomElement()
room.chats = [
]
extension RoomEntity {
static var randomRoom: RoomEntity {
let room = RoomEntity()
room.title = RandomGenerator.randomNames.randomElement()
room.chats = [
]

room.updatedAt = RandomGenerator.randomDate()
return room
}
room.updatedAt = RandomGenerator.randomDate()
return room
}
#endif
}
7 changes: 6 additions & 1 deletion ollamaGUI/Data/RoomOptionEntity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@ class RoomOptionEntity {
var system: String?
var top_p: Float
var top_k: Int
var temperature: Float?
init(model: String?,
system: String?,
top_p: Float = 0.9,
top_k: Int = 40) {
top_k: Int = 40,
temperature: Float = 0.8
) {

self.model = model
self.system = system
self.top_p = top_p
self.top_k = top_k
self.temperature = temperature
}
}
2 changes: 0 additions & 2 deletions ollamaGUI/Helper/RandomGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import Foundation

#if DEBUG
enum RandomGenerator {
static var randomNames: [String] {
[
Expand All @@ -33,4 +32,3 @@ import Foundation
}

}
#endif
5 changes: 4 additions & 1 deletion ollamaGUI/Model/ChatRequestModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ struct ChatRequestModel: Encodable, DictionaryEncodable {
guard let option = option else {
return self
}
options = OptionModel(top_p: option.top_p, top_k: option.top_k)
options = OptionModel(top_p: option.top_p,
top_k: option.top_k,
temperature: option.temperature)

if let model = option.model {
self.model = model
}
Expand Down
8 changes: 6 additions & 2 deletions ollamaGUI/Model/OptionModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,31 @@ import Foundation
struct OptionModel:Encodable,DictionaryEncodable {
var top_p: Float
var top_k: Int
var temperature: Float?

init(top_p: Float, top_k: Int) {
init(top_p: Float, top_k: Int,temperature:Float?) {
self.top_p = top_p
self.top_k = top_k
self.temperature = temperature
}

enum CodingKeys: CodingKey {
case top_p
case top_k
case temperature
}

func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.top_p, forKey: .top_p)
try container.encode(self.top_k, forKey: .top_k)
try container.encodeIfPresent(self.temperature, forKey: .temperature)
}


func getDictionary() -> [String : Any]? {
let dict = getLeafDictionary()
guard var d = dict else {
guard let d = dict else {
return nil
}
return d
Expand Down
7 changes: 1 addition & 6 deletions ollamaGUI/UI/BubbleShape.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,4 @@ struct BubbleShape: Shape {

}

#Preview {
VStack {
ChatBubble(chat: .init(type: RoleEnum.user))
ChatBubble(chat: .init(type: RoleEnum.assistant))
}
}

6 changes: 0 additions & 6 deletions ollamaGUI/UI/ChatBubble.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,3 @@ struct ChatBubble: View {

}

#Preview {
VStack {
ChatBubble(chat: .init(type: RoleEnum.user))
ChatBubble(chat: .init(type: RoleEnum.assistant))
}
}
20 changes: 13 additions & 7 deletions ollamaGUI/UI/SettingSheet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ import SwiftUI
struct SettingSheet: View {
@ObservedObject var model: SettingSheetViewModel
@Binding var showSetting: Bool

var room:RoomEntity
@State var roomState: RoomEntity

init(room: RoomEntity,showSetting: Binding<Bool>) {
self.room = room
self.roomState = room
self.model = SettingSheetViewModel()
self._showSetting = showSetting
model.system = room.option?.system ?? ""
Expand Down Expand Up @@ -51,18 +50,25 @@ struct SettingSheet: View {
}
Text("Works together with top-k. A higher value (e.g., 0.95) will lead to more diverse text, while a lower value (e.g., 0.5) will generate more focused and conservative text. (Default: 0.9)").font(.caption)
Slider(value: $model.topp,in: 0...1)
HStack{
Text("Temperature").font(.headline)
Spacer()
Text("\(String(format: "%.2f",model.temperature))")
}
Text("The temperature of the model. Increasing the temperature will make the model answer more creatively. (Default: 0.8)").font(.caption)
Slider(value: $model.temperature,in: 0...1)
Spacer().frame(height: 16)

HStack{
Text(model.helpText).font(.caption)
Spacer()
Button(action: {

model.reset()
model.apply()
}){
Text("reset")
}.buttonStyle(CommonButton(enabled: false))
Button(action: {
model.apply()
room.option = model.options
roomState.option = model.options
showSetting.toggle()
}){
Text("apply change")
Expand Down
15 changes: 12 additions & 3 deletions ollamaGUI/UI/SettingSheetViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,25 @@ class SettingSheetViewModel: ObservableObject {
@Published var system: String
@Published var topk: Float
@Published var topp: Float
@Published var temperature: Float


@Published var helpText:String

init() {
options = nil
system = ""
helpText = ""
topk = 40
topp = 0.9
temperature = 0.8
}


func reset() {
options = nil
system = ""
topk = 40
topp = 0.9
temperature = 0.8
}


Expand All @@ -34,10 +43,10 @@ class SettingSheetViewModel: ObservableObject {
}
options.top_k = Int(topk)
options.top_p = topp
options.temperature = temperature
if !system.isEmpty {
options.system = system
}
helpText = "success"
print("success")
}
}

0 comments on commit 4e22a93

Please sign in to comment.