From be54dc13c8f0894eefd6e68cf181ecd208a0d29f Mon Sep 17 00:00:00 2001 From: Travis Cline Date: Wed, 20 Mar 2024 17:31:45 -0700 Subject: [PATCH] examples: Use new debugging helper in example (#703) --- .../go.mod | 2 +- .../go.sum | 2 + .../openai_completion_example.go | 37 +------------------ 3 files changed, 4 insertions(+), 37 deletions(-) diff --git a/examples/openai-completion-example-with-http-debugging/go.mod b/examples/openai-completion-example-with-http-debugging/go.mod index 0dece5c06..4f8ef92be 100644 --- a/examples/openai-completion-example-with-http-debugging/go.mod +++ b/examples/openai-completion-example-with-http-debugging/go.mod @@ -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 diff --git a/examples/openai-completion-example-with-http-debugging/go.sum b/examples/openai-completion-example-with-http-debugging/go.sum index 0d584cb4d..18c7d99ac 100644 --- a/examples/openai-completion-example-with-http-debugging/go.sum +++ b/examples/openai-completion-example-with-http-debugging/go.sum @@ -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= diff --git a/examples/openai-completion-example-with-http-debugging/openai_completion_example.go b/examples/openai-completion-example-with-http-debugging/openai_completion_example.go index cec62b305..fa2ff466e 100644 --- a/examples/openai-completion-example-with-http-debugging/openai_completion_example.go +++ b/examples/openai-completion-example-with-http-debugging/openai_completion_example.go @@ -5,7 +5,6 @@ import ( "flag" "fmt" "log" - "net/http" "net/http/httputil" "github.com/tmc/langchaingo/llms" @@ -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...) @@ -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 -}