-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds basic translator and api schemas (#27)
This migrates the translation logic from the PoC code in poc-showcasing branch. Nothing special in here - just the manual translation plus the barebone API definitions for each OpenAI and AWS Bedrock. Just in case anyway curious about why not using OpenAPI generator for the OpenAI part, here's the comment in the apischema/openai package: ``` // Package openai contains the followings are the OpenAI API schema definitions. // Note that we intentionally do not use the code generation tools like OpenAPI Generator not only to keep the code simple // but also because the OpenAI's OpenAPI definition is not compliant with the spec and the existing tools do not work well. ``` Especially, the OpenAPI schema is a bit tricky especially on the streaming=true/false conditionals. Given that only a few endpoints will be supported here anyways, it's not that problematic to have the manually written data structure. --------- Signed-off-by: Takeshi Yoneda <t.y.mathetake@gmail.com>
- Loading branch information
Showing
10 changed files
with
1,253 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package awsbedrock | ||
|
||
import ( | ||
"encoding/json" | ||
"strings" | ||
) | ||
|
||
// ConverseRequest is defined in the AWS Bedrock API: | ||
// https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Converse.html#API_runtime_Converse_RequestBody | ||
type ConverseRequest struct { | ||
Messages []Message `json:"messages,omitempty"` | ||
} | ||
|
||
// Message is defined in the AWS Bedrock API: | ||
// https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Message.html#bedrock-Type-runtime_Message-content | ||
type Message struct { | ||
Role string `json:"role,omitempty"` | ||
Content []ContentBlock `json:"content,omitempty"` | ||
} | ||
|
||
// ContentBlock is defined in the AWS Bedrock API: | ||
// https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ContentBlock.html | ||
type ContentBlock struct { | ||
Text string `json:"text,omitempty"` | ||
} | ||
|
||
// ConverseResponse is defined in the AWS Bedrock API: | ||
// https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Converse.html#API_runtime_Converse_ResponseElements | ||
type ConverseResponse struct { | ||
Output ConverseResponseOutput `json:"output,omitempty"` | ||
Usage TokenUsage `json:"usage,omitempty"` | ||
} | ||
|
||
// ConverseResponseOutput is defined in the AWS Bedrock API: | ||
// https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ConverseOutput.html | ||
type ConverseResponseOutput struct { | ||
Message Message `json:"message,omitempty"` | ||
} | ||
|
||
// TokenUsage is defined in the AWS Bedrock API: | ||
// https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_TokenUsage.html | ||
type TokenUsage struct { | ||
InputTokens int `json:"inputTokens,omitempty"` | ||
OutputTokens int `json:"outputTokens,omitempty"` | ||
TotalTokens int `json:"totalTokens,omitempty"` | ||
} | ||
|
||
// ConverseStreamEvent is the union of all possible event types in the AWS Bedrock API: | ||
// https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ConverseStream.html | ||
type ConverseStreamEvent struct { | ||
ContentBlockIndex int `json:"contentBlockIndex,omitempty"` | ||
Delta *ConverseStreamEventContentBlockDelta `json:"delta,omitempty"` | ||
Role *string `json:"role,omitempty"` | ||
StopReason *string `json:"stopReason,omitempty"` | ||
Usage *TokenUsage `json:"usage,omitempty"` | ||
} | ||
|
||
// String implements fmt.Stringer. | ||
func (c ConverseStreamEvent) String() string { | ||
buf, _ := json.Marshal(c) | ||
return strings.ReplaceAll(string(buf), ",", ", ") | ||
} | ||
|
||
// ConverseStreamEventContentBlockDelta is defined in the AWS Bedrock API: | ||
// https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ContentBlockDelta.html | ||
type ConverseStreamEventContentBlockDelta struct { | ||
Text string `json:"text,omitempty"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// Package openai contains the followings are the OpenAI API schema definitions. | ||
// Note that we intentionally do not use the code generation tools like OpenAPI Generator not only to keep the code simple | ||
// but also because the OpenAI's OpenAPI definition is not compliant with the spec and the existing tools do not work well. | ||
package openai | ||
|
||
import ( | ||
"encoding/json" | ||
"strings" | ||
) | ||
|
||
// ChatCompletionRequest represents a request to /v1/chat/completions. | ||
// https://platform.openai.com/docs/api-reference/chat/create | ||
type ChatCompletionRequest struct { | ||
// Model is described in the OpenAI API documentation: | ||
// https://platform.openai.com/docs/api-reference/chat/create#chat-create-model | ||
Model string `json:"model"` | ||
|
||
// Messages is described in the OpenAI API documentation: | ||
// https://platform.openai.com/docs/api-reference/chat/create#chat-create-messages | ||
Messages []ChatCompletionRequestMessage `json:"messages"` | ||
|
||
// Stream is described in the OpenAI API documentation: | ||
// https://platform.openai.com/docs/api-reference/chat/create#chat-create-stream | ||
Stream bool `json:"stream,omitempty"` | ||
} | ||
|
||
// ChatCompletionRequestMessage represents a message in a ChatCompletionRequest. | ||
// https://platform.openai.com/docs/api-reference/chat/create#chat-create-messages | ||
type ChatCompletionRequestMessage struct { | ||
// Role is the role of the message. The role of the message (whether it represents the user or the AI). | ||
Role string `json:"role,omitempty"` | ||
// Content is the content of the message. Mainly this is a string, but it can be more complex. | ||
Content any `json:"content,omitempty"` | ||
} | ||
|
||
// ChatCompletionResponse represents a response from /v1/chat/completions. | ||
// https://platform.openai.com/docs/api-reference/chat/object | ||
type ChatCompletionResponse struct { | ||
// Choices are described in the OpenAI API documentation: | ||
// https://platform.openai.com/docs/api-reference/chat/object#chat/object-choices | ||
Choices []ChatCompletionResponseChoice `json:"choices,omitempty"` | ||
|
||
// Object is always "chat.completion" for completions. | ||
// https://platform.openai.com/docs/api-reference/chat/object#chat/object-object | ||
Object string `json:"object,omitempty"` | ||
|
||
// Usage is described in the OpenAI API documentation: | ||
// https://platform.openai.com/docs/api-reference/chat/object#chat/object-usage | ||
Usage ChatCompletionResponseUsage `json:"usage,omitempty"` | ||
} | ||
|
||
// ChatCompletionResponseChoice is described in the OpenAI API documentation: | ||
// https://platform.openai.com/docs/api-reference/chat/object#chat/object-choices | ||
type ChatCompletionResponseChoice struct { | ||
// Message is described in the OpenAI API documentation: | ||
// https://platform.openai.com/docs/api-reference/chat/object#chat/object-choices | ||
Message ChatCompletionResponseChoiceMessage `json:"message,omitempty"` | ||
} | ||
|
||
// ChatCompletionResponseChoiceMessage is described in the OpenAI API documentation: | ||
// https://platform.openai.com/docs/api-reference/chat/object#chat/object-choices | ||
type ChatCompletionResponseChoiceMessage struct { | ||
Content *string `json:"content,omitempty"` | ||
Role string `json:"role,omitempty"` | ||
} | ||
|
||
// ChatCompletionResponseUsage is described in the OpenAI API documentation: | ||
// https://platform.openai.com/docs/api-reference/chat/object#chat/object-usage | ||
type ChatCompletionResponseUsage struct { | ||
CompletionTokens int `json:"completion_tokens,omitempty"` | ||
PromptTokens int `json:"prompt_tokens,omitempty"` | ||
TotalTokens int `json:"total_tokens,omitempty"` | ||
} | ||
|
||
// ChatCompletionResponseChunk is described in the OpenAI API documentation: | ||
// https://platform.openai.com/docs/api-reference/chat/streaming#chat-create-messages | ||
type ChatCompletionResponseChunk struct { | ||
// Choices are described in the OpenAI API documentation: | ||
// https://platform.openai.com/docs/api-reference/chat/streaming#chat/streaming-choices | ||
Choices []ChatCompletionResponseChunkChoice `json:"choices,omitempty"` | ||
|
||
// Object is always "chat.completion.chunk" for completions. | ||
// https://platform.openai.com/docs/api-reference/chat/streaming#chat/streaming-object | ||
Object string `json:"object,omitempty"` | ||
|
||
// Usage is described in the OpenAI API documentation: | ||
// https://platform.openai.com/docs/api-reference/chat/streaming#chat/streaming-usage | ||
Usage *ChatCompletionResponseUsage `json:"usage,omitempty"` | ||
} | ||
|
||
// String implements fmt.Stringer. | ||
func (c *ChatCompletionResponseChunk) String() string { | ||
buf, _ := json.Marshal(c) | ||
return strings.ReplaceAll(string(buf), ",", ", ") | ||
} | ||
|
||
// ChatCompletionResponseChunkChoice is described in the OpenAI API documentation: | ||
// https://platform.openai.com/docs/api-reference/chat/streaming#chat/streaming-choices | ||
type ChatCompletionResponseChunkChoice struct { | ||
Delta *ChatCompletionResponseChunkChoiceDelta `json:"delta,omitempty"` | ||
} | ||
|
||
// ChatCompletionResponseChunkChoiceDelta is described in the OpenAI API documentation: | ||
// https://platform.openai.com/docs/api-reference/chat/streaming#chat/streaming-choices | ||
type ChatCompletionResponseChunkChoiceDelta struct { | ||
Content *string `json:"content,omitempty"` | ||
Role *string `json:"role,omitempty"` | ||
} |
Oops, something went wrong.