Skip to content

Commit 04bb3ef

Browse files
authored
feat: add Max Tokens and Context Window Setting Options for Ollama Channel (#1694)
* Update main.go with max_tokens param * Update model.go with max_tokens param * Update model.go * Update main.go * Update main.go * Adds num_ctx param for Ollama Channel * Added num_ctx param for ollama adapter * Added num_ctx param for ollama adapter * Improved data process logic
1 parent b4bfa41 commit 04bb3ef

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

relay/adaptor/ollama/main.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ func ConvertRequest(request model.GeneralOpenAIRequest) *ChatRequest {
3131
TopP: request.TopP,
3232
FrequencyPenalty: request.FrequencyPenalty,
3333
PresencePenalty: request.PresencePenalty,
34+
NumPredict: request.MaxTokens,
35+
NumCtx: request.NumCtx,
3436
},
3537
Stream: request.Stream,
3638
}
@@ -118,8 +120,10 @@ func StreamHandler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusC
118120
common.SetEventStreamHeaders(c)
119121

120122
for scanner.Scan() {
121-
data := strings.TrimPrefix(scanner.Text(), "}")
122-
data = data + "}"
123+
data := scanner.Text()
124+
if strings.HasPrefix(data, "}") {
125+
data = strings.TrimPrefix(data, "}") + "}"
126+
}
123127

124128
var ollamaResponse ChatResponse
125129
err := json.Unmarshal([]byte(data), &ollamaResponse)

relay/adaptor/ollama/model.go

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ type Options struct {
77
TopP float64 `json:"top_p,omitempty"`
88
FrequencyPenalty float64 `json:"frequency_penalty,omitempty"`
99
PresencePenalty float64 `json:"presence_penalty,omitempty"`
10+
NumPredict int `json:"num_predict,omitempty"`
11+
NumCtx int `json:"num_ctx,omitempty"`
1012
}
1113

1214
type Message struct {

relay/model/general.go

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type GeneralOpenAIRequest struct {
2929
Dimensions int `json:"dimensions,omitempty"`
3030
Instruction string `json:"instruction,omitempty"`
3131
Size string `json:"size,omitempty"`
32+
NumCtx int `json:"num_ctx,omitempty"`
3233
}
3334

3435
func (r GeneralOpenAIRequest) ParseInput() []string {

0 commit comments

Comments
 (0)