|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | +) |
| 8 | + |
| 9 | +//json to go |
| 10 | + |
| 11 | +type Arguments struct { |
| 12 | + DocumentURL []string `json:"document_URL"` |
| 13 | + Tag []string `json:"tag"` |
| 14 | +} |
| 15 | + |
| 16 | +func main() { |
| 17 | + functionCalls := make(map[string]interface{}) |
| 18 | + message := os.Args[1] |
| 19 | + |
| 20 | + i := 0 |
| 21 | + for { |
| 22 | + |
| 23 | + fmt.Printf("---- %d ----\n", i) |
| 24 | + |
| 25 | + fmt.Printf("Input -> %s\n", message) |
| 26 | + |
| 27 | + BuiltMessage := BuildMessages(message) |
| 28 | + |
| 29 | + resp, err := ChatGPT(BuiltMessage) |
| 30 | + if err != nil { |
| 31 | + fmt.Printf("Error executing chat GPT: %v\n", err) |
| 32 | + return |
| 33 | + } |
| 34 | + |
| 35 | + // FunctionCallが生成されてなければここでループを抜ける |
| 36 | + if resp.Choices[0].Message.FunctionCall == nil { |
| 37 | + fmt.Printf("Generated message -> %s\n", resp.Choices[0].Message) |
| 38 | + break |
| 39 | + } |
| 40 | + |
| 41 | + fmt.Printf("Generated function -> %s\n", resp.Choices[0].Message.FunctionCall.Name) |
| 42 | + // fmt.Println(resp.Choices[0].Message.FunctionCall.Arguments) |
| 43 | + |
| 44 | + functionName := resp.Choices[0].Message.FunctionCall.Name |
| 45 | + arguments := resp.Choices[0].Message.FunctionCall.Arguments |
| 46 | + |
| 47 | + // 新しいFunction Callの関数名を辞書に追加 |
| 48 | + functionCalls[functionName] = arguments |
| 49 | + |
| 50 | + // 辞書全体の表示 |
| 51 | + functionMapBytes, err := json.MarshalIndent(functionCalls, "", " ") |
| 52 | + if err != nil { |
| 53 | + fmt.Printf("Error marshalling functionMap: %v\n", err) |
| 54 | + return |
| 55 | + } |
| 56 | + |
| 57 | + // 取り残したFunction Callを生成させるためのメッセージ |
| 58 | + message = fmt.Sprintf(` |
| 59 | +%s |
| 60 | +The above is a generated Function Call. |
| 61 | +It is generated based on the following text. |
| 62 | +--- |
| 63 | +%s |
| 64 | +--- |
| 65 | +Are there any other Function Calls? |
| 66 | +`, string(functionMapBytes), os.Args[1]) |
| 67 | + |
| 68 | + i++ |
| 69 | + |
| 70 | + } |
| 71 | + |
| 72 | + fmt.Println("---- Done ----") |
| 73 | + |
| 74 | + // for ループを抜けた後、functionCalls の内容を表示 |
| 75 | + functionMapBytes, err := json.MarshalIndent(functionCalls, "", " ") |
| 76 | + fmt.Println("results") |
| 77 | + if err != nil { |
| 78 | + fmt.Printf("Error marshalling functionCalls: %v\n", err) |
| 79 | + return |
| 80 | + } |
| 81 | + fmt.Println(string(functionMapBytes)) |
| 82 | + |
| 83 | + // fmt.Println("resp.Choices[0].Message ->", resp.Choices[0].Message) |
| 84 | + |
| 85 | + // // FunctionCallがあるか調べる。 |
| 86 | + // if resp.Choices[0].Message.FunctionCall != nil { |
| 87 | + // // fmt.Println(resp.Choices[0].Message) |
| 88 | + // fmt.Println(resp.Choices[0].Message.FunctionCall.Name) |
| 89 | + // fmt.Println(resp.Choices[0].Message.FunctionCall.Arguments) |
| 90 | + |
| 91 | + // args := &Arguments{} |
| 92 | + // err := json.Unmarshal([]byte(resp.Choices[0].Message.FunctionCall.Arguments), args) |
| 93 | + // if err != nil { |
| 94 | + // fmt.Printf("Error decoding JSON: %v\n", err) |
| 95 | + // return |
| 96 | + // } |
| 97 | + |
| 98 | + // if resp.Choices[0].Message.FunctionCall.Name == "get_official_documents" { |
| 99 | + // for i, url := range args.DocumentURL { |
| 100 | + // fmt.Printf("URL %d: %s\n", i, url) |
| 101 | + |
| 102 | + // fmt.Printf("%s\n", GetRequest(url)) |
| 103 | + // } |
| 104 | + // } |
| 105 | + // } |
| 106 | + // GetRequest("https://www.yahoo.co.jp/") |
| 107 | +} |
0 commit comments