Private
Public Access
1
0

Wrap chunk content in a Chunk type

Preparing to include additional information with each chunk (e.g. token
count)
This commit is contained in:
2024-06-08 23:37:58 +00:00
parent c963747066
commit d2d946b776
8 changed files with 35 additions and 20 deletions

View File

@@ -161,7 +161,7 @@ func (c *AnthropicClient) CreateChatCompletionStream(
params model.RequestParameters,
messages []model.Message,
callback provider.ReplyCallback,
output chan<- string,
output chan<- provider.Chunk,
) (string, error) {
if len(messages) == 0 {
return "", fmt.Errorf("Can't create completion from no messages")
@@ -242,7 +242,9 @@ func (c *AnthropicClient) CreateChatCompletionStream(
return "", fmt.Errorf("invalid text delta")
}
sb.WriteString(text)
output <- text
output <- provider.Chunk{
Content: text,
}
case "content_block_stop":
// ignore?
case "message_delta":
@@ -262,7 +264,9 @@ func (c *AnthropicClient) CreateChatCompletionStream(
}
sb.WriteString(FUNCTION_STOP_SEQUENCE)
output <- FUNCTION_STOP_SEQUENCE
output <- provider.Chunk{
Content: FUNCTION_STOP_SEQUENCE,
}
funcCallXml := content[start:] + FUNCTION_STOP_SEQUENCE

View File

@@ -326,7 +326,7 @@ func (c *Client) CreateChatCompletionStream(
params model.RequestParameters,
messages []model.Message,
callback provider.ReplyCallback,
output chan<- string,
output chan<- provider.Chunk,
) (string, error) {
if len(messages) == 0 {
return "", fmt.Errorf("Can't create completion from no messages")
@@ -393,7 +393,9 @@ func (c *Client) CreateChatCompletionStream(
if part.FunctionCall != nil {
toolCalls = append(toolCalls, *part.FunctionCall)
} else if part.Text != "" {
output <- part.Text
output <- provider.Chunk {
Content: part.Text,
}
content.WriteString(part.Text)
}
}

View File

@@ -132,7 +132,7 @@ func (c *OllamaClient) CreateChatCompletionStream(
params model.RequestParameters,
messages []model.Message,
callback provider.ReplyCallback,
output chan<- string,
output chan<- provider.Chunk,
) (string, error) {
if len(messages) == 0 {
return "", fmt.Errorf("Can't create completion from no messages")
@@ -181,7 +181,9 @@ func (c *OllamaClient) CreateChatCompletionStream(
}
if len(streamResp.Message.Content) > 0 {
output <- streamResp.Message.Content
output <- provider.Chunk{
Content: streamResp.Message.Content,
}
content.WriteString(streamResp.Message.Content)
}
}

View File

@@ -245,7 +245,7 @@ func (c *OpenAIClient) CreateChatCompletionStream(
params model.RequestParameters,
messages []model.Message,
callback provider.ReplyCallback,
output chan<- string,
output chan<- provider.Chunk,
) (string, error) {
if len(messages) == 0 {
return "", fmt.Errorf("Can't create completion from no messages")
@@ -319,7 +319,9 @@ func (c *OpenAIClient) CreateChatCompletionStream(
}
}
if len(delta.Content) > 0 {
output <- delta.Content
output <- provider.Chunk {
Content: delta.Content,
}
content.WriteString(delta.Content)
}
}

View File

@@ -8,6 +8,10 @@ import (
type ReplyCallback func(model.Message)
type Chunk struct {
Content string
}
type ChatCompletionClient interface {
// CreateChatCompletion requests a response to the provided messages.
// Replies are appended to the given replies struct, and the
@@ -26,6 +30,6 @@ type ChatCompletionClient interface {
params model.RequestParameters,
messages []model.Message,
callback ReplyCallback,
output chan<- string,
output chan<- Chunk,
) (string, error)
}