Ashad001 commited on
Commit
a73be9f
·
1 Parent(s): 9130446

base version

Browse files
Files changed (4) hide show
  1. .gitignore +4 -0
  2. go.mod +1 -0
  3. go.sum +2 -0
  4. main.go +39 -7
.gitignore CHANGED
@@ -20,3 +20,7 @@
20
  # Go workspace file
21
  go.work
22
  go.work.sum
 
 
 
 
 
20
  # Go workspace file
21
  go.work
22
  go.work.sum
23
+
24
+
25
+ # secrets
26
+ *.env
go.mod CHANGED
@@ -16,6 +16,7 @@ require (
16
  github.com/go-playground/validator/v10 v10.21.0 // indirect
17
  github.com/goccy/go-json v0.10.3 // indirect
18
  github.com/google/uuid v1.6.0 // indirect
 
19
  github.com/json-iterator/go v1.1.12 // indirect
20
  github.com/klauspost/cpuid/v2 v2.2.8 // indirect
21
  github.com/leodido/go-urn v1.4.0 // indirect
 
16
  github.com/go-playground/validator/v10 v10.21.0 // indirect
17
  github.com/goccy/go-json v0.10.3 // indirect
18
  github.com/google/uuid v1.6.0 // indirect
19
+ github.com/joho/godotenv v1.5.1 // indirect
20
  github.com/json-iterator/go v1.1.12 // indirect
21
  github.com/klauspost/cpuid/v2 v2.2.8 // indirect
22
  github.com/leodido/go-urn v1.4.0 // indirect
go.sum CHANGED
@@ -27,6 +27,8 @@ github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PU
27
  github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
28
  github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
29
  github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
 
 
30
  github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
31
  github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
32
  github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
 
27
  github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
28
  github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
29
  github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
30
+ github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
31
+ github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
32
  github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
33
  github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
34
  github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
main.go CHANGED
@@ -1,24 +1,56 @@
1
  package main
2
 
3
  import (
 
4
  "fmt"
5
  "os"
6
 
 
 
7
  "github.com/tmc/langchaingo/llms/openai"
8
  )
9
 
10
  func main() {
11
- if err := run(); err != nil {
12
- fmt.Println(os.Stderr, err)
 
13
  os.Exit(1)
14
  }
15
-
16
  }
17
 
18
- func run() error {
19
- _, err := openai.New()
 
 
 
 
 
 
 
 
 
 
20
  if err != nil {
21
- return err
22
  }
23
- return nil
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  }
 
1
  package main
2
 
3
  import (
4
+ "context"
5
  "fmt"
6
  "os"
7
 
8
+ "github.com/joho/godotenv"
9
+ "github.com/tmc/langchaingo/llms"
10
  "github.com/tmc/langchaingo/llms/openai"
11
  )
12
 
13
  func main() {
14
+ content, err := run()
15
+ if err != nil {
16
+ fmt.Fprintln(os.Stderr, err)
17
  os.Exit(1)
18
  }
19
+ fmt.Println(content)
20
  }
21
 
22
+ func run() (string, error) {
23
+ err := godotenv.Load()
24
+ if err != nil {
25
+ return "", fmt.Errorf("error loading .env file: %v", err)
26
+ }
27
+
28
+ apiKey := os.Getenv("OPENAI_API_KEY")
29
+ if apiKey == "" {
30
+ return "", fmt.Errorf("OPENAI_API_KEY not set in .env file")
31
+ }
32
+
33
+ llm, err := openai.New()
34
  if err != nil {
35
+ return "", fmt.Errorf("error creating OpenAI client: %v", err)
36
  }
37
+
38
+ fmt.Println("OpenAI client created successfully")
39
+
40
+ ctx := context.Background()
41
+ prompt := "What is GoLang?"
42
+
43
+ content, err := llms.GenerateFromSinglePrompt(
44
+ ctx,
45
+ llm,
46
+ prompt,
47
+ )
48
+
49
+ if err != nil {
50
+ return "", fmt.Errorf("error in generating content: %v", err)
51
+ }
52
+
53
+ fmt.Println("Generated content:", content)
54
+
55
+ return content, nil
56
  }