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

chore: fetch -> wretch #1

Merged
merged 2 commits into from
Apr 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion event/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,7 @@ export async function handleMessage(msg: Message) {
return
}
const data = await route.handle(text, msg)
await msg.say(data)
if (data) {
await msg.say(data)
}
}
43 changes: 27 additions & 16 deletions lib/reply.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import config from '../config'
import { sample } from 'midash'
import wretch from 'wretch'
import { retry } from 'wretch/middlewares'

type ChatMessage = {
role: 'system' | 'user' | 'assistant'
Expand All @@ -16,26 +18,35 @@ type GPTModel =

export async function reply(messages: ChatMessage[]) {
const apiKey = sample(config.apiKey)
return fetch(`${config.baseURL}/v1/chat/completions`, {
headers: {
const w = wretch(config.baseURL).middlewares([
retry({
delayTimer: 500,
maxAttempts: 3,
until: (response, error) => response && response.ok
})
])
return w
.url('/v1/chat/completions')
.headers({
Authorization: `Bearer ${apiKey}`,
'Content-Type': 'application/json',
},
method: 'POST',
body: JSON.stringify({
model: config.model,
messages,
}),
})
.then((res) => res.json())
.then((data) => {
'Content-Type': 'application/json'
})
.post(
JSON.stringify({
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里不需要 stringify

model: config.model,
messages
})
)
.json((data) => {
if (!data.choices.length) {
console.log('Error', data)
}
console.log(JSON.stringify({
input: messages,
output: data.choices[0].message
}))
console.log(
JSON.stringify({
input: messages,
output: data.choices[0].message
})
)
return data.choices[0].message.content
})
.catch((e) => {
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
"wechaty": "^1.20.2",
"wechaty-puppet": "^1.20.2",
"wechaty-puppet-padlocal": "^1.20.1",
"wechaty-puppet-wechat": "^1.18.4"
"wechaty-puppet-wechat": "^1.18.4",
"wretch": "^2.5.2",
"wretch-middlewares": "^0.1.13"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不需要这个 package

},
"devDependencies": {
"@types/koa": "^2.13.6",
Expand Down
142 changes: 95 additions & 47 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.