| | package main
|
| |
|
| | import (
|
| | "strings"
|
| | "time"
|
| | )
|
| |
|
| |
|
| | func TransformModelID(modelID string) string {
|
| | parts := strings.Split(modelID, ":")
|
| | return parts[len(parts)-1]
|
| | }
|
| |
|
| | func ToOpenAI(atlasResp AtlassianResponse, modelID string) ChatCompletionResponse {
|
| |
|
| | var usage ChatCompletionUsage
|
| | if atlasResp.PlatformAttributes.Model != "" {
|
| |
|
| | usage = ChatCompletionUsage{
|
| | PromptTokens: nil,
|
| | CompletionTokens: nil,
|
| | TotalTokens: nil,
|
| | }
|
| | }
|
| |
|
| |
|
| | choices := make([]ChatCompletionChoice, len(atlasResp.ResponsePayload.Choices))
|
| | for i, choice := range atlasResp.ResponsePayload.Choices {
|
| |
|
| | var content string
|
| | if len(choice.Message.Content) > 0 {
|
| | content = choice.Message.Content[0].Text
|
| | }
|
| |
|
| | choices[i] = ChatCompletionChoice{
|
| | Index: choice.Index,
|
| | Message: &ChatMessage{
|
| | Role: choice.Message.Role,
|
| | Content: content,
|
| | },
|
| | FinishReason: choice.FinishReason,
|
| | }
|
| | }
|
| |
|
| | return ChatCompletionResponse{
|
| | ID: atlasResp.ResponsePayload.ID,
|
| | Object: "chat.completion",
|
| | Created: atlasResp.ResponsePayload.Created,
|
| | Model: modelID,
|
| | Choices: choices,
|
| | Usage: usage,
|
| | }
|
| | }
|
| |
|
| |
|
| | func ToOpenAIStreamChunk(atlasChunk AtlassianStreamChunk, requestedModel string) ChatCompletionStreamResponse {
|
| | var choices []ChatCompletionChoice
|
| |
|
| | if len(atlasChunk.ResponsePayload.Choices) > 0 {
|
| | choice := atlasChunk.ResponsePayload.Choices[0]
|
| |
|
| | delta := &ChatMessage{}
|
| |
|
| |
|
| | if choice.Message.Role != "" {
|
| | delta.Role = choice.Message.Role
|
| | }
|
| |
|
| |
|
| | if len(choice.Message.Content) > 0 && choice.Message.Content[0].Text != "" {
|
| | delta.Content = choice.Message.Content[0].Text
|
| | }
|
| |
|
| |
|
| | if delta.Role != "" || delta.Content != "" || choice.FinishReason != nil {
|
| | choices = append(choices, ChatCompletionChoice{
|
| | Index: choice.Index,
|
| | Delta: delta,
|
| | FinishReason: choice.FinishReason,
|
| | })
|
| | }
|
| | }
|
| |
|
| |
|
| | id := atlasChunk.ResponsePayload.ID
|
| | if id == "" {
|
| | id = generateChatCompletionID()
|
| | }
|
| |
|
| |
|
| | created := atlasChunk.ResponsePayload.Created
|
| | if created == 0 {
|
| | created = time.Now().Unix()
|
| | }
|
| |
|
| | return ChatCompletionStreamResponse{
|
| | ID: id,
|
| | Object: "chat.completion.chunk",
|
| | Created: created,
|
| | Model: requestedModel,
|
| | Choices: choices,
|
| | }
|
| | }
|
| |
|
| |
|
| | func generateChatCompletionID() string {
|
| | return "chatcmpl-" + string(rune(time.Now().UnixMilli()))
|
| | }
|
| |
|