Initilaize
This commit is contained in:
61
pkg/api/api.go
Normal file
61
pkg/api/api.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
|
||||
"github.com/sashabaranov/go-openai"
|
||||
)
|
||||
|
||||
// Client wraps OpenAI API client
|
||||
type Client struct {
|
||||
client *openai.Client
|
||||
model string
|
||||
}
|
||||
|
||||
// NewClient creates a new OpenAI API client
|
||||
func NewClient(apiKey, model string) *Client {
|
||||
openaiConfig := openai.DefaultConfig(apiKey)
|
||||
openaiConfig.BaseURL = "https://api.msh.team/v1"
|
||||
|
||||
return &Client{
|
||||
client: openai.NewClientWithConfig(openaiConfig),
|
||||
model: model,
|
||||
}
|
||||
}
|
||||
|
||||
// SetModel changes the model used for requests
|
||||
func (c *Client) SetModel(model string) {
|
||||
c.model = model
|
||||
}
|
||||
|
||||
// SendChatCompletion sends a chat completion request and returns a stream
|
||||
func (c *Client) SendChatCompletion(messages []openai.ChatCompletionMessage) (*openai.ChatCompletionStream, error) {
|
||||
req := openai.ChatCompletionRequest{
|
||||
Model: c.model,
|
||||
Messages: messages,
|
||||
Stream: true,
|
||||
}
|
||||
|
||||
return c.client.CreateChatCompletionStream(context.Background(), req)
|
||||
}
|
||||
|
||||
// GetNextResponse gets the next response from a stream
|
||||
func (c *Client) GetNextResponse(stream *openai.ChatCompletionStream) (openai.ChatCompletionStreamResponse, error) {
|
||||
resp, err := stream.Recv()
|
||||
if err != nil {
|
||||
if errors.Is(err, io.EOF) {
|
||||
return openai.ChatCompletionStreamResponse{}, io.EOF
|
||||
}
|
||||
return openai.ChatCompletionStreamResponse{}, err
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// CloseStream closes a stream
|
||||
func (c *Client) CloseStream(stream *openai.ChatCompletionStream) {
|
||||
if stream != nil {
|
||||
_ = stream.Close()
|
||||
}
|
||||
}
|
||||
17
pkg/theme/colors.go
Normal file
17
pkg/theme/colors.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package theme
|
||||
|
||||
import "github.com/charmbracelet/lipgloss/v2"
|
||||
|
||||
var (
|
||||
// Primary colors
|
||||
Blue = lipgloss.Color("#61AFEF")
|
||||
Yellow = lipgloss.Color("#E5C07B")
|
||||
Green = lipgloss.Color("#98C379")
|
||||
Red = lipgloss.Color("#E06C75")
|
||||
Gray = lipgloss.Color("#5C6370")
|
||||
White = lipgloss.Color("#ABB2BF")
|
||||
|
||||
// Background colors
|
||||
BgDark = lipgloss.Color("#282C34")
|
||||
BgLight = lipgloss.Color("#3E4451")
|
||||
)
|
||||
Reference in New Issue
Block a user