-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathchat.go
More file actions
102 lines (85 loc) · 3.23 KB
/
chat.go
File metadata and controls
102 lines (85 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package schema
// ChatRequest defines Glide's Chat Request Schema unified across all language models
type ChatRequest struct {
Message ChatMessage `json:"message" validate:"required"`
MessageHistory []ChatMessage `json:"message_history,omitempty"`
OverrideParams *map[string]ModelParamsOverride `json:"override_params,omitempty"`
}
func (r *ChatRequest) ModelParams(modelNameOrID string) *ModelParamsOverride {
if r.OverrideParams == nil {
return nil
}
if override, found := (*r.OverrideParams)[modelNameOrID]; found {
return &override
}
return nil
}
// ModelParamsOverride allows to redefine chat message and model params based on the model ID
//
// Glide provides an abstraction around concreate models and this is a way to be able to provide model-specific params if needed.
// The override is going to be applied if Glide picks the referenced there (it may pick another model to serve a given request)
type ModelParamsOverride struct {
// TODO: should be just string?
Message ChatMessage `json:"message,omitempty"`
// TODO(185): Add an ability to override model params
}
// ChatParams represents a chat request params that overrides the default model params from configs
type ChatParams struct {
Messages []ChatMessage
// TODO(185): set other params
}
// Params returns a specific chat request params account for model-specific overrides.
func (r *ChatRequest) Params(modelID string, modelName string) *ChatParams {
params := &ChatParams{
Messages: make([]ChatMessage, 0, len(r.MessageHistory)+1),
}
reqMessage := r.Message
if override := r.ModelParams(modelName); override != nil {
// TODO(185): set other params
reqMessage = override.Message
}
if override := r.ModelParams(modelID); override != nil {
// TODO(185): set other params
reqMessage = override.Message
}
params.Messages = append(params.Messages, r.MessageHistory...)
params.Messages = append(params.Messages, reqMessage)
return params
}
func NewChatFromStr(message string) *ChatRequest {
return &ChatRequest{
Message: ChatMessage{
"user",
message,
},
}
}
// ChatResponse defines Glide's Chat Response Schema unified across all language models
type ChatResponse struct {
ID string `json:"id"`
Created int `json:"created_at"`
Provider string `json:"provider_id"`
RouterID string `json:"router_id"`
ModelID string `json:"model_id"`
ModelName string `json:"model_name"`
Cached bool `json:"cached"`
ModelResponse ModelResponse `json:"model_response"`
}
// ModelResponse is the unified response from the provider.
type ModelResponse struct {
Metadata map[string]string `json:"metadata"`
Message ChatMessage `json:"message"`
TokenUsage TokenUsage `json:"token_usage"`
}
type TokenUsage struct {
PromptTokens int `json:"prompt_tokens"`
ResponseTokens int `json:"response_tokens"`
TotalTokens int `json:"total_tokens"`
}
// ChatMessage is a message in a chat request.
type ChatMessage struct {
// The role of the author of this message. One of system, user, or assistant.
Role string `json:"role" validate:"required"`
// The content of the message.
Content string `json:"content" validate:"required"`
}