2024-01-05 17:04:46 +00:00
|
|
|
package openai
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2024-04-17 21:33:49 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2024-01-05 17:04:46 +00:00
|
|
|
|
2024-04-17 21:33:49 +00:00
|
|
|
"github.com/go-skynet/LocalAI/core/backend"
|
|
|
|
"github.com/go-skynet/LocalAI/core/config"
|
2024-02-21 01:21:19 +00:00
|
|
|
"github.com/go-skynet/LocalAI/core/schema"
|
2024-04-18 20:43:12 +00:00
|
|
|
"github.com/go-skynet/LocalAI/pkg/functions"
|
2024-04-17 21:33:49 +00:00
|
|
|
model "github.com/go-skynet/LocalAI/pkg/model"
|
2024-01-05 17:04:46 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
2024-04-17 21:33:49 +00:00
|
|
|
"github.com/google/uuid"
|
2024-01-05 17:04:46 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
)
|
|
|
|
|
2024-03-29 21:29:33 +00:00
|
|
|
// ChatEndpoint is the OpenAI Completion API endpoint https://platform.openai.com/docs/api-reference/chat/create
|
|
|
|
// @Summary Generate a chat completions for a given prompt and model.
|
|
|
|
// @Param request body schema.OpenAIRequest true "query params"
|
|
|
|
// @Success 200 {object} schema.OpenAIResponse "Response"
|
|
|
|
// @Router /v1/chat/completions [post]
|
2024-04-17 21:33:49 +00:00
|
|
|
func ChatEndpoint(cl *config.BackendConfigLoader, ml *model.ModelLoader, startupOptions *config.ApplicationConfig) func(c *fiber.Ctx) error {
|
|
|
|
emptyMessage := ""
|
|
|
|
id := uuid.New().String()
|
|
|
|
created := int(time.Now().Unix())
|
|
|
|
|
|
|
|
process := func(s string, req *schema.OpenAIRequest, config *config.BackendConfig, loader *model.ModelLoader, responses chan schema.OpenAIResponse) {
|
|
|
|
initialMessage := schema.OpenAIResponse{
|
|
|
|
ID: id,
|
|
|
|
Created: created,
|
|
|
|
Model: req.Model, // we have to return what the user sent here, due to OpenAI spec.
|
|
|
|
Choices: []schema.Choice{{Delta: &schema.Message{Role: "assistant", Content: &emptyMessage}}},
|
|
|
|
Object: "chat.completion.chunk",
|
|
|
|
}
|
|
|
|
responses <- initialMessage
|
|
|
|
|
|
|
|
ComputeChoices(req, s, config, startupOptions, loader, func(s string, c *[]schema.Choice) {}, func(s string, usage backend.TokenUsage) bool {
|
|
|
|
resp := schema.OpenAIResponse{
|
|
|
|
ID: id,
|
|
|
|
Created: created,
|
|
|
|
Model: req.Model, // we have to return what the user sent here, due to OpenAI spec.
|
|
|
|
Choices: []schema.Choice{{Delta: &schema.Message{Content: &s}, Index: 0}},
|
|
|
|
Object: "chat.completion.chunk",
|
|
|
|
Usage: schema.OpenAIUsage{
|
|
|
|
PromptTokens: usage.Prompt,
|
|
|
|
CompletionTokens: usage.Completion,
|
|
|
|
TotalTokens: usage.Prompt + usage.Completion,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
responses <- resp
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
close(responses)
|
|
|
|
}
|
|
|
|
processTools := func(noAction string, prompt string, req *schema.OpenAIRequest, config *config.BackendConfig, loader *model.ModelLoader, responses chan schema.OpenAIResponse) {
|
|
|
|
result := ""
|
|
|
|
_, tokenUsage, _ := ComputeChoices(req, prompt, config, startupOptions, loader, func(s string, c *[]schema.Choice) {}, func(s string, usage backend.TokenUsage) bool {
|
|
|
|
result += s
|
|
|
|
// TODO: Change generated BNF grammar to be compliant with the schema so we can
|
|
|
|
// stream the result token by token here.
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
2024-04-18 20:43:12 +00:00
|
|
|
results := functions.ParseFunctionCall(result, config.FunctionsConfig)
|
|
|
|
noActionToRun := len(results) > 0 && results[0].Name == noAction || len(results) == 0
|
2024-04-17 21:33:49 +00:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case noActionToRun:
|
|
|
|
initialMessage := schema.OpenAIResponse{
|
|
|
|
ID: id,
|
|
|
|
Created: created,
|
|
|
|
Model: req.Model, // we have to return what the user sent here, due to OpenAI spec.
|
|
|
|
Choices: []schema.Choice{{Delta: &schema.Message{Role: "assistant", Content: &emptyMessage}}},
|
|
|
|
Object: "chat.completion.chunk",
|
|
|
|
}
|
|
|
|
responses <- initialMessage
|
|
|
|
|
2024-05-14 07:39:20 +00:00
|
|
|
result, err := handleQuestion(config, req, ml, startupOptions, results, result, prompt)
|
2024-04-17 21:33:49 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("error handling question")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := schema.OpenAIResponse{
|
|
|
|
ID: id,
|
|
|
|
Created: created,
|
|
|
|
Model: req.Model, // we have to return what the user sent here, due to OpenAI spec.
|
|
|
|
Choices: []schema.Choice{{Delta: &schema.Message{Content: &result}, Index: 0}},
|
|
|
|
Object: "chat.completion.chunk",
|
|
|
|
Usage: schema.OpenAIUsage{
|
|
|
|
PromptTokens: tokenUsage.Prompt,
|
|
|
|
CompletionTokens: tokenUsage.Completion,
|
|
|
|
TotalTokens: tokenUsage.Prompt + tokenUsage.Completion,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
responses <- resp
|
|
|
|
|
|
|
|
default:
|
|
|
|
for i, ss := range results {
|
2024-04-18 20:43:12 +00:00
|
|
|
name, args := ss.Name, ss.Arguments
|
2024-04-17 21:33:49 +00:00
|
|
|
|
|
|
|
initialMessage := schema.OpenAIResponse{
|
|
|
|
ID: id,
|
|
|
|
Created: created,
|
|
|
|
Model: req.Model, // we have to return what the user sent here, due to OpenAI spec.
|
|
|
|
Choices: []schema.Choice{{
|
|
|
|
Delta: &schema.Message{
|
|
|
|
Role: "assistant",
|
|
|
|
ToolCalls: []schema.ToolCall{
|
|
|
|
{
|
|
|
|
Index: i,
|
|
|
|
ID: id,
|
|
|
|
Type: "function",
|
|
|
|
FunctionCall: schema.FunctionCall{
|
|
|
|
Name: name,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}}},
|
|
|
|
Object: "chat.completion.chunk",
|
|
|
|
}
|
|
|
|
responses <- initialMessage
|
|
|
|
|
|
|
|
responses <- schema.OpenAIResponse{
|
|
|
|
ID: id,
|
|
|
|
Created: created,
|
|
|
|
Model: req.Model, // we have to return what the user sent here, due to OpenAI spec.
|
|
|
|
Choices: []schema.Choice{{
|
|
|
|
Delta: &schema.Message{
|
|
|
|
Role: "assistant",
|
|
|
|
ToolCalls: []schema.ToolCall{
|
|
|
|
{
|
|
|
|
Index: i,
|
|
|
|
ID: id,
|
|
|
|
Type: "function",
|
|
|
|
FunctionCall: schema.FunctionCall{
|
|
|
|
Arguments: args,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}}},
|
|
|
|
Object: "chat.completion.chunk",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(responses)
|
|
|
|
}
|
|
|
|
|
2024-01-05 17:04:46 +00:00
|
|
|
return func(c *fiber.Ctx) error {
|
2024-04-17 21:33:49 +00:00
|
|
|
modelFile, input, err := readRequest(c, ml, startupOptions, true)
|
2024-01-05 17:04:46 +00:00
|
|
|
if err != nil {
|
2024-04-17 21:33:49 +00:00
|
|
|
return fmt.Errorf("failed reading parameters from request:%w", err)
|
2024-01-05 17:04:46 +00:00
|
|
|
}
|
|
|
|
|
2024-04-17 21:33:49 +00:00
|
|
|
config, input, err := mergeRequestWithConfig(modelFile, input, cl, ml, startupOptions.Debug, startupOptions.Threads, startupOptions.ContextSize, startupOptions.F16)
|
2024-01-05 17:04:46 +00:00
|
|
|
if err != nil {
|
2024-04-17 21:33:49 +00:00
|
|
|
return fmt.Errorf("failed reading parameters from request:%w", err)
|
|
|
|
}
|
|
|
|
log.Debug().Msgf("Configuration read: %+v", config)
|
|
|
|
|
2024-04-18 20:43:12 +00:00
|
|
|
funcs := input.Functions
|
|
|
|
shouldUseFn := len(input.Functions) > 0 && config.ShouldUseFunctions()
|
|
|
|
|
2024-04-17 21:33:49 +00:00
|
|
|
// Allow the user to set custom actions via config file
|
|
|
|
// to be "embedded" in each model
|
|
|
|
noActionName := "answer"
|
|
|
|
noActionDescription := "use this action to answer without performing any action"
|
|
|
|
|
|
|
|
if config.FunctionsConfig.NoActionFunctionName != "" {
|
|
|
|
noActionName = config.FunctionsConfig.NoActionFunctionName
|
|
|
|
}
|
|
|
|
if config.FunctionsConfig.NoActionDescriptionName != "" {
|
|
|
|
noActionDescription = config.FunctionsConfig.NoActionDescriptionName
|
|
|
|
}
|
|
|
|
|
|
|
|
if input.ResponseFormat.Type == "json_object" {
|
2024-04-18 20:43:12 +00:00
|
|
|
input.Grammar = functions.JSONBNF
|
2024-04-17 21:33:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
config.Grammar = input.Grammar
|
|
|
|
|
2024-04-18 20:43:12 +00:00
|
|
|
if shouldUseFn {
|
2024-04-17 21:33:49 +00:00
|
|
|
log.Debug().Msgf("Response needs to process functions")
|
2024-04-18 20:43:12 +00:00
|
|
|
}
|
2024-04-17 21:33:49 +00:00
|
|
|
|
2024-04-18 20:43:12 +00:00
|
|
|
switch {
|
|
|
|
case !config.FunctionsConfig.NoGrammar && shouldUseFn:
|
|
|
|
noActionGrammar := functions.Function{
|
2024-04-17 21:33:49 +00:00
|
|
|
Name: noActionName,
|
|
|
|
Description: noActionDescription,
|
|
|
|
Parameters: map[string]interface{}{
|
|
|
|
"properties": map[string]interface{}{
|
|
|
|
"message": map[string]interface{}{
|
|
|
|
"type": "string",
|
|
|
|
"description": "The message to reply the user with",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append the no action function
|
|
|
|
if !config.FunctionsConfig.DisableNoAction {
|
|
|
|
funcs = append(funcs, noActionGrammar)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Force picking one of the functions by the request
|
|
|
|
if config.FunctionToCall() != "" {
|
|
|
|
funcs = funcs.Select(config.FunctionToCall())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update input grammar
|
2024-05-11 23:13:22 +00:00
|
|
|
// Handle if we should return "name" instead of "functions"
|
|
|
|
if config.FunctionsConfig.FunctionName {
|
|
|
|
jsStruct := funcs.ToJSONNameStructure()
|
2024-05-15 18:03:18 +00:00
|
|
|
config.Grammar = jsStruct.Grammar(config.FunctionsConfig.GrammarPrefix, "", config.FunctionsConfig.ParallelCalls, config.FunctionsConfig.GrammarMessage)
|
2024-05-11 23:13:22 +00:00
|
|
|
} else {
|
|
|
|
jsStruct := funcs.ToJSONFunctionStructure()
|
2024-05-15 18:03:18 +00:00
|
|
|
config.Grammar = jsStruct.Grammar(config.FunctionsConfig.GrammarPrefix, "", config.FunctionsConfig.ParallelCalls, config.FunctionsConfig.GrammarMessage)
|
2024-05-11 23:13:22 +00:00
|
|
|
}
|
2024-04-18 20:43:12 +00:00
|
|
|
case input.JSONFunctionGrammarObject != nil:
|
2024-05-15 18:03:18 +00:00
|
|
|
config.Grammar = input.JSONFunctionGrammarObject.Grammar(config.FunctionsConfig.GrammarPrefix, "", config.FunctionsConfig.ParallelCalls, config.FunctionsConfig.GrammarMessage)
|
2024-05-11 23:13:22 +00:00
|
|
|
case input.JSONFunctionGrammarObjectName != nil:
|
2024-05-15 18:03:18 +00:00
|
|
|
config.Grammar = input.JSONFunctionGrammarObjectName.Grammar(config.FunctionsConfig.GrammarPrefix, "", config.FunctionsConfig.ParallelCalls, config.FunctionsConfig.GrammarMessage)
|
2024-04-18 20:43:12 +00:00
|
|
|
default:
|
|
|
|
// Force picking one of the functions by the request
|
|
|
|
if config.FunctionToCall() != "" {
|
|
|
|
funcs = funcs.Select(config.FunctionToCall())
|
|
|
|
}
|
2024-01-05 17:04:46 +00:00
|
|
|
}
|
|
|
|
|
2024-04-18 20:43:12 +00:00
|
|
|
// process functions if we have any defined or if we have a function call string
|
|
|
|
|
2024-04-17 21:33:49 +00:00
|
|
|
// functions are not supported in stream mode (yet?)
|
|
|
|
toStream := input.Stream
|
|
|
|
|
|
|
|
log.Debug().Msgf("Parameters: %+v", config)
|
|
|
|
|
|
|
|
var predInput string
|
|
|
|
|
|
|
|
// If we are using the tokenizer template, we don't need to process the messages
|
|
|
|
// unless we are processing functions
|
2024-04-18 20:43:12 +00:00
|
|
|
if !config.TemplateConfig.UseTokenizerTemplate || shouldUseFn {
|
2024-04-17 21:33:49 +00:00
|
|
|
suppressConfigSystemPrompt := false
|
|
|
|
mess := []string{}
|
|
|
|
for messageIndex, i := range input.Messages {
|
|
|
|
var content string
|
|
|
|
role := i.Role
|
|
|
|
|
|
|
|
// if function call, we might want to customize the role so we can display better that the "assistant called a json action"
|
|
|
|
// if an "assistant_function_call" role is defined, we use it, otherwise we use the role that is passed by in the request
|
|
|
|
if (i.FunctionCall != nil || i.ToolCalls != nil) && i.Role == "assistant" {
|
|
|
|
roleFn := "assistant_function_call"
|
|
|
|
r := config.Roles[roleFn]
|
|
|
|
if r != "" {
|
|
|
|
role = roleFn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r := config.Roles[role]
|
|
|
|
contentExists := i.Content != nil && i.StringContent != ""
|
|
|
|
|
|
|
|
fcall := i.FunctionCall
|
|
|
|
if len(i.ToolCalls) > 0 {
|
|
|
|
fcall = i.ToolCalls
|
|
|
|
}
|
|
|
|
|
|
|
|
// First attempt to populate content via a chat message specific template
|
|
|
|
if config.TemplateConfig.ChatMessage != "" {
|
|
|
|
chatMessageData := model.ChatMessageTemplateData{
|
|
|
|
SystemPrompt: config.SystemPrompt,
|
|
|
|
Role: r,
|
|
|
|
RoleName: role,
|
|
|
|
Content: i.StringContent,
|
|
|
|
FunctionCall: fcall,
|
|
|
|
FunctionName: i.Name,
|
|
|
|
LastMessage: messageIndex == (len(input.Messages) - 1),
|
|
|
|
Function: config.Grammar != "" && (messageIndex == (len(input.Messages) - 1)),
|
|
|
|
MessageIndex: messageIndex,
|
|
|
|
}
|
|
|
|
templatedChatMessage, err := ml.EvaluateTemplateForChatMessage(config.TemplateConfig.ChatMessage, chatMessageData)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Interface("message", chatMessageData).Str("template", config.TemplateConfig.ChatMessage).Msg("error processing message with template, skipping")
|
|
|
|
} else {
|
|
|
|
if templatedChatMessage == "" {
|
|
|
|
log.Warn().Msgf("template \"%s\" produced blank output for %+v. Skipping!", config.TemplateConfig.ChatMessage, chatMessageData)
|
|
|
|
continue // TODO: This continue is here intentionally to skip over the line `mess = append(mess, content)` below, and to prevent the sprintf
|
|
|
|
}
|
|
|
|
log.Debug().Msgf("templated message for chat: %s", templatedChatMessage)
|
|
|
|
content = templatedChatMessage
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
marshalAnyRole := func(f any) {
|
|
|
|
j, err := json.Marshal(f)
|
|
|
|
if err == nil {
|
|
|
|
if contentExists {
|
|
|
|
content += "\n" + fmt.Sprint(r, " ", string(j))
|
|
|
|
} else {
|
|
|
|
content = fmt.Sprint(r, " ", string(j))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
marshalAny := func(f any) {
|
|
|
|
j, err := json.Marshal(f)
|
|
|
|
if err == nil {
|
|
|
|
if contentExists {
|
|
|
|
content += "\n" + string(j)
|
|
|
|
} else {
|
|
|
|
content = string(j)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If this model doesn't have such a template, or if that template fails to return a value, template at the message level.
|
|
|
|
if content == "" {
|
|
|
|
if r != "" {
|
|
|
|
if contentExists {
|
|
|
|
content = fmt.Sprint(r, i.StringContent)
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.FunctionCall != nil {
|
|
|
|
marshalAnyRole(i.FunctionCall)
|
|
|
|
}
|
|
|
|
if i.ToolCalls != nil {
|
|
|
|
marshalAnyRole(i.ToolCalls)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if contentExists {
|
|
|
|
content = fmt.Sprint(i.StringContent)
|
|
|
|
}
|
|
|
|
if i.FunctionCall != nil {
|
|
|
|
marshalAny(i.FunctionCall)
|
|
|
|
}
|
|
|
|
if i.ToolCalls != nil {
|
|
|
|
marshalAny(i.ToolCalls)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Special Handling: System. We care if it was printed at all, not the r branch, so check seperately
|
|
|
|
if contentExists && role == "system" {
|
|
|
|
suppressConfigSystemPrompt = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mess = append(mess, content)
|
|
|
|
}
|
|
|
|
|
2024-05-14 23:17:02 +00:00
|
|
|
joinCharacter := "\n"
|
|
|
|
if config.TemplateConfig.JoinChatMessagesByCharacter != nil {
|
|
|
|
joinCharacter = *config.TemplateConfig.JoinChatMessagesByCharacter
|
|
|
|
}
|
|
|
|
|
|
|
|
predInput = strings.Join(mess, joinCharacter)
|
2024-04-17 21:33:49 +00:00
|
|
|
log.Debug().Msgf("Prompt (before templating): %s", predInput)
|
|
|
|
|
|
|
|
templateFile := ""
|
2024-04-11 17:20:22 +00:00
|
|
|
|
2024-04-17 21:33:49 +00:00
|
|
|
// A model can have a "file.bin.tmpl" file associated with a prompt template prefix
|
|
|
|
if ml.ExistsInModelPath(fmt.Sprintf("%s.tmpl", config.Model)) {
|
|
|
|
templateFile = config.Model
|
|
|
|
}
|
|
|
|
|
2024-04-18 20:43:12 +00:00
|
|
|
if config.TemplateConfig.Chat != "" && !shouldUseFn {
|
2024-04-17 21:33:49 +00:00
|
|
|
templateFile = config.TemplateConfig.Chat
|
|
|
|
}
|
|
|
|
|
2024-04-18 20:43:12 +00:00
|
|
|
if config.TemplateConfig.Functions != "" && shouldUseFn {
|
2024-04-17 21:33:49 +00:00
|
|
|
templateFile = config.TemplateConfig.Functions
|
|
|
|
}
|
|
|
|
|
|
|
|
if templateFile != "" {
|
|
|
|
templatedInput, err := ml.EvaluateTemplateForPrompt(model.ChatPromptTemplate, templateFile, model.PromptTemplateData{
|
|
|
|
SystemPrompt: config.SystemPrompt,
|
|
|
|
SuppressSystemPrompt: suppressConfigSystemPrompt,
|
|
|
|
Input: predInput,
|
|
|
|
Functions: funcs,
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
predInput = templatedInput
|
|
|
|
log.Debug().Msgf("Template found, input modified to: %s", predInput)
|
|
|
|
} else {
|
|
|
|
log.Debug().Msgf("Template failed loading: %s", err.Error())
|
|
|
|
}
|
|
|
|
}
|
2024-01-05 17:04:46 +00:00
|
|
|
|
2024-04-17 21:33:49 +00:00
|
|
|
log.Debug().Msgf("Prompt (after templating): %s", predInput)
|
2024-04-18 20:43:12 +00:00
|
|
|
if shouldUseFn && config.Grammar != "" {
|
2024-04-17 21:33:49 +00:00
|
|
|
log.Debug().Msgf("Grammar: %+v", config.Grammar)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case toStream:
|
|
|
|
|
|
|
|
log.Debug().Msgf("Stream request received")
|
2024-01-05 17:04:46 +00:00
|
|
|
c.Context().SetContentType("text/event-stream")
|
|
|
|
//c.Response().Header.SetContentType(fiber.MIMETextHTMLCharsetUTF8)
|
2024-04-17 21:33:49 +00:00
|
|
|
// c.Set("Content-Type", "text/event-stream")
|
2024-01-05 17:04:46 +00:00
|
|
|
c.Set("Cache-Control", "no-cache")
|
|
|
|
c.Set("Connection", "keep-alive")
|
|
|
|
c.Set("Transfer-Encoding", "chunked")
|
|
|
|
|
2024-04-17 21:33:49 +00:00
|
|
|
responses := make(chan schema.OpenAIResponse)
|
|
|
|
|
2024-04-18 20:43:12 +00:00
|
|
|
if !shouldUseFn {
|
2024-04-17 21:33:49 +00:00
|
|
|
go process(predInput, input, config, ml, responses)
|
|
|
|
} else {
|
|
|
|
go processTools(noActionName, predInput, input, config, ml, responses)
|
|
|
|
}
|
|
|
|
|
2024-01-05 17:04:46 +00:00
|
|
|
c.Context().SetBodyStreamWriter(fasthttp.StreamWriter(func(w *bufio.Writer) {
|
|
|
|
usage := &schema.OpenAIUsage{}
|
2024-02-17 09:00:34 +00:00
|
|
|
toolsCalled := false
|
2024-04-17 21:33:49 +00:00
|
|
|
for ev := range responses {
|
|
|
|
usage = &ev.Usage // Copy a pointer to the latest usage chunk so that the stop message can reference it
|
|
|
|
if len(ev.Choices[0].Delta.ToolCalls) > 0 {
|
2024-02-17 09:00:34 +00:00
|
|
|
toolsCalled = true
|
|
|
|
}
|
2024-01-05 17:04:46 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
enc := json.NewEncoder(&buf)
|
2024-04-17 21:33:49 +00:00
|
|
|
enc.Encode(ev)
|
|
|
|
log.Debug().Msgf("Sending chunk: %s", buf.String())
|
2024-01-05 17:04:46 +00:00
|
|
|
_, err := fmt.Fprintf(w, "data: %v\n", buf.String())
|
|
|
|
if err != nil {
|
2024-04-17 21:33:49 +00:00
|
|
|
log.Debug().Msgf("Sending chunk failed: %v", err)
|
|
|
|
input.Cancel()
|
2024-01-05 17:04:46 +00:00
|
|
|
break
|
|
|
|
}
|
2024-04-17 21:33:49 +00:00
|
|
|
w.Flush()
|
2024-01-05 17:04:46 +00:00
|
|
|
}
|
|
|
|
|
2024-02-17 09:00:34 +00:00
|
|
|
finishReason := "stop"
|
|
|
|
if toolsCalled {
|
|
|
|
finishReason = "tool_calls"
|
2024-04-17 21:33:49 +00:00
|
|
|
} else if toolsCalled && len(input.Tools) == 0 {
|
2024-02-17 09:00:34 +00:00
|
|
|
finishReason = "function_call"
|
|
|
|
}
|
|
|
|
|
2024-01-05 17:04:46 +00:00
|
|
|
resp := &schema.OpenAIResponse{
|
2024-04-17 21:33:49 +00:00
|
|
|
ID: id,
|
|
|
|
Created: created,
|
|
|
|
Model: input.Model, // we have to return what the user sent here, due to OpenAI spec.
|
2024-01-05 17:04:46 +00:00
|
|
|
Choices: []schema.Choice{
|
|
|
|
{
|
2024-02-17 09:00:34 +00:00
|
|
|
FinishReason: finishReason,
|
2024-01-05 17:04:46 +00:00
|
|
|
Index: 0,
|
2024-04-17 21:33:49 +00:00
|
|
|
Delta: &schema.Message{Content: &emptyMessage},
|
2024-01-05 17:04:46 +00:00
|
|
|
}},
|
|
|
|
Object: "chat.completion.chunk",
|
|
|
|
Usage: *usage,
|
|
|
|
}
|
|
|
|
respData, _ := json.Marshal(resp)
|
|
|
|
|
|
|
|
w.WriteString(fmt.Sprintf("data: %s\n\n", respData))
|
|
|
|
w.WriteString("data: [DONE]\n\n")
|
|
|
|
w.Flush()
|
|
|
|
}))
|
2024-04-13 07:45:34 +00:00
|
|
|
return nil
|
2024-04-17 21:33:49 +00:00
|
|
|
|
|
|
|
// no streaming mode
|
|
|
|
default:
|
|
|
|
result, tokenUsage, err := ComputeChoices(input, predInput, config, startupOptions, ml, func(s string, c *[]schema.Choice) {
|
2024-04-18 20:43:12 +00:00
|
|
|
if !shouldUseFn {
|
2024-04-17 21:33:49 +00:00
|
|
|
// no function is called, just reply and use stop as finish reason
|
|
|
|
*c = append(*c, schema.Choice{FinishReason: "stop", Index: 0, Message: &schema.Message{Role: "assistant", Content: &s}})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-18 20:43:12 +00:00
|
|
|
results := functions.ParseFunctionCall(s, config.FunctionsConfig)
|
|
|
|
noActionsToRun := len(results) > 0 && results[0].Name == noActionName || len(results) == 0
|
2024-04-17 21:33:49 +00:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case noActionsToRun:
|
2024-05-14 07:39:20 +00:00
|
|
|
result, err := handleQuestion(config, input, ml, startupOptions, results, s, predInput)
|
2024-04-17 21:33:49 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("error handling question")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
*c = append(*c, schema.Choice{
|
|
|
|
Message: &schema.Message{Role: "assistant", Content: &result}})
|
|
|
|
default:
|
|
|
|
toolChoice := schema.Choice{
|
|
|
|
Message: &schema.Message{
|
|
|
|
Role: "assistant",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(input.Tools) > 0 {
|
|
|
|
toolChoice.FinishReason = "tool_calls"
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ss := range results {
|
2024-04-18 20:43:12 +00:00
|
|
|
name, args := ss.Name, ss.Arguments
|
2024-04-17 21:33:49 +00:00
|
|
|
if len(input.Tools) > 0 {
|
|
|
|
// If we are using tools, we condense the function calls into
|
|
|
|
// a single response choice with all the tools
|
|
|
|
toolChoice.Message.ToolCalls = append(toolChoice.Message.ToolCalls,
|
|
|
|
schema.ToolCall{
|
|
|
|
ID: id,
|
|
|
|
Type: "function",
|
|
|
|
FunctionCall: schema.FunctionCall{
|
|
|
|
Name: name,
|
|
|
|
Arguments: args,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
// otherwise we return more choices directly
|
|
|
|
*c = append(*c, schema.Choice{
|
|
|
|
FinishReason: "function_call",
|
|
|
|
Message: &schema.Message{
|
|
|
|
Role: "assistant",
|
|
|
|
FunctionCall: map[string]interface{}{
|
|
|
|
"name": name,
|
|
|
|
"arguments": args,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(input.Tools) > 0 {
|
|
|
|
// we need to append our result if we are using tools
|
|
|
|
*c = append(*c, toolChoice)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := &schema.OpenAIResponse{
|
|
|
|
ID: id,
|
|
|
|
Created: created,
|
|
|
|
Model: input.Model, // we have to return what the user sent here, due to OpenAI spec.
|
|
|
|
Choices: result,
|
|
|
|
Object: "chat.completion",
|
|
|
|
Usage: schema.OpenAIUsage{
|
|
|
|
PromptTokens: tokenUsage.Prompt,
|
|
|
|
CompletionTokens: tokenUsage.Completion,
|
|
|
|
TotalTokens: tokenUsage.Prompt + tokenUsage.Completion,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
respData, _ := json.Marshal(resp)
|
|
|
|
log.Debug().Msgf("Response: %s", respData)
|
|
|
|
|
|
|
|
// Return the prediction in the response body
|
|
|
|
return c.JSON(resp)
|
2024-01-05 17:04:46 +00:00
|
|
|
}
|
2024-04-17 21:33:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-14 07:39:20 +00:00
|
|
|
func handleQuestion(config *config.BackendConfig, input *schema.OpenAIRequest, ml *model.ModelLoader, o *config.ApplicationConfig, funcResults []functions.FuncCallResults, result, prompt string) (string, error) {
|
|
|
|
|
|
|
|
if len(funcResults) == 0 && result != "" {
|
|
|
|
log.Debug().Msgf("nothing function results but we had a message from the LLM")
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2024-04-17 21:33:49 +00:00
|
|
|
log.Debug().Msgf("nothing to do, computing a reply")
|
2024-04-18 20:43:12 +00:00
|
|
|
arg := ""
|
|
|
|
if len(funcResults) > 0 {
|
|
|
|
arg = funcResults[0].Arguments
|
|
|
|
}
|
2024-04-17 21:33:49 +00:00
|
|
|
// If there is a message that the LLM already sends as part of the JSON reply, use it
|
|
|
|
arguments := map[string]interface{}{}
|
2024-04-18 20:43:12 +00:00
|
|
|
if err := json.Unmarshal([]byte(arg), &arguments); err != nil {
|
|
|
|
log.Debug().Msg("handleQuestion: function result did not contain a valid JSON object")
|
|
|
|
}
|
2024-04-17 21:33:49 +00:00
|
|
|
m, exists := arguments["message"]
|
|
|
|
if exists {
|
|
|
|
switch message := m.(type) {
|
|
|
|
case string:
|
|
|
|
if message != "" {
|
|
|
|
log.Debug().Msgf("Reply received from LLM: %s", message)
|
|
|
|
message = backend.Finetune(*config, prompt, message)
|
|
|
|
log.Debug().Msgf("Reply received from LLM(finetuned): %s", message)
|
|
|
|
|
|
|
|
return message, nil
|
|
|
|
}
|
2024-01-05 17:04:46 +00:00
|
|
|
}
|
2024-04-17 21:33:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug().Msgf("No action received from LLM, without a message, computing a reply")
|
|
|
|
// Otherwise ask the LLM to understand the JSON output and the context, and return a message
|
|
|
|
// Note: This costs (in term of CPU/GPU) another computation
|
|
|
|
config.Grammar = ""
|
|
|
|
images := []string{}
|
|
|
|
for _, m := range input.Messages {
|
|
|
|
images = append(images, m.StringImages...)
|
|
|
|
}
|
2024-02-17 09:00:34 +00:00
|
|
|
|
2024-04-17 21:33:49 +00:00
|
|
|
predFunc, err := backend.ModelInference(input.Context, prompt, input.Messages, images, ml, *config, o, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("model inference failed")
|
|
|
|
return "", err
|
|
|
|
}
|
2024-01-05 17:04:46 +00:00
|
|
|
|
2024-04-17 21:33:49 +00:00
|
|
|
prediction, err := predFunc()
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("prediction failed")
|
|
|
|
return "", err
|
2024-01-05 17:04:46 +00:00
|
|
|
}
|
2024-04-17 21:33:49 +00:00
|
|
|
return backend.Finetune(*config, prompt, prediction.Response), nil
|
|
|
|
}
|