以下是调用ChatGPT对话API的HTML和JavaScript代码示例:

<!DOCTYPE html>
<html>
<head>
	<title>ChatGPT API Demo</title>
	<meta charset="UTF-8">
	<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>
<body>
	<h1>ChatGPT API Demo</h1>
	<p>Type something to start the conversation:</p>
	<textarea id="input" cols="50" rows="10"></textarea>
	<button onclick="sendInput()">Send</button>
	<p>Conversation:</p>
	<textarea id="output" cols="50" rows="10" disabled></textarea>

	<script>
		function sendInput() {
			const input = document.getElementById("input").value;
			const output = document.getElementById("output");
			output.value += "You: " + input + "\n";
			document.getElementById("input").value = "";

			const config = {
				headers: {
					'Content-Type': 'application/json',
					'Authorization': 'Bearer YOUR_API_KEY'
				}
			};

			const data = {
				prompt: input,
				max_tokens: 50,
				temperature: 0.7,
				stop: ["\n"]
			};

			axios.post('https://api.openai.com/v1/chat/completions', data, config)
			.then(response => {
				output.value += "Bot: " + response.data.choices[0].text.trim() + "\n";
			})
			.catch(error => {
				console.log(error);
			});
		}
	</script>
</body>
</html>

请注意,此代码中的“YOUR_API_KEY”需要替换为您的实际API密钥。此外,此代码仅使用了“max_tokens”、“temperature”和“stop”参数来演示如何使用ChatGPT API。您可以根据自己的需求调整这些参数,并使用其他可用参数。

给一段html用的js调用chatgpt对话api的v1chatcompletions接口的demo代码stream模式写入文本框调用post

原文地址: https://www.cveoy.top/t/topic/LR5 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录