1+ %% Create Simple ChatBot
2+ % This example shows how to create a simple chatbot using the |openAIChat| and
3+ % |openAIMessages| functions.
4+ %
5+ % When you run this example, an interactive AI chat starts in the MATLAB Command
6+ % Window. To leave the chat, type "end" or press *Ctrl+C*.
7+ %%
8+ % * This example includes three steps:
9+ % * Define model parameters, such as the maximum word count, and a stop word.
10+ % * Create an openAIChat object and set up a meta prompt.
11+ % * Set up the chat loop.
12+ %%
13+ % To run this example, you need a valid API key from a paid OpenAI API account.
14+
15+ loadenv(" .env" )
16+ addpath(' ..' )
17+ %% Setup Model
18+ % Set the maximum allowable number of words per chat session and define the
19+ % keyword that, when entered by the user, ends the chat session. This example
20+ % uses the model |gpt-3.5-turbo|.
21+
22+ wordLimit = 2000 ;
23+ stopWord = " end" ;
24+ modelName = " gpt-3.5-turbo" ;
25+ %%
26+ % Create an instance of |openAIChat| to perform the chat and |openAIMessages|
27+ % to store the conversation history|.|
28+
29+ chat = openAIChat(" You are a helpful assistant. You reply in a very concise way, keeping answers limited to short sentences." , ModelName= modelName );
30+ messages = openAIMessages ;
31+ %% Chat loop
32+ % Start the chat and keep it going until it sees the word in |stopWord|.
33+
34+ totalWords = 0 ;
35+ messagesSizes = [];
36+ %%
37+ % The main loop continues indefinitely until you input the stop word or press
38+ % *Ctrl+C.*
39+
40+ while true
41+ query = input(" User: " , " s" );
42+ query = string(query );
43+ disp(" User: " + query )
44+ %%
45+ % If the you input the stop word, display a farewell message and exit the loop.
46+
47+ if query == stopWord
48+ disp(" AI: Closing the chat. Have a great day!" )
49+ break ;
50+ end
51+
52+ numWordsQuery = countNumWords(query );
53+ %%
54+ % If the query exceeds the word limit, display an error message and halt execution.
55+
56+ if numWordsQuery > wordLimit
57+ error(" Your query should have less than 2000 words. You query had " + numWordsQuery + " words" )
58+ end
59+ %%
60+ % Keep track of the size of each message and the total number of words used
61+ % so far.
62+
63+ messagesSizes = [messagesSizes ; numWordsQuery ]; % #ok
64+ totalWords = totalWords + numWordsQuery ;
65+ %%
66+ % If the total word count exceeds the limit, remove messages from the start
67+ % of the session until it no longer does.
68+
69+ while totalWords > wordLimit
70+ totalWords = totalWords - messagesSizes(1 );
71+ messages = removeMessage(messages , 1 );
72+ messagesSizes(1 ) = [];
73+ end
74+ %%
75+ % Add the new message to the session and generate a new response.
76+
77+ messages = addUserMessage(messages , query );
78+ [text , response ] = generate(chat , messages );
79+
80+ disp(" AI: " + text )
81+ %%
82+ % Count the number of words in the response and update the total word count.
83+
84+ numWordsResponse = countNumWords(text );
85+ messagesSizes = [messagesSizes ; numWordsResponse ]; % #ok
86+ totalWords = totalWords + numWordsResponse ;
87+ %%
88+ % Add the response to the session.
89+
90+ messages = addResponseMessage(messages , response );
91+ end
92+ %% |countNumWords| function
93+ % Function to count the number of words in a text string
94+
95+ function numWords = countNumWords(text )
96+ numWords = doclength(tokenizedDocument(text ));
97+ end
98+ %%
99+ % _Copyright 2023-2024 The MathWorks, Inc._
0 commit comments