|
| 1 | +// Copyright 2026 Redpanda Data, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package main |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + |
| 21 | + "github.com/redpanda-data/ai-sdk-go/agent" |
| 22 | + "github.com/redpanda-data/ai-sdk-go/agent/llmagent" |
| 23 | + "github.com/redpanda-data/ai-sdk-go/llm" |
| 24 | + "github.com/redpanda-data/ai-sdk-go/providers/openaicompat" |
| 25 | + "github.com/redpanda-data/ai-sdk-go/store/session" |
| 26 | +) |
| 27 | + |
| 28 | +func main() { |
| 29 | + ctx := context.Background() |
| 30 | + |
| 31 | + // 1. Setup Ollama Provider (OpenAI Compatible) |
| 32 | + // Ollama doesn't require a real API key, but the SDK needs a non-empty string. |
| 33 | + // We point it to the default local Ollama port 11434. |
| 34 | + provider, err := openaicompat.NewProvider("ollama", |
| 35 | + openaicompat.WithBaseURL("http://localhost:11434/v1"), |
| 36 | + ) |
| 37 | + if err != nil { |
| 38 | + panic(fmt.Sprintf("failed to create provider: %v", err)) |
| 39 | + } |
| 40 | + |
| 41 | + // 2. Initialize the Model (using llama3) |
| 42 | + model, err := provider.NewModel("llama3") |
| 43 | + if err != nil { |
| 44 | + panic(fmt.Sprintf("failed to create model: %v", err)) |
| 45 | + } |
| 46 | + |
| 47 | + // 3. Create Agent with placeholders in the system prompt |
| 48 | + prompt := "You are a helpful assistant. Hello {user_name}! Today's date is {current_date}." |
| 49 | + myAgent, err := llmagent.New("local-agent", prompt, model) |
| 50 | + if err != nil { |
| 51 | + panic(fmt.Sprintf("failed to create agent: %v", err)) |
| 52 | + } |
| 53 | + |
| 54 | + // 4. Setup Session with Metadata for templating |
| 55 | + sess := &session.State{ |
| 56 | + Metadata: map[string]any{ |
| 57 | + "user_name": "Felipe", |
| 58 | + }, |
| 59 | + } |
| 60 | + sess.Messages = append(sess.Messages, llm.NewMessage(llm.RoleUser, llm.NewTextPart("Tell me whats the day today accornding to your instructions and a fun fact about Go."))) |
| 61 | + |
| 62 | + // 5. Add Global Instructions via Context |
| 63 | + gctx := agent.ContextWithGlobalInstructions(ctx, "Keep the response extremely short and professional.") |
| 64 | + |
| 65 | + // 6. Run the Agent |
| 66 | + inv := agent.NewInvocationMetadata(sess, myAgent.Info()) |
| 67 | + fmt.Println("--- Running Agent with Ollama ---") |
| 68 | + fmt.Printf("Metadata being sent: user_name=%s\n", |
| 69 | + sess.Metadata["user_name"]) |
| 70 | + for evt, err := range myAgent.Run(gctx, inv) { |
| 71 | + if err != nil { |
| 72 | + fmt.Printf("\nError during execution: %v\n", err) |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + switch e := evt.(type) { |
| 77 | + case agent.AssistantDeltaEvent: |
| 78 | + // Print streaming token deltas |
| 79 | + if e.Delta.Part.IsText() { |
| 80 | + fmt.Print(e.Delta.Part.Text) |
| 81 | + } |
| 82 | + case agent.InvocationEndEvent: |
| 83 | + fmt.Println("\n--- Execution Finished ---") |
| 84 | + fmt.Printf("Finish Reason: %v\n", e.FinishReason) |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments