feat(generation): score articles with selected relevance model (#48)
This commit is contained in:
@ -24,19 +24,29 @@ func newOpenAI(apiKey, model string) *openAIProvider {
|
||||
func (p *openAIProvider) Name() string { return "openai" }
|
||||
|
||||
func (p *openAIProvider) Summarize(ctx context.Context, prompt string, opts GenOptions) (string, error) {
|
||||
resp, err := p.client.CreateChatCompletion(ctx, openai.ChatCompletionRequest{
|
||||
request := openai.ChatCompletionRequest{
|
||||
Model: p.model,
|
||||
MaxCompletionTokens: opts.MaxOutputTokens,
|
||||
Messages: []openai.ChatCompletionMessage{
|
||||
{Role: openai.ChatMessageRoleUser, Content: prompt},
|
||||
},
|
||||
})
|
||||
}
|
||||
if opts.JSONMode {
|
||||
request.ResponseFormat = &openai.ChatCompletionResponseFormat{Type: openai.ChatCompletionResponseFormatTypeJSONObject}
|
||||
}
|
||||
if len(opts.JSONSchema) > 0 {
|
||||
request.ResponseFormat = &openai.ChatCompletionResponseFormat{Type: openai.ChatCompletionResponseFormatTypeJSONSchema, JSONSchema: &openai.ChatCompletionResponseFormatJSONSchema{Name: "title_relevance", Strict: true, Schema: opts.JSONSchema}}
|
||||
}
|
||||
resp, err := p.client.CreateChatCompletion(ctx, request)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(resp.Choices) == 0 {
|
||||
return "", nil
|
||||
}
|
||||
if opts.RequireComplete && resp.Choices[0].FinishReason != openai.FinishReasonStop {
|
||||
return "", errGenerationIncomplete
|
||||
}
|
||||
return resp.Choices[0].Message.Content, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user