Skip to content

Commit

Permalink
feat: add system prompt support
Browse files Browse the repository at this point in the history
  • Loading branch information
JingYiJun committed Feb 9, 2024
1 parent 372267c commit e203139
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
21 changes: 13 additions & 8 deletions apis/record/infer.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,19 @@ func InferOpenAI(
openaiConfig.BaseURL = model.Url
client := openai.NewClientWithConfig(openaiConfig)

var messages = make([]openai.ChatCompletionMessage, 0, len(postRecord)+2)
messages = append(messages, openai.ChatCompletionMessage{
Role: "system",
Content: model.OpenAISystemPrompt,
})
messages = append(messages, postRecord.ToOpenAIMessages()...)
messages = append(messages, openai.ChatCompletionMessage{
Role: "user",
Content: record.Request,
})
request := openai.ChatCompletionRequest{
Model: model.OpenAIModelName,
Messages: append(
postRecord.ToOpenAIMessages(),
openai.ChatCompletionMessage{
Role: "user",
Content: record.Request,
},
),
Model: model.OpenAIModelName,
Messages: messages,
}

if ctx == nil {
Expand Down Expand Up @@ -137,6 +141,7 @@ func InferOpenAI(

resultBuilder.WriteString(response.Choices[0].Delta.Content)
nowOutput = resultBuilder.String()

before, _, found := CutLastAny(nowOutput, ",.?!\n,。?!")
if !found || before == detectedOutput {
continue
Expand Down
1 change: 1 addition & 0 deletions models/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type ModelConfig struct {
CallbackUrl string `json:"callback_url"`
APIType APIType `json:"api_type"`
OpenAIModelName string `json:"openai_model_name"`
OpenAISystemPrompt string `json:"openai_system_prompt"`
}

type ModelConfigs = []*ModelConfig
Expand Down

0 comments on commit e203139

Please sign in to comment.