Skip to content

Commit

Permalink
feat: add Max Tokens and Context Window Setting Options for Ollama Ch…
Browse files Browse the repository at this point in the history
…annel (#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
  • Loading branch information
MotorBottle authored Aug 6, 2024
1 parent b4bfa41 commit 04bb3ef
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
8 changes: 6 additions & 2 deletions relay/adaptor/ollama/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func ConvertRequest(request model.GeneralOpenAIRequest) *ChatRequest {
TopP: request.TopP,
FrequencyPenalty: request.FrequencyPenalty,
PresencePenalty: request.PresencePenalty,
NumPredict: request.MaxTokens,
NumCtx: request.NumCtx,
},
Stream: request.Stream,
}
Expand Down Expand Up @@ -118,8 +120,10 @@ func StreamHandler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusC
common.SetEventStreamHeaders(c)

for scanner.Scan() {
data := strings.TrimPrefix(scanner.Text(), "}")
data = data + "}"
data := scanner.Text()
if strings.HasPrefix(data, "}") {
data = strings.TrimPrefix(data, "}") + "}"
}

var ollamaResponse ChatResponse
err := json.Unmarshal([]byte(data), &ollamaResponse)
Expand Down
2 changes: 2 additions & 0 deletions relay/adaptor/ollama/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ type Options struct {
TopP float64 `json:"top_p,omitempty"`
FrequencyPenalty float64 `json:"frequency_penalty,omitempty"`
PresencePenalty float64 `json:"presence_penalty,omitempty"`
NumPredict int `json:"num_predict,omitempty"`
NumCtx int `json:"num_ctx,omitempty"`
}

type Message struct {
Expand Down
1 change: 1 addition & 0 deletions relay/model/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type GeneralOpenAIRequest struct {
Dimensions int `json:"dimensions,omitempty"`
Instruction string `json:"instruction,omitempty"`
Size string `json:"size,omitempty"`
NumCtx int `json:"num_ctx,omitempty"`
}

func (r GeneralOpenAIRequest) ParseInput() []string {
Expand Down

0 comments on commit 04bb3ef

Please sign in to comment.