Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(web): add ChatGPT example #1052

Merged
merged 1 commit into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions web/public/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -411,5 +411,6 @@
"item3_1": "Feedback",
"item3_2": "Forum"
}
}
}
},
"ChatGPT example": "ChatGPT example"
}
5 changes: 3 additions & 2 deletions web/public/locales/zh-CN/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -411,5 +411,6 @@
"item3_1": "问题反馈",
"item3_2": "开发者社区"
}
}
}
},
"ChatGPT example": "ChatGPT 示例"
}
5 changes: 3 additions & 2 deletions web/public/locales/zh/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -411,5 +411,6 @@
"item3_1": "问题反馈",
"item3_2": "开发者社区"
}
}
}
},
"ChatGPT example": "ChatGPT 示例"
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,45 @@ exports.main = async function (ctx: FunctionContext) {
}
`,
},
{
label: t("ChatGPT example"),
value: `import cloud from '@lafjs/cloud'

export async function main(ctx: FunctionContext) {
const data = ctx.body
const { ChatGPTAPI } = await import('chatgpt')


let api = cloud.shared.get('api')

if (!api) {
// 这里需要填写你的apiKey
api = new ChatGPTAPI({ apiKey: '' })
cloud.shared.set('api', api)
}

ctx.response.setHeader('Content-Type', 'application/octet-stream');

const obj = {
onProgress: (partialResponse => {
if (partialResponse && partialResponse.delta != undefined) {
ctx.response.write(partialResponse.delta);
}
})
}

if (data.parentMessageId) {
obj.parentMessageId = data.parentMessageId
}

const res = await api.sendMessage(data.message, obj)

ctx.response.end("--!" + res.id)

return res
}
`,
},
];

export default functionTemplates;