icon picker
代码

请输入任务描述:例如我要进行某某模型的部署/我要进行动力学模拟...

请求API js兼容格式

fetch("https://openrouter.ai/api/v1/chat/completions", {
method: "POST",
headers: {
"Authorization": "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",
"HTTP-Referer": " ",
"Content-Type": " "
},
body: JSON.stringify({
"model": "google/gemini-2.0-flash-thinking-exp:free",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "What is in this image?"
},
{
"type": "image_url",
"image_url": {
"url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
}
}
]
}
]
})
});

openrouter key

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
document.addEventListener("DOMContentLoaded", () => {
const userInput = document.getElementById("userInput");
const sendBtn = document.getElementById("sendBtn");

// 发送请求
sendBtn.addEventListener("click", async () => {
const message = userInput.value.trim();
if (!message) return;

const systemLoad = {
cpuUsage: 70,
gpuUsage: 60,
memoryUsage: 50,
storageSpace: 500,
};

// 构建 Prompt 数据
const prompt = `
用户需求:${message}
当前系统负载:CPU使用率=${systemLoad.cpuUsage}%,GPU使用率=${systemLoad.gpuUsage}%,内存使用率=${systemLoad.memoryUsage}%,存储空间=${systemLoad.storageSpace}GB
历史任务数据:过去相似任务的CPU需求=4核,GPU需求=2块,内存需求=16GB,存储需求=200GB
请根据这些信息评估资源需求并给出建议:CPU、GPU、内存、存储空间
`;

// 调用 OpenRouter API
const response = await fetch("https://openrouter.ai/api/v1/chat/completions", {
method: "POST",
headers: {
"Authorization": "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", // 替换为实际 API 密钥
"Content-Type": "application/json"
},
body: JSON.stringify({
model: "google/gemini-2.0-flash-thinking-exp:free",
messages: [
{
role: "user",
content: [{ type: "text", text: prompt }]
}
]
})
});

const data = await response.json();
console.log(data); // 打印整个 API 响应数据结构

// 检查返回数据结构
if (data && data.choices && data.choices[0] && data.choices[0].message) {
const botReply = data.choices[0].message.content; // 获取 AI 回复的内容
console.log("Bot Reply:", botReply); // 输出 AI 回复到控制台
} else {
console.error("API 返回的结构不正确", data);
}
});
});

上版本
document.addEventListener("DOMContentLoaded", () => {
const userInput = document.getElementById("userInput");
const sendBtn = document.getElementById("sendBtn");
const clearBtn = document.getElementById("clearBtn");
const chatHistory = document.getElementById("chat-history");

// 发送消息
async function sendMessage() {
const message = userInput.value.trim();
if (!message) return;

// 在界面中添加用户消息
addMessageToChat("You", message, "right");
userInput.value = ""; // 清空输入框

// 获取用户 ID
const { userId } = await chrome.storage.sync.get("userId");

// 构建大模型请求的标准化 prompt
//使用用户输入的文本和其他信息构建 prompt 预留动态获取 其他地方传参给prompt
const prompt = `
用户需求:${message}
当前系统负载:CPU使用率=70%,GPU使用率=60%,内存使用率=50%,存储空间=500GB
历史任务数据:过去相似任务的CPU需求=4核,GPU需求=2块,内存需求=16GB,存储需求=200GB
请根据这些信息评估资源需求并给出建议:CPU、GPU、内存、存储空间
`;

// 调用 OpenRouter API 发送请求
const response = await fetch("https://openrouter.ai/api/v1/chat/completions", {
method: "POST",
headers: {
"Authorization": "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", // 请替换为实际 API 密钥
//"HTTP-Referer": "<YOUR_SITE_URL>", // 可选
//"X-Title": "<YOUR_SITE_NAME>", // 可选
},
body: JSON.stringify({
"model": "google/gemini-2.0-flash-thinking-exp:free",
"messages": [
{
"role": "user",
"content": [
{ "type": "text", "text": message }, // 用户输入的文本
{ "type": "image_url", "image_url": { "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg" } } // 你可以根据需要修改此图片 URL
]
}
]
})
});

const data = await response.json();
const botReply = data.choices[0].message.content;

// 在界面中添加 AI 回复消息
addMessageToChat("Assistant", botReply, "left");
}

// 在聊天窗口添加消息
function addMessageToChat(sender, text, position) {
const messageDiv = document.createElement("div");
messageDiv.classList.add(position); // 根据位置添加不同的类(right 或 left)
messageDiv.innerHTML = `<strong>${sender}:</strong> ${text}`;
chatHistory.appendChild(messageDiv);

// 让聊天窗口自动滚动到底部
chatHistory.scrollTop = chatHistory.scrollHeight;
}

// 清除文本框内容
clearBtn.addEventListener("click", () => {
userInput.value = "";
});

// 绑定按钮点击事件
sendBtn.addEventListener("click", sendMessage);

// 绑定回车键发送消息
userInput.addEventListener("keypress", (event) => {
if (event.key === "Enter" && !event.shiftKey) {
event.preventDefault();
sendMessage();
}
});
});

Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.