-
-
Notifications
You must be signed in to change notification settings - Fork 632
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
344 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
examples/openai-completion-example-with-http-debugging/go.mod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module github.com/tmc/langchaingo/examples/openai-completion-example-with-http-debugging | ||
|
||
go 1.21 | ||
|
||
toolchain go1.21.4 | ||
|
||
require github.com/tmc/langchaingo v0.1.4-alpha.0 | ||
|
||
require ( | ||
github.com/dlclark/regexp2 v1.8.1 // indirect | ||
github.com/google/uuid v1.4.0 // indirect | ||
github.com/pkoukk/tiktoken-go v0.1.2 // indirect | ||
) |
16 changes: 16 additions & 0 deletions
16
examples/openai-completion-example-with-http-debugging/go.sum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/dlclark/regexp2 v1.8.1 h1:6Lcdwya6GjPUNsBct8Lg/yRPwMhABj269AAzdGSiR+0= | ||
github.com/dlclark/regexp2 v1.8.1/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= | ||
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4= | ||
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= | ||
github.com/pkoukk/tiktoken-go v0.1.2 h1:u7PCSBiWJ3nJYoTGShyM9iHXz4dNyYkurwwp+GHtyHY= | ||
github.com/pkoukk/tiktoken-go v0.1.2/go.mod h1:boMWvk9pQCOTx11pgu0DrIdrAKgQzzJKUP6vLXaz7Rw= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= | ||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= | ||
github.com/tmc/langchaingo v0.1.4-alpha.0 h1:WVu2KgHr9wloHAiMbbeytiv2W76vpkA59uUwf0EUgrQ= | ||
github.com/tmc/langchaingo v0.1.4-alpha.0/go.mod h1:PKtJMXizDxJnT86q7lsVsyzTJqd0P2QKF7wt2jF6Lxk= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
74 changes: 74 additions & 0 deletions
74
examples/openai-completion-example-with-http-debugging/openai_completion_example.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"net/http/httputil" | ||
|
||
"github.com/tmc/langchaingo/llms" | ||
"github.com/tmc/langchaingo/llms/openai" | ||
) | ||
|
||
var flagDebugHTTP = flag.Bool("debug-http", true, "enable debugging of HTTP requests and responses") | ||
|
||
func main() { | ||
// Demonstrates how to use a custom HTTP client to log requests and responses. | ||
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 | ||
}, | ||
})) | ||
} | ||
|
||
llm, err := openai.New(opts...) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
ctx := context.Background() | ||
completion, err := llms.GenerateFromSinglePrompt(ctx, | ||
llm, | ||
"The first man to walk on the moon", | ||
llms.WithTemperature(0.0), | ||
) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.