-
Notifications
You must be signed in to change notification settings - Fork 1.3k
docs: add Go example for interactive weather assistant in Step 5 #318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -782,6 +782,107 @@ python weather_assistant.py | |
|
|
||
| </details> | ||
|
|
||
| <details> | ||
| <summary><strong>Go</strong></summary> | ||
|
|
||
| Create `weather-assistant.go`: | ||
|
|
||
| ```go | ||
| package main | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "fmt" | ||
| "log" | ||
| "math/rand" | ||
| "os" | ||
| "strings" | ||
|
|
||
| copilot "github.com/github/copilot-sdk/go" | ||
| ) | ||
|
|
||
| type WeatherParams struct { | ||
| City string `json:"city" jsonschema:"The city name"` | ||
| } | ||
|
|
||
| type WeatherResult struct { | ||
| City string `json:"city"` | ||
| Temperature string `json:"temperature"` | ||
| Condition string `json:"condition"` | ||
| } | ||
|
|
||
| func main() { | ||
| getWeather := copilot.DefineTool( | ||
| "get_weather", | ||
| "Get the current weather for a city", | ||
| func(params WeatherParams, inv copilot.ToolInvocation) (WeatherResult, error) { | ||
| conditions := []string{"sunny", "cloudy", "rainy", "partly cloudy"} | ||
| temp := rand.Intn(30) + 50 | ||
| condition := conditions[rand.Intn(len(conditions))] | ||
| return WeatherResult{ | ||
| City: params.City, | ||
| Temperature: fmt.Sprintf("%d°F", temp), | ||
| Condition: condition, | ||
| }, nil | ||
| }, | ||
| ) | ||
|
|
||
| client := copilot.NewClient(nil) | ||
| if err := client.Start(); err != nil { | ||
| log.Fatal(err) | ||
| } | ||
| defer client.Stop() | ||
|
|
||
| session, err := client.CreateSession(&copilot.SessionConfig{ | ||
| Model: "gpt-4.1", | ||
| Streaming: true, | ||
| Tools: []copilot.Tool{getWeather}, | ||
| }) | ||
| if err != nil { | ||
| log.Fatal(err) | ||
| } | ||
|
|
||
| session.On(func(event copilot.SessionEvent) { | ||
| if event.Type == "assistant.message_delta" { | ||
| fmt.Print(*event.Data.DeltaContent) | ||
| } | ||
| if event.Type == "session.idle" { | ||
| fmt.Println() | ||
| } | ||
| }) | ||
|
|
||
| fmt.Println("🌤️ Weather Assistant (type 'exit' to quit)") | ||
| fmt.Println(" Try: 'What's the weather in Paris?' or 'Compare weather in NYC and LA'\n") | ||
|
|
||
| scanner := bufio.NewScanner(os.Stdin) | ||
| for { | ||
| fmt.Print("You: ") | ||
| if !scanner.Scan() { | ||
| break | ||
| } | ||
|
Comment on lines
+862
to
+864
|
||
| input := scanner.Text() | ||
| if strings.ToLower(input) == "exit" { | ||
| break | ||
| } | ||
|
|
||
| fmt.Print("Assistant: ") | ||
| _, err = session.SendAndWait(copilot.MessageOptions{Prompt: input}, 0) | ||
| if err != nil { | ||
| log.Fatal(err) | ||
| } | ||
|
Comment on lines
+872
to
+875
|
||
| fmt.Println() | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| Run with: | ||
|
|
||
| ```bash | ||
| go run weather-assistant.go | ||
| ``` | ||
|
|
||
| </details> | ||
|
|
||
| <details> | ||
| <summary><strong>.NET</strong></summary> | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
event.Data.DeltaContentis a pointer and may be nil (the Go SDK types mark it asomitempty). Dereferencing it unconditionally can panic at runtime; guard for nil before printing (or handle empty deltas).Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed, though event.Data is a value type (not a pointer), so only DeltaContent needs a nil check.
fix: simplify condition in session event handling for Go example