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

examples: Use new debugging helper in example #703

Merged
merged 1 commit into from
Mar 21, 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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.21

toolchain go1.21.4

require github.com/tmc/langchaingo v0.1.7
require github.com/tmc/langchaingo v0.1.8-0.20240321002625-491288f2a685

require (
github.com/dlclark/regexp2 v1.10.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions examples/openai-completion-example-with-http-debugging/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tmc/langchaingo v0.1.7 h1:Jx3/KEUAkCxU0hcNo+WZcXDnCUG/PfjcrW7N+f3ohOw=
github.com/tmc/langchaingo v0.1.7/go.mod h1:lPpWPoAud+yQowJNRZhdtRbQCSHKF+jRxd0gU58GDHU=
github.com/tmc/langchaingo v0.1.8-0.20240321002625-491288f2a685 h1:mG62pEXBSYhpvkKTR8KqSZa/JEjHCjJ2/AMTAmqDfV4=
github.com/tmc/langchaingo v0.1.8-0.20240321002625-491288f2a685/go.mod h1:3msEx024+dmJM7SbSUK9c91Il6Zn6Jg/hjPYAbG4sRY=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"flag"
"fmt"
"log"
"net/http"
"net/http/httputil"

"github.com/tmc/langchaingo/llms"
Expand All @@ -19,11 +18,7 @@ func main() {
flag.Parse()
var opts []openai.Option
if *flagDebugHTTP {
opts = append(opts, openai.WithHTTPClient(&http.Client{
Transport: &logTransport{
Transport: http.DefaultTransport, // Use http.DefaultTransport as the underlying transport
},
}))
opts = append(opts, openai.WithHTTPClient(httputil.DebugHTTPClient))
}

llm, err := openai.New(opts...)
Expand All @@ -42,33 +37,3 @@ func main() {

fmt.Println(completion)
}

// logTransport wraps around an existing http.RoundTripper, allowing us to
// intercept and log the request and response.
type logTransport struct {
Transport http.RoundTripper
}

// RoundTrip executes a single HTTP transaction and logs the request and response.
func (c *logTransport) RoundTrip(req *http.Request) (*http.Response, error) {
// Log the request
requestDump, err := httputil.DumpRequestOut(req, true)
if err == nil {
log.Println("Request:\n" + string(requestDump))
} else {
log.Println("Error dumping request:", err)
}
// Use the underlying Transport to execute the request
resp, err := c.Transport.RoundTrip(req)
if err != nil {
return nil, err // Return early if there's an error
}
// Log the response
responseDump, err := httputil.DumpResponse(resp, true)
if err == nil {
log.Println("Response:\n" + string(responseDump))
} else {
log.Println("Error dumping response:", err)
}
return resp, err
}
Loading