# Large Language Model-Informed X-ray Photoelectron Spectroscopy Data Analysis
## 4 Materials and Methodology
### LLM-Informed XPS Analysis  
In our methodology, the LLM plays a significant role in interpreting the outcomes of the XPS data curve-fitting procedure as depicted in Figure 3. We employ the LLM to provide insights on the potential chemical states of oxygen, suggest potential sources of error, and recommend any additional experiments needed for further confirmation based on the parameters obtained from the curve-fitting process. In Figure 3, the interaction between the user at the CIRCE-NAPP beamline at the CELLS ALBA Synchrotron facility and the LLM-informed agent is depicted. This diagram elucidates the key stages of our innovative XPS data analysis pipeline, beginning with the user providing initial XPS data. The data are then subjected to the curve-fitting process, described in Algorithm 1. The resultant peak parameters are subsequently analyzed by the LLM to provide detailed insights into possible oxygen chemical states, potential sources of error, and recommended additional experiments, as shown in Algorithm 2. This analysis is then returned to the user, facilitating a decision-making process informed by deep, AI-assisted understanding of the data. This iterative process enables rapid, accurate, and efficient analysis of XPS data, with each loop offering opportunities for fine-tuning and increased precision. ```
0:parameters: List of fitted parameters for each peak
0:analysis_response: Detailed analysis from the LLM
1:global recommendation,cache
2:functionGetLLMPeakAnalysis(parameters)
3:parameters_string \(\leftarrow\) ConvertParametersToString(parameters)
4:if parameters_string \(\in\) recommendation_cache then
5:return recommendation_cache[parameters_string]
6:endif
7:retries \(\leftarrow\) 0
8:max_retries \(\leftarrow\) 100
9:wait_time \(\leftarrow\) 60\(\triangleright\) Seconds
10:while retries \(\leq\) max_retries do
11: Attempt to send request to LLM
12:Handle exceptions and retry if needed with exponential backoff
13:if request successful then
14:break
15:endif
16:endwhile
17:if retries \(>\) max_retries then
18:raise Exception(Maximum retries exceeded.') 19:endif
20:analysis_response \(\leftarrow\) response.choices[0].message.content.strip()
21:recommendation_cache[parameters_string] \(\leftarrow\) analysis_response
22:return analysis_response
23:endfunction
```  
**Algorithm 2** LLM Peak Analysis Function for GPT-3.5/4.0  
This analysis starts structuring the LLM operation around a Python function, which takes as input a list of fitted parameters for each peak in the XPS data. These parameters include the center, amplitude, and standard deviation (sigma) of each peak. Figure 3: Flow diagram of the user interaction with the LLM-informed agent in the XPS data analysis at the CIRCE-NAPP beamline. This function creates a formatted string, detailing the center, amplitude, and sigma of each peak. This string acts as the basis for a query to the LLM. Prior to querying the LLM, we verify whether the same string exists in a cache, a dictionary that stores previous queries and their corresponding responses from the LLM. If a match is found, the cached response is returned immediately. This cache mechanism minimizes redundant queries to the LLM and enhances the efficiency of the function. In cases where the query string is not found in the cache, the function engages in an interaction with the LLM. A maximum of 100 attempts, or \"retries\", are allowed for successful communication with the LLM. The API function handles this interaction, where a system message first sets the context of the LLM as an expert in XPS with deep knowledge in chemical state identification and binding energy levels. The user message then presents the fitted peak parameters and requests a detailed analysis. The choice of 100 retries was determined based on a balance between ensuring robustness in data processing and practical considerations regarding system wait times and API responsiveness.