add basic code to call deepseek

This commit is contained in:
geekgeekrun
2025-04-12 14:35:58 +08:00
parent 7458067708
commit 85e27dc6ef
5 changed files with 166 additions and 15 deletions

View File

@@ -1,5 +1,6 @@
import { Page } from 'puppeteer'
import { sleepWithRandomDelay } from '@geekgeekrun/utils/sleep.mjs'
import { sleepWithRandomDelay, sleep } from '@geekgeekrun/utils/sleep.mjs';
import { completes } from '@geekgeekrun/utils/gpt-request.mjs'
export const sendLookForwardReplyEmotion = async (page: Page) => {
const emotionEntryButtonProxy = await page.$('.chat-conversation .message-controls .btn-emotion')
@@ -15,3 +16,64 @@ export const sendLookForwardReplyEmotion = async (page: Page) => {
)
await lookForwardReplyEmojiProxy!.click()
}
export const sendGptContent = async (page: Page, chatRecords) => {
const chatList = [
{
role: 'system',
content:
'你是一个求职消息发送机器人正在帮助一位求职者在Boss直聘上寻找一份工作。求职者需要向招聘者聊天得到招聘者的回复后方能获得一次投递简历的机会。你需要“从求职者简历中提取到的信息来生成一个要发送给招聘者的消息”。同时你需要注意每次发送的内容一定要接续之前发送的内容最好不要和已发过的内容重复。求职者简历信息如下' +
``
}
]
chatList.push({
role: 'user',
content:
'请帮我写一句开场白。请确保仅响应一句话以JSON响应数据结构参考`{"response": "这里是将会发送给招聘者的内容"}`'
})
for (const record of chatRecords) {
const assistantJsonContent = JSON.stringify({
response: record.text
})
chatList.push({
role: 'assistant',
content: `\`\`\`json\n${assistantJsonContent}\n\`\`\``
})
chatList.push({
role: 'user',
content:
'请根据接续之前你所回复的内容根据我的简历写一句自我介绍注意尽量不要和之前的聊天内容重复。请确保仅响应一句话以JSON响应数据结构参考`{"response": "这里是将会发送给招聘者的内容"}`'
})
}
console.log(chatList)
debugger
const res = await completes(chatList)
console.log(res)
let textToSend
try {
const rawMarkdownText = res?.message?.content
textToSend = JSON.parse(
rawMarkdownText.replace(/^```json/m, '').replace(/```$/m, '')
)?.response
if (!textToSend) {
throw new Error(`empty content. ${err?.message} ${res?.message?.content}`)
}
} catch (err) {
throw new Error(`fail to parse response. ${err?.message} ${res?.message?.content}`)
}
const chatInputSelector = `.chat-conversation .message-controls .chat-input`
const chatInputHandle = await page.$(chatInputSelector)
await chatInputHandle.click()
await sleep(500)
await chatInputHandle.click()
await chatInputHandle.type(
textToSend,
{
delay: 50
}
)
await sleep(1000)
const sendButtonSelector = `.chat-conversation .message-controls .chat-op .btn-send:not(.disabled)`
await page.click(sendButtonSelector)
}

View File

@@ -1,7 +1,7 @@
import { bootstrap, launchBoss } from './bootstrap'
import { MsgStatus, type ChatListItem } from './types'
import { Browser, Page } from 'puppeteer'
import { sendLookForwardReplyEmotion } from './boss-operation'
import { sendGptContent, sendLookForwardReplyEmotion } from './boss-operation'
import { sleep, sleepWithRandomDelay } from '@geekgeekrun/utils/sleep.mjs'
import attachListenerForKillSelfOnParentExited from '../../utils/attachListenerForKillSelfOnParentExited'
import { app } from 'electron'
@@ -277,7 +277,19 @@ const mainLoop = async () => {
(throttleIntervalMinutes + 4 * Math.random()) * 60 * 1000
) {
await sleepWithRandomDelay(3250)
await sendLookForwardReplyEmotion(pageMapByName.boss!)
// execute reply
// eslint-disable-next-line no-constant-condition
if (1 + 1 === 2) {
try {
const messageListForGpt = historyMessageList.filter(it => it.bizType !== 101 && it.isSelf)
debugger
await sendGptContent(pageMapByName.boss!, messageListForGpt)
} catch (err) {
await sendLookForwardReplyEmotion(pageMapByName.boss!)
}
} else {
await sendLookForwardReplyEmotion(pageMapByName.boss!)
}
} else {
cursorToContinueFind += 1
}

View File

@@ -0,0 +1,21 @@
import OpenAI from "openai";
const GPT_API_KEY = `sk-40fdef46fee24402bc05311304fce7a1`
export async function completes(messages) {
const openai = new OpenAI({
baseURL: 'https://api.deepseek.com',
apiKey: GPT_API_KEY
});
const completion = await openai.chat.completions.create({
messages,
model: "deepseek-chat",
frequency_penalty: 0,
max_tokens: 100,
temperature: 0.2
});
console.log(completion.choices[0].message.content);
return completion.choices?.[0] ?? null;
}

View File

@@ -3,9 +3,10 @@
"private": true,
"version": "1.0.0",
"description": "utils",
"scripts": {
},
"scripts": {},
"author": "geekgeekrun",
"license": "ISC",
"dependencies": {}
"dependencies": {
"openai": "^4.91.1"
}
}