Selamlar,ChatGPT apisi ile proje yapmaktayımda biri sen kimsin vb. soru sorduğunda kendini ChatGPT olarak tanıtmamasını istiyorum nasıl yapabilirim? Aşağıya örnek kod bırakıyorum
Bu kodu yapınca tüm mesajlara ''Ben ÖrnekGPT,size nasıl yardımcı olabilirim?'' yazıyor
const getChatResponse = async (incomingChatDiv) => {
const API_URL = "https://api.openai.com/v1/completions";
const pElement = document.createElement("p");
const requestOptions = {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${API_KEY}`
},
body: JSON.stringify({
model: "gpt-3.5-turbo-instruct",
prompt: `Kullanıcı: ÖrnekGPT\n`,
max_tokens: 2048,
temperature: 0.2,
n: 1,
stop: null
})
}Aşağıdaki kodta normal çalışıyor fakat sen kimsin vb. sorularda ChatGPT cevabını alıyorum
const requestOptions = {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${API_KEY}`
},
body: JSON.stringify({
model: "gpt-3.5-turbo-instruct",
prompt: userText,
max_tokens: 2048,
temperature: 0.2,
n: 1,
stop: null
})
}Soruları istisna olarak ekledim fakat bir soru binbir türlü sorulduğu için çok uzun sürer,nasıl yapabilirim?
if (
userText.toLowerCase() === "sen kimsin?" ||
userText.toLowerCase() === "sen kimsin" ||
userText.toLowerCase() === "sen kimsin söyle" ||
userText.toLowerCase() === "kendini tanıt" ||
userText.toLowerCase() === "kendini tanıtsana" ||
userText.toLowerCase() === "kendini tanıtır mısın" ||
userText.toLowerCase() === "kim olduğunu söyle" ||
userText.toLowerCase() === "kim olduğunu söylesene"
) {
pElement.textContent = "Ben ÖrnekGPT, sana nasıl yardımcı olabilirim?";
} else {
// Diğer durumlarda API'ye istek gönderin ve yanıtı alın
const requestOptions = {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${API_KEY}`
},
body: JSON.stringify({
model: "gpt-3.5-turbo-instruct",
prompt: userText,
max_tokens: 2048,
temperature: 0.2,
n: 1,
stop: null
})
}Systeme tanımlayınca direkt şu mesajı atıyor
- Oops! Something went wrong while retrieving the response. Please try again.
konsoldada şu hata var
- TypeError: Cannot read properties of undefined (reading '0')
at getChatResponse (script.js:72:52)
script.js:72:52de bu
- pElement.textContent = response.choices[0].text.trim();
const getChatResponse = async (incomingChatDiv, userText) => {
const API_URL = "https://api.openai.com/v1/completions";
const pElement = document.createElement("p");
const requestOptions = {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${API_KEY}`
},
body: JSON.stringify({
model: "ÖrnekGPT", // Özel modelinizin adını buraya ekleyin
messages: [
{
role: 'system',
content: 'You are speaking to ÖrnekGPT',
},
{
role: 'user',
content: userText,
},
],
max_tokens: 2048,
temperature: 0.2,
n: 1,
stop: null
})
};
try {
const response = await (await fetch(API_URL, requestOptions)).json();
pElement.textContent = response.choices[0].text.trim();
} catch (error) {
console.log(error);
pElement.classList.add("error");
pElement.textContent = "Oops! Something went wrong while retrieving the response. Please try again.";
}
}