Skip to content

Commit

Permalink
examples: Use new debugging helper in example (#703)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmc authored Mar 21, 2024
1 parent 491288f commit be54dc1
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 37 deletions.
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
}

0 comments on commit be54dc1

Please sign in to comment.