Skip to content

Commit

Permalink
OpenAI - Added Support for Token Callback / Bug Fixes (newrelic#875)
Browse files Browse the repository at this point in the history
* Bug Fixes for OpenAI
  • Loading branch information
mirackara authored Mar 25, 2024
1 parent 3480bb1 commit 67c0a92
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/newrelic/go-agent/v3/integrations/nropenai"
"github.com/newrelic/go-agent/v3/newrelic"
"github.com/pkoukk/tiktoken-go"
openai "github.com/sashabaranov/go-openai"
)

Expand All @@ -25,6 +26,34 @@ func main() {
}
app.WaitForConnection(10 * time.Second)

// SetLLMTokenCountCallback allows for custom token counting, if left unset and if newrelic.ConfigAIMonitoringRecordContentEnabled()
// is disabled, no token counts will be reported
app.SetLLMTokenCountCallback(func(modelName string, content string) int {
var tokensPerMessage, tokensPerName int
switch modelName {
case "gpt-3.5-turbo-0613",
"gpt-3.5-turbo-16k-0613",
"gpt-4-0314",
"gpt-4-32k-0314",
"gpt-4-0613",
"gpt-4-32k-0613":
tokensPerMessage = 3
tokensPerName = 1
case "gpt-3.5-turbo-0301":
tokensPerMessage = 4
tokensPerName = -1
}

tkm, err := tiktoken.EncodingForModel(modelName)
if err != nil {
fmt.Println("error getting tokens", err)
return 0
}
token := tkm.Encode(content, nil, nil)
totalTokens := len(token) + tokensPerMessage + tokensPerName
return totalTokens
})

// OpenAI Config - Additionally, NRDefaultAzureConfig(apiKey, baseURL string) can be used for Azure
cfg := nropenai.NRDefaultConfig(os.Getenv("OPEN_AI_API_KEY"))

Expand All @@ -40,13 +69,13 @@ func main() {

// GPT Request
req := openai.ChatCompletionRequest{
Model: openai.GPT3Dot5Turbo,
Model: openai.GPT4,
Temperature: 0.7,
MaxTokens: 150,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: "What is 8*5",
Content: "What is Observability in Software Engineering?",
},
},
}
Expand All @@ -56,8 +85,9 @@ func main() {
if err != nil {
panic(err)
}

fmt.Println(resp.ChatCompletionResponse.Choices[0].Message.Content)
if len(resp.ChatCompletionResponse.Choices) == 0 {
fmt.Println("No choices returned")
}

// Shutdown Application
app.Shutdown(5 * time.Second)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func main() {
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: "What is 8*5",
Content: "What is observability in software engineering?",
},
},
}
Expand Down
25 changes: 25 additions & 0 deletions v3/integrations/nropenai/examples/embeddings/embeddings_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/newrelic/go-agent/v3/integrations/nropenai"
"github.com/newrelic/go-agent/v3/newrelic"
"github.com/pkoukk/tiktoken-go"
openai "github.com/sashabaranov/go-openai"
)

Expand All @@ -23,7 +24,31 @@ func main() {
panic(err)
}
app.WaitForConnection(10 * time.Second)
app.SetLLMTokenCountCallback(func(modelName string, content string) int {
var tokensPerMessage, tokensPerName int
switch modelName {
case "gpt-3.5-turbo-0613",
"gpt-3.5-turbo-16k-0613",
"gpt-4-0314",
"gpt-4-32k-0314",
"gpt-4-0613",
"gpt-4-32k-0613":
tokensPerMessage = 3
tokensPerName = 1
case "gpt-3.5-turbo-0301":
tokensPerMessage = 4
tokensPerName = -1
}

tkm, err := tiktoken.EncodingForModel(modelName)
if err != nil {
fmt.Println("error getting tokens", err)
return 0
}
token := tkm.Encode(content, nil, nil)
totalTokens := len(token) + tokensPerMessage + tokensPerName
return totalTokens
})
// OpenAI Config - Additionally, NRDefaultAzureConfig(apiKey, baseURL string) can be used for Azure
cfg := nropenai.NRDefaultConfig(os.Getenv("OPEN_AI_API_KEY"))

Expand Down
4 changes: 3 additions & 1 deletion v3/integrations/nropenai/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ go 1.21.0
require (
github.com/google/uuid v1.6.0
github.com/newrelic/go-agent/v3 v3.30.0
github.com/pkoukk/tiktoken-go v0.1.6
github.com/sashabaranov/go-openai v1.20.2
)

require (
github.com/dlclark/regexp2 v1.10.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
golang.org/x/net v0.9.0 // indirect
golang.org/x/sys v0.7.0 // indirect
Expand All @@ -18,4 +20,4 @@ require (
google.golang.org/protobuf v1.30.0 // indirect
)

replace github.com/newrelic/go-agent/v3 => ../..
replace github.com/newrelic/go-agent/v3 => ../..
Loading

0 comments on commit 67c0a92

Please sign in to comment.