Skip to content

Commit 062b3a1

Browse files
authored
docs: add Go example for interactive weather assistant in Step 5 (#318)
* docs: add Go example for interactive weather assistant in Step 5 * fix: address review feedback for Go Step 5 example * fix: simplify condition in session event handling for Go example
1 parent 736a848 commit 062b3a1

1 file changed

Lines changed: 107 additions & 0 deletions

File tree

docs/getting-started.md

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,113 @@ python weather_assistant.py
786786

787787
</details>
788788

789+
<details>
790+
<summary><strong>Go</strong></summary>
791+
792+
Create `weather-assistant.go`:
793+
794+
```go
795+
package main
796+
797+
import (
798+
"bufio"
799+
"fmt"
800+
"log"
801+
"math/rand"
802+
"os"
803+
"strings"
804+
805+
copilot "github.com/github/copilot-sdk/go"
806+
)
807+
808+
type WeatherParams struct {
809+
City string `json:"city" jsonschema:"The city name"`
810+
}
811+
812+
type WeatherResult struct {
813+
City string `json:"city"`
814+
Temperature string `json:"temperature"`
815+
Condition string `json:"condition"`
816+
}
817+
818+
func main() {
819+
getWeather := copilot.DefineTool(
820+
"get_weather",
821+
"Get the current weather for a city",
822+
func(params WeatherParams, inv copilot.ToolInvocation) (WeatherResult, error) {
823+
conditions := []string{"sunny", "cloudy", "rainy", "partly cloudy"}
824+
temp := rand.Intn(30) + 50
825+
condition := conditions[rand.Intn(len(conditions))]
826+
return WeatherResult{
827+
City: params.City,
828+
Temperature: fmt.Sprintf("%d°F", temp),
829+
Condition: condition,
830+
}, nil
831+
},
832+
)
833+
834+
client := copilot.NewClient(nil)
835+
if err := client.Start(); err != nil {
836+
log.Fatal(err)
837+
}
838+
defer client.Stop()
839+
840+
session, err := client.CreateSession(&copilot.SessionConfig{
841+
Model: "gpt-4.1",
842+
Streaming: true,
843+
Tools: []copilot.Tool{getWeather},
844+
})
845+
if err != nil {
846+
log.Fatal(err)
847+
}
848+
849+
session.On(func(event copilot.SessionEvent) {
850+
if event.Type == "assistant.message_delta" {
851+
if event.Data.DeltaContent != nil {
852+
fmt.Print(*event.Data.DeltaContent)
853+
}
854+
}
855+
if event.Type == "session.idle" {
856+
fmt.Println()
857+
}
858+
})
859+
860+
fmt.Println("🌤️ Weather Assistant (type 'exit' to quit)")
861+
fmt.Println(" Try: 'What's the weather in Paris?' or 'Compare weather in NYC and LA'\n")
862+
863+
scanner := bufio.NewScanner(os.Stdin)
864+
for {
865+
fmt.Print("You: ")
866+
if !scanner.Scan() {
867+
break
868+
}
869+
input := scanner.Text()
870+
if strings.ToLower(input) == "exit" {
871+
break
872+
}
873+
874+
fmt.Print("Assistant: ")
875+
_, err = session.SendAndWait(copilot.MessageOptions{Prompt: input}, 0)
876+
if err != nil {
877+
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
878+
break
879+
}
880+
fmt.Println()
881+
}
882+
if err := scanner.Err(); err != nil {
883+
fmt.Fprintf(os.Stderr, "Input error: %v\n", err)
884+
}
885+
}
886+
```
887+
888+
Run with:
889+
890+
```bash
891+
go run weather-assistant.go
892+
```
893+
894+
</details>
895+
789896
<details>
790897
<summary><strong>.NET</strong></summary>
791898

0 commit comments

Comments
 (0)