Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EscapeString #440

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"net/http"
"strconv"
)

// Chat message role defined by the OpenAI API.
Expand All @@ -21,9 +22,15 @@ var (
ErrChatCompletionStreamNotSupported = errors.New("streaming is not supported with this method, please use CreateChatCompletionStream") //nolint:lll
)

type EscapeString string

func (esc EscapeString) MarshalJSON() ([]byte, error) {
return []byte(strconv.QuoteToASCII(string(esc))), nil
}

type ChatCompletionMessage struct {
Role string `json:"role"`
Content string `json:"content"`
Role string `json:"role"`
Content EscapeString `json:"content"`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you adopt a method that doesn't involve breaking changes?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not very familiar with other implementation methods.


// This property isn't in the official documentation, but it's in
// the documentation for the official library for python:
Expand Down
4 changes: 2 additions & 2 deletions chat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,12 +268,12 @@ func handleChatCompletionEndpoint(w http.ResponseWriter, r *http.Request) {
res.Choices = append(res.Choices, ChatCompletionChoice{
Message: ChatCompletionMessage{
Role: ChatMessageRoleAssistant,
Content: completionStr,
Content: EscapeString(completionStr),
},
Index: i,
})
}
inputTokens := numTokens(completionReq.Messages[0].Content) * n
inputTokens := numTokens(string(completionReq.Messages[0].Content)) * n
completionTokens := completionReq.MaxTokens * n
res.Usage = Usage{
PromptTokens: inputTokens,
Expand Down
2 changes: 1 addition & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ func Example_chatbot() {
for s.Scan() {
req.Messages = append(req.Messages, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleUser,
Content: s.Text(),
Content: openai.EscapeString(s.Text()),
})
resp, err := client.CreateChatCompletion(context.Background(), req)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion examples/chatbot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ func main() {
fmt.Print("> ")
s := bufio.NewScanner(os.Stdin)
for s.Scan() {
content := s.Text()
req.Messages = append(req.Messages, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleUser,
Content: s.Text(),
Content: openai.EscapeString(content),
})
resp, err := client.CreateChatCompletion(context.Background(), req)
if err != nil {
Expand Down