Hey there,
I want to reference a chat conversation I had in a “normal” chat gpt window in my API request.
I couldn’t find a solution yet to reference a session ID.
(Reason for this is, that I have given pretty long instructions in this chat window and would love for the API to continue where this specific chat has left off.
So I don’t have to give all the instructions in every prompt again wasting huge amounts of tokens.)
Here is the javascript example (API for Google Sheets btw) that Chat itself suggested but it did not work.
function GPT_4(Input) {
const GPT_API = "sk-XvXXXXX";
const BASE_URL = "https://api.openai.com/v1/chat/completions";
const sessionId = "XXXXX"; // Session ID
const headers = {
"Content-Type": "application/json",
"Authorization": `Bearer ${GPT_API}`
};
const options = {
method: "POST",
headers: headers,
muteHttpExceptions: true,
payload: JSON.stringify({
"model": "gpt-4",
"messages": [{
"role": "system",
"content": ""
}, {
"role": "user",
"content": Input
}],
"temperature": 0.5,
"session_id": sessionId // Adding session ID to the request
})
};
try {
const response = UrlFetchApp.fetch(BASE_URL, options);
const jsonResponse = JSON.parse(response.getContentText());
if (jsonResponse.choices && jsonResponse.choices.length > 0) {
console.log(jsonResponse.choices[0].message.content);
return jsonResponse.choices[0].message.content;
} else {
console.log("Response does not contain choices.", jsonResponse);
return "Error: Response does not contain choices.";
}
} catch (e) {
console.error("Error making API request:", e);
return "Error occurred during API request.";
}
}
Any ideas or workarounds how I could avoid giving all instructions in every API request?
(Summaries don’t work because it is very specific & long.)