Skip to content

chore: collect and log token usage #349

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

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/fatih/color v1.16.0
github.com/getkin/kin-openapi v0.123.0
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/gptscript-ai/chat-completion-client v0.0.0-20240502162133-7dabc28eab59
github.com/gptscript-ai/chat-completion-client v0.0.0-20240515050533-bdef9f2226a9
github.com/hexops/autogold/v2 v2.2.1
github.com/jaytaylor/html2text v0.0.0-20230321000545-74c2419ad056
github.com/mholt/archiver/v4 v4.0.0-alpha.8
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gptscript-ai/chat-completion-client v0.0.0-20240502162133-7dabc28eab59 h1:Nda0GDkrmIDiAFHfaXu0eIp6SsBs0/4Zyo87ff3Qcqo=
github.com/gptscript-ai/chat-completion-client v0.0.0-20240502162133-7dabc28eab59/go.mod h1:7P/o6/IWa1KqsntVf68hSnLKuu3+xuqm6lYhch1w4jo=
github.com/gptscript-ai/chat-completion-client v0.0.0-20240515050533-bdef9f2226a9 h1:s6nL/aokB1sJTqVXEjN0zFI5CJa66ubw9g68VTMzEw0=
github.com/gptscript-ai/chat-completion-client v0.0.0-20240515050533-bdef9f2226a9/go.mod h1:7P/o6/IWa1KqsntVf68hSnLKuu3+xuqm6lYhch1w4jo=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
Expand Down
6 changes: 6 additions & 0 deletions pkg/monitor/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type display struct {
dumpState string
callIDMap map[string]string
callLock sync.Mutex
usage types.Usage
}

type livePrinter struct {
Expand Down Expand Up @@ -226,6 +227,10 @@ func (d *display) Event(event runner.Event) {
userSpecifiedToolName: event.CallContext.ToolName,
}

d.usage.PromptTokens += event.Usage.PromptTokens
d.usage.CompletionTokens += event.Usage.CompletionTokens
d.usage.TotalTokens += event.Usage.TotalTokens

switch event.Type {
case runner.EventTypeCallStart:
d.livePrinter.progressStart(currentCall)
Expand Down Expand Up @@ -283,6 +288,7 @@ func (d *display) Stop(output string, err error) {
defer d.callLock.Unlock()

log.Fields("runID", d.dump.ID, "output", output, "err", err).Debugf("Run stopped")
log.Fields("runID", d.dump.ID, "total", d.usage.TotalTokens, "prompt", d.usage.PromptTokens, "completion", d.usage.CompletionTokens).Infof("usage ")
d.dump.Output = output
d.dump.Err = err
if d.dumpState != "" {
Expand Down
9 changes: 9 additions & 0 deletions pkg/mvl/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ func (f formatter) Format(entry *logrus.Entry) ([]byte, error) {
if i, ok := entry.Data["response"]; ok && i != "" {
msg += fmt.Sprintf(" [response=%s]", i)
}
if i, ok := entry.Data["total"]; ok && i != "" {
msg += fmt.Sprintf(" [total=%v]", i)
}
if i, ok := entry.Data["prompt"]; ok && i != "" {
msg += fmt.Sprintf(" [prompt=%v]", i)
}
if i, ok := entry.Data["completion"]; ok && i != "" {
msg += fmt.Sprintf(" [completion=%v]", i)
}
return []byte(fmt.Sprintf("%s %s\n",
entry.Time.Format(time.TimeOnly),
msg)), nil
Expand Down
14 changes: 14 additions & 0 deletions pkg/openai/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,9 @@ func (c *Client) Call(ctx context.Context, messageRequest types.CompletionReques
Model: messageRequest.Model,
Messages: msgs,
MaxTokens: messageRequest.MaxTokens,
StreamOptions: &openai.StreamOptions{
IncludeUsage: true,
},
}

if messageRequest.Temperature == nil {
Expand Down Expand Up @@ -372,17 +375,27 @@ func (c *Client) Call(ctx context.Context, messageRequest types.CompletionReques
}
}

var usage types.Usage
if !cacheResponse {
usage = result.Usage
}

status <- types.CompletionStatus{
CompletionID: id,
Chunks: response,
Response: result,
Usage: usage,
Cached: cacheResponse,
}

return &result, nil
}

func appendMessage(msg types.CompletionMessage, response openai.ChatCompletionStreamResponse) types.CompletionMessage {
msg.Usage.CompletionTokens = types.FirstSet(msg.Usage.CompletionTokens, response.Usage.CompletionTokens)
msg.Usage.PromptTokens = types.FirstSet(msg.Usage.PromptTokens, response.Usage.PromptTokens)
msg.Usage.TotalTokens = types.FirstSet(msg.Usage.TotalTokens, response.Usage.TotalTokens)

if len(response.Choices) == 0 {
return msg
}
Expand Down Expand Up @@ -470,6 +483,7 @@ func (c *Client) call(ctx context.Context, request openai.ChatCompletionRequest,
Object: resp.Object,
Created: resp.Created,
Model: resp.Model,
Usage: resp.Usage,
Choices: []openai.ChatCompletionStreamChoice{
{
Index: resp.Choices[0].Index,
Expand Down
2 changes: 2 additions & 0 deletions pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ type Event struct {
ChatCompletionID string `json:"chatCompletionId,omitempty"`
ChatRequest any `json:"chatRequest,omitempty"`
ChatResponse any `json:"chatResponse,omitempty"`
Usage types.Usage `json:"usage,omitempty"`
ChatResponseCached bool `json:"chatResponseCached,omitempty"`
Content string `json:"content,omitempty"`
}
Expand Down Expand Up @@ -622,6 +623,7 @@ func streamProgress(callCtx *engine.Context, monitor Monitor) (chan<- types.Comp
ChatCompletionID: status.CompletionID,
ChatRequest: status.Request,
ChatResponse: status.Response,
Usage: status.Usage,
ChatResponseCached: status.Cached,
})
}
Expand Down
60 changes: 40 additions & 20 deletions pkg/tests/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,15 +247,17 @@ func TestSubChat(t *testing.T) {
{
"text": "Call chatbot"
}
]
],
"usage": {}
},
{
"role": "user",
"content": [
{
"text": "Hello"
}
]
],
"usage": {}
},
{
"role": "assistant",
Expand All @@ -269,7 +271,8 @@ func TestSubChat(t *testing.T) {
}
}
}
]
],
"usage": {}
}
],
"MaxTokens": 0,
Expand Down Expand Up @@ -312,15 +315,17 @@ func TestSubChat(t *testing.T) {
{
"text": "This is a chatbot"
}
]
],
"usage": {}
},
{
"role": "assistant",
"content": [
{
"text": "Assistant 1"
}
]
],
"usage": {}
}
],
"MaxTokens": 0,
Expand Down Expand Up @@ -370,15 +375,17 @@ func TestSubChat(t *testing.T) {
{
"text": "Call chatbot"
}
]
],
"usage": {}
},
{
"role": "user",
"content": [
{
"text": "Hello"
}
]
],
"usage": {}
},
{
"role": "assistant",
Expand All @@ -392,7 +399,8 @@ func TestSubChat(t *testing.T) {
}
}
}
]
],
"usage": {}
}
],
"MaxTokens": 0,
Expand Down Expand Up @@ -435,31 +443,35 @@ func TestSubChat(t *testing.T) {
{
"text": "This is a chatbot"
}
]
],
"usage": {}
},
{
"role": "assistant",
"content": [
{
"text": "Assistant 1"
}
]
],
"usage": {}
},
{
"role": "user",
"content": [
{
"text": "User 1"
}
]
],
"usage": {}
},
{
"role": "assistant",
"content": [
{
"text": "Assistant 2"
}
]
],
"usage": {}
}
],
"MaxTokens": 0,
Expand Down Expand Up @@ -513,23 +525,26 @@ func TestChat(t *testing.T) {
{
"text": "This is a chatbot"
}
]
],
"usage": {}
},
{
"role": "user",
"content": [
{
"text": "Hello"
}
]
],
"usage": {}
},
{
"role": "assistant",
"content": [
{
"text": "Assistant 1"
}
]
],
"usage": {}
}
],
"MaxTokens": 0,
Expand Down Expand Up @@ -567,39 +582,44 @@ func TestChat(t *testing.T) {
{
"text": "This is a chatbot"
}
]
],
"usage": {}
},
{
"role": "user",
"content": [
{
"text": "Hello"
}
]
],
"usage": {}
},
{
"role": "assistant",
"content": [
{
"text": "Assistant 1"
}
]
],
"usage": {}
},
{
"role": "user",
"content": [
{
"text": "User 1"
}
]
],
"usage": {}
},
{
"role": "assistant",
"content": [
{
"text": "Assistant 2"
}
]
],
"usage": {}
}
],
"MaxTokens": 0,
Expand Down
3 changes: 2 additions & 1 deletion pkg/tests/testdata/TestCase/call1.golden
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
{
"text": "Ask Bob how he is doing and let me know exactly what he said."
}
]
],
"usage": {}
}
],
"MaxTokens": 0,
Expand Down
3 changes: 2 additions & 1 deletion pkg/tests/testdata/TestCase2/call1.golden
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
{
"text": "Ask Bob how he is doing and let me know exactly what he said."
}
]
],
"usage": {}
}
],
"MaxTokens": 0,
Expand Down
6 changes: 4 additions & 2 deletions pkg/tests/testdata/TestChat/call1.golden
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@
{
"text": "This is a chatbot"
}
]
],
"usage": {}
},
{
"role": "user",
"content": [
{
"text": "Hello"
}
]
],
"usage": {}
}
],
"MaxTokens": 0,
Expand Down
Loading