2024-07-25 11:30:49 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package waveai
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
openaiapi "github.com/sashabaranov/go-openai"
|
|
|
|
"github.com/wavetermdev/thenextwave/pkg/wavebase"
|
2024-08-09 03:24:54 +02:00
|
|
|
"github.com/wavetermdev/thenextwave/pkg/wcloud"
|
2024-07-25 11:30:49 +02:00
|
|
|
"github.com/wavetermdev/thenextwave/pkg/wshrpc"
|
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
)
|
|
|
|
|
|
|
|
const OpenAIPacketStr = "openai"
|
|
|
|
const OpenAICloudReqStr = "openai-cloudreq"
|
|
|
|
const PacketEOFStr = "EOF"
|
|
|
|
|
|
|
|
type OpenAICmdInfoPacketOutputType struct {
|
|
|
|
Model string `json:"model,omitempty"`
|
|
|
|
Created int64 `json:"created,omitempty"`
|
|
|
|
FinishReason string `json:"finish_reason,omitempty"`
|
|
|
|
Message string `json:"message,omitempty"`
|
|
|
|
Error string `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
2024-07-26 22:30:11 +02:00
|
|
|
func MakeOpenAIPacket() *wshrpc.OpenAIPacketType {
|
|
|
|
return &wshrpc.OpenAIPacketType{Type: OpenAIPacketStr}
|
2024-07-25 11:30:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type OpenAICmdInfoChatMessage struct {
|
|
|
|
MessageID int `json:"messageid"`
|
|
|
|
IsAssistantResponse bool `json:"isassistantresponse,omitempty"`
|
|
|
|
AssistantResponse *OpenAICmdInfoPacketOutputType `json:"assistantresponse,omitempty"`
|
|
|
|
UserQuery string `json:"userquery,omitempty"`
|
|
|
|
UserEngineeredQuery string `json:"userengineeredquery,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type OpenAICloudReqPacketType struct {
|
2024-07-26 22:30:11 +02:00
|
|
|
Type string `json:"type"`
|
|
|
|
ClientId string `json:"clientid"`
|
|
|
|
Prompt []wshrpc.OpenAIPromptMessageType `json:"prompt"`
|
|
|
|
MaxTokens int `json:"maxtokens,omitempty"`
|
|
|
|
MaxChoices int `json:"maxchoices,omitempty"`
|
2024-07-25 11:30:49 +02:00
|
|
|
}
|
|
|
|
|
2024-07-29 22:21:44 +02:00
|
|
|
type OpenAIOptsType struct {
|
|
|
|
Model string `json:"model"`
|
|
|
|
APIToken string `json:"apitoken"`
|
|
|
|
BaseURL string `json:"baseurl,omitempty"`
|
|
|
|
MaxTokens int `json:"maxtokens,omitempty"`
|
|
|
|
MaxChoices int `json:"maxchoices,omitempty"`
|
|
|
|
Timeout int `json:"timeout,omitempty"`
|
|
|
|
BlockId string `json:"blockid"`
|
|
|
|
}
|
|
|
|
|
2024-07-25 11:30:49 +02:00
|
|
|
func MakeOpenAICloudReqPacket() *OpenAICloudReqPacketType {
|
|
|
|
return &OpenAICloudReqPacketType{
|
|
|
|
Type: OpenAICloudReqStr,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetWSEndpoint() string {
|
|
|
|
return PCloudWSEndpoint
|
|
|
|
if !wavebase.IsDevMode() {
|
|
|
|
return PCloudWSEndpoint
|
|
|
|
} else {
|
|
|
|
endpoint := os.Getenv(PCloudWSEndpointVarName)
|
|
|
|
if endpoint == "" {
|
|
|
|
panic("Invalid PCloud ws dev endpoint, PCLOUD_WS_ENDPOINT not set or invalid")
|
|
|
|
}
|
|
|
|
return endpoint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const DefaultMaxTokens = 1000
|
2024-08-01 09:57:06 +02:00
|
|
|
const DefaultModel = "gpt-3.5-turbo"
|
2024-07-25 11:30:49 +02:00
|
|
|
const DefaultStreamChanSize = 10
|
|
|
|
const PCloudWSEndpoint = "wss://wsapi.waveterm.dev/"
|
|
|
|
const PCloudWSEndpointVarName = "PCLOUD_WS_ENDPOINT"
|
|
|
|
|
|
|
|
const CloudWebsocketConnectTimeout = 1 * time.Minute
|
|
|
|
|
2024-07-26 22:30:11 +02:00
|
|
|
func convertUsage(resp openaiapi.ChatCompletionResponse) *wshrpc.OpenAIUsageType {
|
2024-07-25 11:30:49 +02:00
|
|
|
if resp.Usage.TotalTokens == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2024-07-26 22:30:11 +02:00
|
|
|
return &wshrpc.OpenAIUsageType{
|
2024-07-25 11:30:49 +02:00
|
|
|
PromptTokens: resp.Usage.PromptTokens,
|
|
|
|
CompletionTokens: resp.Usage.CompletionTokens,
|
|
|
|
TotalTokens: resp.Usage.TotalTokens,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-26 22:30:11 +02:00
|
|
|
func ConvertPrompt(prompt []wshrpc.OpenAIPromptMessageType) []openaiapi.ChatCompletionMessage {
|
2024-07-25 11:30:49 +02:00
|
|
|
var rtn []openaiapi.ChatCompletionMessage
|
|
|
|
for _, p := range prompt {
|
|
|
|
msg := openaiapi.ChatCompletionMessage{Role: p.Role, Content: p.Content, Name: p.Name}
|
|
|
|
rtn = append(rtn, msg)
|
|
|
|
}
|
|
|
|
return rtn
|
|
|
|
}
|
|
|
|
|
2024-08-09 03:24:54 +02:00
|
|
|
func makeAIError(err error) wshrpc.RespOrErrorUnion[wshrpc.OpenAIPacketType] {
|
|
|
|
return wshrpc.RespOrErrorUnion[wshrpc.OpenAIPacketType]{Error: err}
|
|
|
|
}
|
|
|
|
|
2024-07-26 22:30:11 +02:00
|
|
|
func RunCloudCompletionStream(ctx context.Context, request wshrpc.OpenAiStreamRequest) chan wshrpc.RespOrErrorUnion[wshrpc.OpenAIPacketType] {
|
|
|
|
rtn := make(chan wshrpc.RespOrErrorUnion[wshrpc.OpenAIPacketType])
|
2024-08-09 03:24:54 +02:00
|
|
|
wsEndpoint := wcloud.GetWSEndpoint()
|
2024-07-25 11:30:49 +02:00
|
|
|
go func() {
|
|
|
|
defer close(rtn)
|
2024-08-09 03:24:54 +02:00
|
|
|
if wsEndpoint == "" {
|
|
|
|
rtn <- makeAIError(fmt.Errorf("no cloud ws endpoint found"))
|
|
|
|
return
|
|
|
|
}
|
2024-07-25 11:30:49 +02:00
|
|
|
if request.Opts == nil {
|
2024-08-09 03:24:54 +02:00
|
|
|
rtn <- makeAIError(fmt.Errorf("no openai opts found"))
|
2024-07-25 11:30:49 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
websocketContext, dialCancelFn := context.WithTimeout(context.Background(), CloudWebsocketConnectTimeout)
|
|
|
|
defer dialCancelFn()
|
2024-08-09 03:24:54 +02:00
|
|
|
conn, _, err := websocket.DefaultDialer.DialContext(websocketContext, wsEndpoint, nil)
|
2024-07-25 11:30:49 +02:00
|
|
|
if err == context.DeadlineExceeded {
|
2024-08-09 03:24:54 +02:00
|
|
|
rtn <- makeAIError(fmt.Errorf("OpenAI request, timed out connecting to cloud server: %v", err))
|
2024-07-25 11:30:49 +02:00
|
|
|
return
|
|
|
|
} else if err != nil {
|
2024-08-09 03:24:54 +02:00
|
|
|
rtn <- makeAIError(fmt.Errorf("OpenAI request, websocket connect error: %v", err))
|
2024-07-25 11:30:49 +02:00
|
|
|
return
|
|
|
|
}
|
2024-07-26 22:30:11 +02:00
|
|
|
defer func() {
|
|
|
|
err = conn.Close()
|
|
|
|
if err != nil {
|
2024-08-09 03:24:54 +02:00
|
|
|
rtn <- makeAIError(fmt.Errorf("unable to close openai channel: %v", err))
|
2024-07-26 22:30:11 +02:00
|
|
|
}
|
|
|
|
}()
|
2024-07-25 11:30:49 +02:00
|
|
|
reqPk := MakeOpenAICloudReqPacket()
|
|
|
|
reqPk.ClientId = request.ClientId
|
|
|
|
reqPk.Prompt = request.Prompt
|
|
|
|
reqPk.MaxTokens = request.Opts.MaxTokens
|
|
|
|
reqPk.MaxChoices = request.Opts.MaxChoices
|
|
|
|
configMessageBuf, err := json.Marshal(reqPk)
|
|
|
|
if err != nil {
|
2024-08-09 03:24:54 +02:00
|
|
|
rtn <- makeAIError(fmt.Errorf("OpenAI request, packet marshal error: %v", err))
|
2024-07-25 11:30:49 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
err = conn.WriteMessage(websocket.TextMessage, configMessageBuf)
|
|
|
|
if err != nil {
|
2024-08-09 03:24:54 +02:00
|
|
|
rtn <- makeAIError(fmt.Errorf("OpenAI request, websocket write config error: %v", err))
|
2024-07-25 11:30:49 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
_, socketMessage, err := conn.ReadMessage()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("err received: %v", err)
|
2024-08-09 03:24:54 +02:00
|
|
|
rtn <- makeAIError(fmt.Errorf("OpenAI request, websocket error reading message: %v", err))
|
2024-07-25 11:30:49 +02:00
|
|
|
break
|
|
|
|
}
|
2024-07-26 22:30:11 +02:00
|
|
|
var streamResp *wshrpc.OpenAIPacketType
|
2024-07-25 11:30:49 +02:00
|
|
|
err = json.Unmarshal(socketMessage, &streamResp)
|
|
|
|
if err != nil {
|
2024-08-09 03:24:54 +02:00
|
|
|
rtn <- makeAIError(fmt.Errorf("OpenAI request, websocket response json decode error: %v", err))
|
2024-07-25 11:30:49 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
if streamResp.Error == PacketEOFStr {
|
|
|
|
// got eof packet from socket
|
|
|
|
break
|
|
|
|
} else if streamResp.Error != "" {
|
|
|
|
// use error from server directly
|
2024-08-09 03:24:54 +02:00
|
|
|
rtn <- makeAIError(fmt.Errorf("%v", streamResp.Error))
|
2024-07-25 11:30:49 +02:00
|
|
|
break
|
|
|
|
}
|
2024-07-26 22:30:11 +02:00
|
|
|
rtn <- wshrpc.RespOrErrorUnion[wshrpc.OpenAIPacketType]{Response: *streamResp}
|
2024-07-25 11:30:49 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
return rtn
|
|
|
|
}
|
|
|
|
|
2024-07-26 22:30:11 +02:00
|
|
|
func RunLocalCompletionStream(ctx context.Context, request wshrpc.OpenAiStreamRequest) chan wshrpc.RespOrErrorUnion[wshrpc.OpenAIPacketType] {
|
|
|
|
rtn := make(chan wshrpc.RespOrErrorUnion[wshrpc.OpenAIPacketType])
|
2024-07-25 11:30:49 +02:00
|
|
|
go func() {
|
|
|
|
defer close(rtn)
|
|
|
|
if request.Opts == nil {
|
2024-07-26 22:30:11 +02:00
|
|
|
rtn <- wshrpc.RespOrErrorUnion[wshrpc.OpenAIPacketType]{Error: fmt.Errorf("no openai opts found")}
|
2024-07-25 11:30:49 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if request.Opts.Model == "" {
|
2024-07-26 22:30:11 +02:00
|
|
|
rtn <- wshrpc.RespOrErrorUnion[wshrpc.OpenAIPacketType]{Error: fmt.Errorf("no openai model specified")}
|
2024-07-25 11:30:49 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if request.Opts.BaseURL == "" && request.Opts.APIToken == "" {
|
2024-07-26 22:30:11 +02:00
|
|
|
rtn <- wshrpc.RespOrErrorUnion[wshrpc.OpenAIPacketType]{Error: fmt.Errorf("no api token")}
|
2024-07-25 11:30:49 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
clientConfig := openaiapi.DefaultConfig(request.Opts.APIToken)
|
|
|
|
if request.Opts.BaseURL != "" {
|
|
|
|
clientConfig.BaseURL = request.Opts.BaseURL
|
|
|
|
}
|
|
|
|
client := openaiapi.NewClientWithConfig(clientConfig)
|
|
|
|
req := openaiapi.ChatCompletionRequest{
|
|
|
|
Model: request.Opts.Model,
|
|
|
|
Messages: ConvertPrompt(request.Prompt),
|
|
|
|
MaxTokens: request.Opts.MaxTokens,
|
|
|
|
Stream: true,
|
|
|
|
}
|
|
|
|
if request.Opts.MaxChoices > 1 {
|
|
|
|
req.N = request.Opts.MaxChoices
|
|
|
|
}
|
|
|
|
apiResp, err := client.CreateChatCompletionStream(ctx, req)
|
|
|
|
if err != nil {
|
2024-07-26 22:30:11 +02:00
|
|
|
rtn <- wshrpc.RespOrErrorUnion[wshrpc.OpenAIPacketType]{Error: fmt.Errorf("error calling openai API: %v", err)}
|
2024-07-25 11:30:49 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
sentHeader := false
|
|
|
|
for {
|
|
|
|
streamResp, err := apiResp.Recv()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("err received2: %v", err)
|
2024-07-26 22:30:11 +02:00
|
|
|
rtn <- wshrpc.RespOrErrorUnion[wshrpc.OpenAIPacketType]{Error: fmt.Errorf("OpenAI request, websocket error reading message: %v", err)}
|
2024-07-25 11:30:49 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
if streamResp.Model != "" && !sentHeader {
|
|
|
|
pk := MakeOpenAIPacket()
|
|
|
|
pk.Model = streamResp.Model
|
|
|
|
pk.Created = streamResp.Created
|
2024-07-26 22:30:11 +02:00
|
|
|
rtn <- wshrpc.RespOrErrorUnion[wshrpc.OpenAIPacketType]{Response: *pk}
|
2024-07-25 11:30:49 +02:00
|
|
|
sentHeader = true
|
|
|
|
}
|
|
|
|
for _, choice := range streamResp.Choices {
|
|
|
|
pk := MakeOpenAIPacket()
|
|
|
|
pk.Index = choice.Index
|
|
|
|
pk.Text = choice.Delta.Content
|
|
|
|
pk.FinishReason = string(choice.FinishReason)
|
2024-07-26 22:30:11 +02:00
|
|
|
rtn <- wshrpc.RespOrErrorUnion[wshrpc.OpenAIPacketType]{Response: *pk}
|
2024-07-25 11:30:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return rtn
|
|
|
|
}
|
|
|
|
|
2024-07-26 22:30:11 +02:00
|
|
|
func marshalResponse(resp openaiapi.ChatCompletionResponse) []*wshrpc.OpenAIPacketType {
|
|
|
|
var rtn []*wshrpc.OpenAIPacketType
|
2024-07-25 11:30:49 +02:00
|
|
|
headerPk := MakeOpenAIPacket()
|
|
|
|
headerPk.Model = resp.Model
|
|
|
|
headerPk.Created = resp.Created
|
|
|
|
headerPk.Usage = convertUsage(resp)
|
|
|
|
rtn = append(rtn, headerPk)
|
|
|
|
for _, choice := range resp.Choices {
|
|
|
|
choicePk := MakeOpenAIPacket()
|
|
|
|
choicePk.Index = choice.Index
|
|
|
|
choicePk.Text = choice.Message.Content
|
|
|
|
choicePk.FinishReason = string(choice.FinishReason)
|
|
|
|
rtn = append(rtn, choicePk)
|
|
|
|
}
|
|
|
|
return rtn
|
|
|
|
}
|
|
|
|
|
2024-07-26 22:30:11 +02:00
|
|
|
func CreateErrorPacket(errStr string) *wshrpc.OpenAIPacketType {
|
2024-07-25 11:30:49 +02:00
|
|
|
errPk := MakeOpenAIPacket()
|
|
|
|
errPk.FinishReason = "error"
|
|
|
|
errPk.Error = errStr
|
|
|
|
return errPk
|
|
|
|
}
|
|
|
|
|
2024-07-26 22:30:11 +02:00
|
|
|
func CreateTextPacket(text string) *wshrpc.OpenAIPacketType {
|
2024-07-25 11:30:49 +02:00
|
|
|
pk := MakeOpenAIPacket()
|
|
|
|
pk.Text = text
|
|
|
|
return pk
|
|
|
|
}
|