@@ -26,7 +26,7 @@ type Chat struct {
26
26
// NewChat initializes and returns a new Chat instance.
27
27
func NewChat () * Chat {
28
28
return & Chat {
29
- endPoint : "/api /chat" ,
29
+ endPoint : "/v1 /chat/completions " ,
30
30
}
31
31
}
32
32
@@ -85,58 +85,58 @@ func (c *Chat) RequestConvert(ctx eocontext.EoContext, extender map[string]inter
85
85
86
86
// SetProvider the modified body in the HTTP context.
87
87
httpContext .Proxy ().Body ().SetRaw ("application/json" , body )
88
- httpContext .Response ().AppendStreamFunc (c .streamFunc ())
88
+ // httpContext.Response().AppendStreamFunc(c.streamFunc())
89
89
return nil
90
90
}
91
91
92
- func (c * Chat ) streamFunc () http_context.StreamFunc {
93
- return func (ctx http_context.IHttpContext , p []byte ) ([]byte , error ) {
94
- data := eosc .NewBase [Response ]()
95
- err := json .Unmarshal (p , data )
96
- if err != nil {
97
- return nil , err
98
- }
99
- status := ctx .Response ().StatusCode ()
100
- switch status {
101
- case 200 :
102
- // Calculate the token consumption for a successful request.
103
- usage := data .Config
104
- if usage .Done {
105
- convert .SetAIStatusNormal (ctx )
106
- convert .SetAIModelInputToken (ctx , usage .PromptEvalCount )
107
- convert .SetAIModelOutputToken (ctx , usage .EvalCount )
108
- convert .SetAIModelTotalToken (ctx , usage .PromptEvalCount + usage .EvalCount )
109
- }
110
- case 404 :
111
- convert .SetAIStatusInvalid (ctx )
112
- case 429 :
113
- convert .SetAIStatusExceeded (ctx )
114
- }
115
-
116
- // Prepare the response body for the client.
117
- responseBody := & convert.ClientResponse {}
118
- resp := data .Config
119
- if resp .Message != nil {
120
- responseBody .Message = & convert.Message {
121
- Role : resp .Message .Role ,
122
- Content : resp .Message .Content ,
123
- }
124
- if resp .Done {
125
- responseBody .FinishReason = convert .FinishStop
126
- }
127
- } else {
128
- responseBody .Code = - 1
129
- responseBody .Error = "response message is nil"
130
- }
131
-
132
- // Marshal the modified response body back into JSON.
133
- body , err := json .Marshal (responseBody )
134
- if err != nil {
135
- return nil , err
136
- }
137
- return body , nil
138
- }
139
- }
92
+ // func (c *Chat) streamFunc() http_context.StreamFunc {
93
+ // return func(ctx http_context.IHttpContext, p []byte) ([]byte, error) {
94
+ // data := eosc.NewBase[Response]()
95
+ // err := json.Unmarshal(p, data)
96
+ // if err != nil {
97
+ // return nil, err
98
+ // }
99
+ // status := ctx.Response().StatusCode()
100
+ // switch status {
101
+ // case 200:
102
+ // // Calculate the token consumption for a successful request.
103
+ // usage := data.Config
104
+ // if usage.Done {
105
+ // convert.SetAIStatusNormal(ctx)
106
+ // convert.SetAIModelInputToken(ctx, usage.PromptEvalCount)
107
+ // convert.SetAIModelOutputToken(ctx, usage.EvalCount)
108
+ // convert.SetAIModelTotalToken(ctx, usage.PromptEvalCount+usage.EvalCount)
109
+ // }
110
+ // case 404:
111
+ // convert.SetAIStatusInvalid(ctx)
112
+ // case 429:
113
+ // convert.SetAIStatusExceeded(ctx)
114
+ // }
115
+ //
116
+ // // Prepare the response body for the client.
117
+ // responseBody := &convert.ClientResponse{}
118
+ // resp := data.Config
119
+ // if resp.Message != nil {
120
+ // responseBody.Message = &convert.Message{
121
+ // Role: resp.Message.Role,
122
+ // Content: resp.Message.Content,
123
+ // }
124
+ // if resp.Done {
125
+ // responseBody.FinishReason = convert.FinishStop
126
+ // }
127
+ // } else {
128
+ // responseBody.Code = -1
129
+ // responseBody.Error = "response message is nil"
130
+ // }
131
+ //
132
+ // // Marshal the modified response body back into JSON.
133
+ // body, err := json.Marshal(responseBody)
134
+ // if err != nil {
135
+ // return nil, err
136
+ // }
137
+ // return body, nil
138
+ // }
139
+ // }
140
140
141
141
// ResponseConvert converts the response body for the Chat mode.
142
142
// It processes the response to ensure it conforms to the expected format and encoding.
@@ -147,58 +147,49 @@ func (c *Chat) ResponseConvert(ctx eocontext.EoContext) error {
147
147
return err
148
148
}
149
149
150
- status := httpContext .Response ().StatusCode ()
151
- switch status {
152
- case 200 :
153
- convert .SetAIStatusNormal (ctx )
154
- }
155
- if httpContext .Response ().IsBodyStream () {
156
-
157
- return nil
158
- }
159
150
// Retrieve the response body.
160
151
body := httpContext .Response ().GetBody ()
161
152
if body == nil {
162
153
return nil
163
154
}
164
155
165
156
// Parse the response body into a base configuration.
166
- data := eosc .NewBase [Response ]()
157
+ data := eosc .NewBase [convert. Response ]()
167
158
err = json .Unmarshal (body , data )
168
159
if err != nil {
169
160
return err
170
161
}
171
162
switch httpContext .Response ().StatusCode () {
172
163
case 200 :
173
164
// Calculate the token consumption for a successful request.
174
- usage := data .Config
165
+ usage := data .Config . Usage
175
166
convert .SetAIStatusNormal (ctx )
176
- convert .SetAIModelInputToken (ctx , usage .PromptEvalCount )
177
- convert .SetAIModelOutputToken (ctx , usage .EvalCount )
178
- convert .SetAIModelTotalToken (ctx , usage .PromptEvalCount + usage . EvalCount )
179
- }
180
-
181
- // Prepare the response body for the client.
182
- responseBody := & convert.ClientResponse {}
183
- resp := data .Config
184
- if resp .Message != nil {
185
- responseBody .Message = & convert.Message {
186
- Role : resp .Message .Role ,
187
- Content : resp .Message .Content ,
188
- }
189
- responseBody .FinishReason = convert .FinishStop
190
- } else {
191
- responseBody .Code = - 1
192
- responseBody .Error = resp .Error
193
- }
194
-
195
- // Marshal the modified response body back into JSON.
196
- body , err = json .Marshal (responseBody )
197
- if err != nil {
198
- return err
199
- }
200
-
201
- httpContext .Response ().SetBody (body )
167
+ convert .SetAIModelInputToken (ctx , usage .PromptTokens )
168
+ convert .SetAIModelOutputToken (ctx , usage .CompletionTokens )
169
+ convert .SetAIModelTotalToken (ctx , usage .TotalTokens )
170
+ }
171
+ //
172
+ //// Prepare the response body for the client.
173
+ // responseBody := &convert.ClientResponse{}
174
+ // resp := data.Config
175
+ // if resp.Choices != nil {
176
+ // responseBody.Message = &convert.Message{
177
+ // Role: resp.Message.Role,
178
+ // Content: resp.Message.Content,
179
+ // }
180
+ // responseBody.FinishReason = convert.FinishStop
181
+ // } else {
182
+ // responseBody.Code = -1
183
+ // responseBody.Error = resp.Error
184
+ // }
185
+ //
186
+ //// Marshal the modified response body back into JSON.
187
+ // body, err = json.Marshal(responseBody)
188
+ // if err != nil {
189
+ // return err
190
+ // }
191
+ //
192
+ // httpContext.Response().SetBody(body)
202
193
203
194
// SetProvider the modified response in the HTTP context.
204
195
return nil
0 commit comments