1
+ %% Analyze Scientific Papers Using ChatGPT Function Calls
2
+ % This example shows how to extract recent scientific papers from ArXiv, summarize
3
+ % them using ChatGPT, and write the results to a CSV file using the |openAIFunction|
4
+ % function.
5
+ %%
6
+ % * The example contains three steps:
7
+ % * Define a custom function for ChatGPT to use to process its input and output.
8
+ % * Extract papers from ArXiv.
9
+ % * Use ChatGPT to assess whether a paper is relevant to your query, and to
10
+ % add an entry to the results table if so.
11
+ %% Initialize OpenAI API Function and Chat
12
+ % Use |openAIFunction| to define functions that the model will be able to requests
13
+ % calls.
14
+ %
15
+ % Set up the function to store paper details and initiate a chat with the OpenAI
16
+ % API with a defined role as a scientific paper expert.
17
+ %
18
+ % Define the function that you want the model to have access to. In this example
19
+ % the used function is |writePaperDetails|.
20
+
21
+ f = openAIFunction(" writePaperDetails" , " Function to write paper details to a table." );
22
+ f = addParameter(f , " name" , type= " string" , description= " Name of the paper." );
23
+ f = addParameter(f , " url" , type= " string" , description= " URL containing the paper." );
24
+ f = addParameter(f , " explanation" , type= " string" , description= " Explanation on why the paper is related to the given topic." );
25
+
26
+ chat = openAIChat(" You are an expert in filtering scientific papers. " + ...
27
+ " Given a certain topic, you are able to decide if the paper" + ...
28
+ " fits the given topic or not." , Tools= f );
29
+
30
+ function writePaperDetails(name , url , desc )
31
+ filename = " papers_to_read.csv" ;
32
+ T = table(name , url , desc , VariableNames= [" Name" , " URL" , " Description" ]);
33
+ writetable(T , filename , WriteMode= " append" );
34
+ end
35
+ %% Extract Papers From ArXiv
36
+ % Specify the category of interest, the date range for the query, and the maximum
37
+ % number of results to retrieve from the ArXiv API.
38
+
39
+ category = " cs.CL" ;
40
+ endDate = datetime(" today" , " Format" ," uuuuMMdd" );
41
+ startDate = datetime(" today" , " Format" ," uuuuMMdd" ) - 5 ;
42
+ maxResults = 40 ;
43
+ urlQuery = " https://export.arxiv.org/api/query?search_query=" + ...
44
+ " cat:" + category + ...
45
+ " &submittedDate=[" +string(startDate )+" +TO+" +string(endDate )+" ]" +...
46
+ " &max_results=" + maxResults + ...
47
+ " &sortBy=submittedDate&sortOrder=descending" ;
48
+
49
+ options = weboptions(' Timeout' ,160 );
50
+ code = webread(urlQuery ,options );
51
+ %%
52
+ % Extract individual paper entries from the API response and use ChatGPT to
53
+ % determine whether each paper is related to the specified topic.
54
+ %
55
+ % ChatGPT will parse the XML file, so we only need to extract the relevant entries.
56
+
57
+ entries = extractBetween(code , ' <entry>' , ' </entry>' );
58
+ %% Write Relevant Information to Table
59
+ % Determine the topic of interest.
60
+
61
+ topic = " Large Language Models" ;
62
+ %%
63
+ % Loop over the entries and see if they are relevant to the topic of interest.
64
+
65
+ for i = 1 : length(entries )
66
+ prompt = " Given the following paper:" + newline + ...
67
+ string(entries{i })+ newline + ...
68
+ " Is it related to the topic: " + topic +" ?" + ...
69
+ " Answer 'yes' or 'no'." ;
70
+ [text , response ] = generate(chat , prompt );
71
+
72
+ %%
73
+ % If the model classifies this entry as relevant, then it tries to request a
74
+ % function call.
75
+
76
+ if contains(" yes" , text , IgnoreCase= true )
77
+ prompt = " Given the following paper:" + newline + string(entries{i })+ newline + ...
78
+ " Given the topic: " + topic + newline + " Write the details to a table." ;
79
+ [text , response ] = generate(chat , prompt );
80
+
81
+ %%
82
+ % If |function_call| if part of the response, it means the model is requesting
83
+ % a function call. The function call request should contain the needed arguments
84
+ % to call the function specified at the end of this example and defined with |openAIFunctions|.
85
+
86
+ if isfield(response , " tool_calls" )
87
+ funCall = response .tool_calls ;
88
+ functionCallAttempt(funCall );
89
+ end
90
+ end
91
+ end
92
+ %%
93
+ % Read the generated file.
94
+
95
+ data = readtable(" papers_to_read.csv" , Delimiter= " ," )
96
+ %% Helper Function
97
+ % This function handles function call attempts from the model, checking the
98
+ % function name and arguments before calling the appropriate function to store
99
+ % the paper details.
100
+
101
+ function functionCallAttempt(funCall )
102
+ %%
103
+ % The model can sometimes hallucinate function names, so you need to ensure
104
+ % that it's suggesting the correct name.
105
+
106
+ if funCall .function .name == " writePaperDetails"
107
+ try
108
+ %%
109
+ % The model can sometimes return improperly formed JSON, which needs to be handled.
110
+
111
+ funArgs = jsondecode(funCall .function .arguments );
112
+ catch ME
113
+ error(" Model returned improperly formed JSON." );
114
+ end
115
+ %%
116
+ % The model can hallucinate arguments. The code needs to ensure the arguments
117
+ % have been defined before calling the function.
118
+
119
+ if isfield(funArgs , " name" ) && isfield(funArgs , " url" ) && isfield(funArgs ," explanation" )
120
+ writePaperDetails(string(funArgs .name ), string(funArgs .url ), string(funArgs .explanation ));
121
+ end
122
+ end
123
+ end
124
+ %%
125
+ % _Copyright 2023-2024 The MathWorks, Inc._
0 commit comments