我的chatgpt网站:
{
"name": "get_current_datetime",
"description": "获取北京时间,当前时间,当前日期",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "城市,例如:北京"
}
},
"required": [
"city"
]
}
}
const assistantsAddMessageOnAzure = async (req, res) => {
let {
message = { role: 'user', content: '你好' },
apiKey = 'sk-xxx',
assistantId = 'asst_xxx',
threadId = '',
} = req.body
if (apiKey === apiKeyOnServer) {
let errorData = {}
let count = 0
let isSuccess = true
let resultMessages = []
let thread = {}
try {
if (threadId) {
let oldThread = {}
try {
oldThread = await openai.beta.threads.retrieve(threadId) //update retrieve
} catch (error) {
console.log(error)
}
if (oldThread.id) {
thread = oldThread
} else {
thread = await openai.beta.threads.create()
console.log('thread', thread)
}
} else {
thread = await openai.beta.threads.create()
console.log('thread', thread)
}
const messageRes = await openai.beta.threads.messages.create(
thread.id,
message
)
console.log('messageRes', messageRes)
const run = await openai.beta.threads.runs.create(thread.id, {
assistant_id: assistantId,
})
const handleProcessing = async () => {
await openai.beta.threads.runs
.retrieve(thread.id, run.id)
.then(async (processRes) => {
if (processRes.status === 'completed') {
resultMessages = await openai.beta.threads.messages.list(
thread.id
)
} else if (processRes.status === 'requires_action') {
let toolCalls =
processRes.required_action.submit_tool_outputs.tool_calls
if (Array.isArray(toolCalls) && toolCalls.length > 0) {
let tool_outputs = []
for (const toolCall of toolCalls) {
let functionName = toolCall.function.name
if (functionName === 'get_current_datetime') {
let myArguments = toolCall.function.arguments
myArguments = myArguments ? JSON.parse(myArguments) : {}
let currentTimeInChina = getCurrentTimeForChina()
let content = moment(currentTimeInChina).format(
'YYYY-MM-DD HH:mm:ss'
)
tool_outputs.push({
tool_call_id: toolCall.id,
output: content,
})
}
}
await openai.beta.threads.runs.submitToolOutputs(
thread.id,
run.id,
{
tool_outputs,
}
)
await handleProcessing()
}
} else {
console.log('progress', Date(), count, processRes)
count++
//2分钟还在等待
if (count > 12 * 2) {
let runCancel = await openai.beta.threads.runs.cancel(
thread.id,
run.id
)
console.log('runCancel', runCancel)
isSuccess = false
res.send({
code: 400,
data: {},
message: `请求超时,自动取消`,
})
return
}
await sleep(5000)
await handleProcessing()
}
})
}
await sleep(1000)
await handleProcessing()
// const response = await openai.beta.threads.del(thread.id)
// console.log('response', response)
} catch (error) {
res.send({
code: 400,
data: {
errorData,
},
message: `失败-机器人无应答【3】,${error}`,
})
return
}
if (isSuccess) {
console.log('resultMessages', resultMessages)
res.send({
code: 200,
data: {
thread,
errorData,
resultMessages,
},
message: '成功',
})
} else {
res.send({
code: 400,
data: {
isSuccess,
errorData,
},
message: '失败,机器人无应答【7】',
})
}
} else {
res.send({
code: 400,
message: '失败:参数apiKey',
})
}
}
在美国的服务器上获取北京的当前时间:
function getCurrentTimeForChina() {
// 获取当前的日期和时间
const now = new Date()
// 转换为UTC时间
const nowUtc = new Date(now.getTime() + now.getTimezoneOffset() * 60000)
// 中国时区(北京时间)是 UTC+8,所以加上8小时的毫秒数
const chinaTime = new Date(nowUtc.getTime() + 8 * 60 * 60 * 1000)
return chinaTime
}