Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

provider.go 1.5 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "github.com/sashabaranov/go-openai"
  7. )
  8. var client *openai.Client
  9. func init() {
  10. client = openai.NewClient(os.Getenv("OPENAI_API_KEY"))
  11. }
  12. func CallApi(prompt string, options map[string]interface{}, ctx map[string]interface{}) (map[string]interface{}, error) {
  13. // Get config values
  14. // someOption := options["config"].(map[string]interface{})["someOption"].(string)
  15. resp, err := client.CreateChatCompletion(
  16. context.Background(),
  17. openai.ChatCompletionRequest{
  18. Model: openai.GPT4,
  19. Messages: []openai.ChatCompletionMessage{
  20. {
  21. Role: openai.ChatMessageRoleSystem,
  22. Content: "You are a marketer working for a startup called Acme.",
  23. },
  24. {
  25. Role: openai.ChatMessageRoleUser,
  26. Content: prompt,
  27. },
  28. },
  29. },
  30. )
  31. if err != nil {
  32. return nil, fmt.Errorf("ChatCompletion error: %v", err)
  33. }
  34. return map[string]interface{}{
  35. "output": resp.Choices[0].Message.Content,
  36. }, nil
  37. }
  38. func SomeOtherFunction(prompt string, options map[string]interface{}, context map[string]interface{}) (map[string]interface{}, error) {
  39. return CallApi(prompt+"\nWrite in ALL CAPS", options, context)
  40. }
  41. func AsyncProvider(prompt string, options map[string]interface{}, context map[string]interface{}) (map[string]interface{}, error) {
  42. // In Go, we don't have async/await syntax, but we can use goroutines and channels for concurrency
  43. // For this example, we'll just call the regular function
  44. return CallApi(prompt, options, context)
  45. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...