-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04_conversation.exs
More file actions
193 lines (148 loc) · 6.53 KB
/
04_conversation.exs
File metadata and controls
193 lines (148 loc) · 6.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env elixir
# Nous AI - Conversation (v0.8.0)
# Multi-turn conversations with context continuation
IO.puts("=== Nous AI - Conversation Demo ===\n")
agent = Nous.new("lmstudio:qwen3",
instructions: "You are a friendly assistant. Remember our conversation."
)
# ============================================================================
# Method 1: Context Continuation (v0.8.0 - Recommended)
# ============================================================================
IO.puts("--- Method 1: Context Continuation (v0.8.0) ---\n")
# First message
{:ok, result1} = Nous.run(agent, "Hi! My name is Alice.")
IO.puts("User: Hi! My name is Alice.")
IO.puts("Assistant: #{result1.output}\n")
# Follow-up using context: option - carries forward all state
{:ok, result2} = Nous.run(agent, "What's my name?", context: result1.context)
IO.puts("User: What's my name?")
IO.puts("Assistant: #{result2.output}\n")
# Continue the conversation
{:ok, result3} = Nous.run(agent, "I'm working on an Elixir project.", context: result2.context)
IO.puts("User: I'm working on an Elixir project.")
IO.puts("Assistant: #{result3.output}\n")
# The context carries all conversation history
{:ok, result4} = Nous.run(agent, "What do you know about me?", context: result3.context)
IO.puts("User: What do you know about me?")
IO.puts("Assistant: #{result4.output}\n")
IO.puts("Conversation length: #{length(result4.context.messages)} messages")
IO.puts("")
# ============================================================================
# Method 2: Message History (Legacy, still supported)
# ============================================================================
IO.puts("--- Method 2: Message History (Legacy) ---\n")
# Start fresh
{:ok, r1} = Nous.run(agent, "My favorite color is blue.")
IO.puts("User: My favorite color is blue.")
IO.puts("Assistant: #{r1.output}\n")
# Pass message_history explicitly
{:ok, r2} = Nous.run(agent, "What's my favorite color?", message_history: r1.new_messages)
IO.puts("User: What's my favorite color?")
IO.puts("Assistant: #{r2.output}\n")
# ============================================================================
# Method 3: Custom Message List
# ============================================================================
IO.puts("--- Method 3: Custom Messages ---\n")
alias Nous.Message
# Build a message list directly
messages = [
Message.system("You are a math tutor."),
Message.user("What is the square root of 144?"),
Message.assistant("The square root of 144 is 12."),
Message.user("And what's 12 squared?")
]
{:ok, result} = Nous.run(agent, messages: messages)
IO.puts("Continuing from custom history...")
IO.puts("Assistant: #{result.output}\n")
# ============================================================================
# Method 4: Multimodal Messages (Images)
# ============================================================================
IO.puts("--- Method 4: Multimodal Messages (Images) ---\n")
alias Nous.Message.ContentPart
# For models that support vision (Claude, GPT-4V, etc.)
# Note: LMStudio/local models may not support images
# Image from URL
image_message = Message.user([
ContentPart.text("What do you see in this image?"),
ContentPart.image_url("https://upload.wikimedia.org/wikipedia/commons/thumb/4/04/Elixir_logo.png/180px-Elixir_logo.png")
])
IO.puts("Created message with image URL:")
IO.puts(" Text: #{Message.extract_text(image_message)}")
IO.puts(" Has image: yes\n")
# Image from local file (converts to base64 data URL)
# {:ok, local_image} = ContentPart.from_file("/path/to/image.jpg")
# Message.user([ContentPart.text("Describe this:"), local_image])
# Image from base64 data
base64_png = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="
data_url = ContentPart.base64_to_data_url(base64_png, "image/png")
base64_message = Message.user([
ContentPart.text("What color is this pixel?"),
ContentPart.image_url(data_url)
])
IO.puts("Created message with base64 image:")
IO.puts(" Text: #{Message.extract_text(base64_message)}\n")
# Combine with conversation history
multimodal_conversation = [
Message.system("You are an image analyst."),
Message.user("I'll show you some images."),
Message.assistant("Great! I'm ready to analyze any images you share."),
Message.user([
ContentPart.text("What's in this logo?"),
ContentPart.image_url("https://upload.wikimedia.org/wikipedia/commons/thumb/4/04/Elixir_logo.png/180px-Elixir_logo.png")
])
]
IO.puts("Built multimodal conversation with #{length(multimodal_conversation)} messages")
IO.puts("(Run with vision-capable model like Claude or GPT-4V)\n")
# ============================================================================
# Conversation with Tools
# ============================================================================
IO.puts("--- Conversation with Tools ---\n")
notes = fn ctx, %{"text" => text} ->
existing = ctx.deps[:notes] || []
%{saved: text, total_notes: length(existing) + 1}
end
agent_with_notes = Nous.new("lmstudio:qwen3",
instructions: "You have a notes tool. Use it to save important info.",
tools: [notes]
)
{:ok, r1} = Nous.run(agent_with_notes, "Save a note: Buy groceries",
deps: %{notes: []}
)
IO.puts("User: Save a note: Buy groceries")
IO.puts("Assistant: #{r1.output}\n")
# Continue with context
{:ok, r2} = Nous.run(agent_with_notes, "Save another: Call mom",
context: r1.context,
deps: %{notes: ["Buy groceries"]}
)
IO.puts("User: Save another: Call mom")
IO.puts("Assistant: #{r2.output}\n")
# ============================================================================
# Key Points
# ============================================================================
IO.puts("""
--- Key Points ---
v0.8.0 Context Continuation (recommended):
{:ok, result} = Nous.run(agent, "First message")
{:ok, result2} = Nous.run(agent, "Follow up", context: result.context)
Legacy Message History:
{:ok, result} = Nous.run(agent, "Message", message_history: previous)
# Use result.new_messages for next call
Custom Messages:
Nous.run(agent, messages: [Message.system(...), Message.user(...)])
Multimodal Messages (images):
alias Nous.Message.ContentPart
Message.user([
ContentPart.text("What's in this image?"),
ContentPart.image_url("https://example.com/image.jpg")
])
# From local file:
{:ok, img} = ContentPart.from_file("/path/to/image.jpg")
Message.user([ContentPart.text("Describe:"), img])
The context: option preserves:
- All messages
- Tool call history
- Usage statistics
- Dependencies (deps)
""")
IO.puts("Next: mix run examples/05_callbacks.exs")