MCPcopy Index your code
hub / github.com/docker/docker-agent / run

Function run

examples/chat/main.go:60–112  ·  view source on GitHub ↗
(ctx context.Context, baseURL, model string, stream bool)

Source from the content-addressed store, hash-verified

58}
59
60func run(ctx context.Context, baseURL, model string, stream bool) error {
61 // The chat server doesn't validate API keys, but the OpenAI SDK
62 // requires *some* string to be passed.
63 client := openai.NewClient(
64 option.WithBaseURL(baseURL),
65 option.WithAPIKey("not-needed"),
66 )
67
68 // Ask the server which agents are exposed and pick a default model
69 // when the user didn't pin one. This also doubles as a connectivity
70 // check.
71 if model == "" {
72 picked, err := pickDefaultModel(ctx, &client)
73 if err != nil {
74 return fmt.Errorf("listing models: %w", err)
75 }
76 model = picked
77 }
78 fmt.Printf("Connected to %s — chatting with %q. Type \"exit\" to quit.\n", baseURL, model)
79
80 // History keeps the conversation going across turns. The chat server
81 // is stateless: it builds a fresh session per request from whatever
82 // messages the client sends, so it's the client's job to remember.
83 var history []openai.ChatCompletionMessageParamUnion
84
85 in := bufio.NewScanner(os.Stdin)
86 in.Buffer(make([]byte, 0, 64*1024), 1024*1024)
87 for {
88 fmt.Print("\n> ")
89 if !in.Scan() {
90 if err := in.Err(); err != nil {
91 return err
92 }
93 fmt.Println()
94 return nil // EOF
95 }
96 userInput := strings.TrimSpace(in.Text())
97 if userInput == "" {
98 continue
99 }
100 if userInput == "exit" || userInput == "quit" {
101 return nil
102 }
103
104 history = append(history, openai.UserMessage(userInput))
105
106 reply, err := chat(ctx, &client, model, history, stream)
107 if err != nil {
108 return err
109 }
110 history = append(history, openai.AssistantMessage(reply))
111 }
112}
113
114// pickDefaultModel queries /v1/models and returns the first agent name
115// the server advertises.

Callers 1

mainFunction · 0.70

Calls 6

pickDefaultModelFunction · 0.85
chatFunction · 0.85
PrintfMethod · 0.80
PrintMethod · 0.80
PrintlnMethod · 0.80
ErrMethod · 0.65

Tested by

no test coverage detected