-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathCreateSimpleChatBotUsingAIAgent.m
More file actions
63 lines (62 loc) · 3.35 KB
/
Copy pathCreateSimpleChatBotUsingAIAgent.m
File metadata and controls
63 lines (62 loc) · 3.35 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
%[text] # Create Simple ChatBot
%[text] When you run this example, an interactive AI chat starts in the MATLAB® Command Window. To leave the chat, type "end" or press **Ctrl+C**.
%[text] This example includes three steps:
%[text] - Define model parameters and a stop word.
%[text] - Create an LLM client object and an agent using this client for managing the history of the chat loop.
%[text] - Set up the chat loop. \
%[text] To run this example, you need a valid API key from a paid OpenAI™ API account.
% loadenv(".env")
%%
%[text] ## Create LLM Client and Agent
api = "openai";
modelName = "gpt-4.1-mini";
client = aisdk.LLMClient(api, modelName);
%[text] Create an instance of `AIAgent` to manage the message history in the chat. Also define the bot behavior through a system prompt.
sysPrompt = "You are a helpful assistant. You reply in a very concise way, keeping answers limited to short sentences.";
session = aisdk.AIAgent(client, SystemPrompt=sysPrompt);
%%
%[text] ## Chat loop
%[text] Start the chat and keep it going until it sees the word in `stopWord` (or until you hit **Ctrl+C**).
stopWord = "end";
while true %[output:group:7ff8d64f]
prompt = input("User: ", "s");
prompt = string(prompt);
disp("User: " + prompt) %[output:41401c71] %[output:76bc4f07] %[output:67dad65d]
%[text] If you input the stop word, display a farewell message and exit the loop.
if prompt == stopWord
disp("AI: Closing the chat. Have a great day!") %[output:632d19df]
break;
end
text = run(session, prompt); %[output:32ab2fe7] %[output:8d24b85b]
disp("AI: " + text) %[output:9e5e5f67] %[output:5869b4a4]
end %[output:group:7ff8d64f]
%[text] *Copyright 2026 The MathWorks, Inc.*
%[appendix]{"version":"1.0"}
%---
%[metadata:view]
% data: {"layout":"inline","rightPanelPercent":25.8}
%---
%[output:41401c71]
% data: {"dataType":"text","outputData":{"text":"User: What's Bohemian Rhapsody about?\n","truncated":false}}
%---
%[output:32ab2fe7]
% data: {"dataType":"text","outputData":{"text":"[think]\n\"Bohemian Rhapsody\" is about a young man confessing to a murder and grappling with guilt and fate.\n","truncated":false}}
%---
%[output:9e5e5f67]
% data: {"dataType":"text","outputData":{"text":"AI: \"Bohemian Rhapsody\" is about a young man confessing to a murder and grappling with guilt and fate.\n","truncated":false}}
%---
%[output:76bc4f07]
% data: {"dataType":"text","outputData":{"text":"User: Can you elaborate?\n","truncated":false}}
%---
%[output:8d24b85b]
% data: {"dataType":"text","outputData":{"text":"[think]\nThe song tells a dramatic story. A young man admits to killing someone, feels remorse, and fears consequences. It blends different musical styles to express his emotional turmoil and confusion. The meaning is open to interpretation.\n","truncated":false}}
%---
%[output:5869b4a4]
% data: {"dataType":"text","outputData":{"text":"AI: The song tells a dramatic story. A young man admits to killing someone, feels remorse, and fears consequences. It blends different musical styles to express his emotional turmoil and confusion. The meaning is open to interpretation.\n","truncated":false}}
%---
%[output:67dad65d]
% data: {"dataType":"text","outputData":{"text":"User: end\n","truncated":false}}
%---
%[output:632d19df]
% data: {"dataType":"text","outputData":{"text":"AI: Closing the chat. Have a great day!\n","truncated":false}}
%---