Skip to content

Commit b1c3d2e

Browse files
committed
add AI chat help
1 parent 38a1085 commit b1c3d2e

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

src/ai.ts

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import OpenAI, { ClientOptions } from 'openai'
22

33
import {
44
PARSE_ELEMENTS_CONTEXT,
5-
GET_ELEMENT_SELECTORS_CONTEXT
5+
GET_ELEMENT_SELECTORS_CONTEXT,
6+
HELP_CONTEXT
67
} from './default'
78

89
type Model =
@@ -38,6 +39,7 @@ interface RunOption {
3839
context: string
3940
HTMLContent: string
4041
userContent: string
42+
responseFormatType: 'text' | 'json_object'
4143
}
4244

4345
interface ParseElementsContentOption {
@@ -71,7 +73,13 @@ class AIAssistant {
7173
}
7274

7375
private async run<T>(option: RunOption): Promise<T> {
74-
const { model = this.model, context, HTMLContent, userContent } = option
76+
const {
77+
model = this.model,
78+
context,
79+
HTMLContent,
80+
userContent,
81+
responseFormatType
82+
} = option
7583

7684
const completion = await this.openai.chat.completions.create({
7785
model,
@@ -80,12 +88,14 @@ class AIAssistant {
8088
{ role: 'user', name: 'x-crawl', content: HTMLContent },
8189
{ role: 'user', name: 'coder', content: userContent }
8290
],
83-
response_format: { type: 'json_object' }
91+
response_format: { type: responseFormatType }
8492
})
8593

86-
const result = JSON.parse(
87-
completion.choices[0].message.content ?? '{}'
88-
) as any
94+
const content = completion.choices[0].message.content
95+
const result =
96+
responseFormatType === 'json_object'
97+
? (JSON.parse(content ?? '{}') as any)
98+
: content
8999

90100
console.log(result)
91101

@@ -113,7 +123,8 @@ class AIAssistant {
113123
model,
114124
context: PARSE_ELEMENTS_CONTEXT,
115125
HTMLContent: HTML,
116-
userContent: coderContent
126+
userContent: coderContent,
127+
responseFormatType: 'json_object'
117128
})
118129

119130
return result
@@ -141,7 +152,22 @@ class AIAssistant {
141152
model,
142153
context: GET_ELEMENT_SELECTORS_CONTEXT,
143154
HTMLContent: HTML,
144-
userContent: coderContent
155+
userContent: coderContent,
156+
responseFormatType: 'json_object'
157+
})
158+
159+
return result
160+
}
161+
162+
async help(content: string, option: GetCommonOtherOption = {}) {
163+
const { model } = option
164+
165+
const result = await this.run<GetElementSelectorsResult>({
166+
model,
167+
context: HELP_CONTEXT,
168+
HTMLContent: '',
169+
userContent: content,
170+
responseFormatType: 'text'
145171
})
146172

147173
return result

src/default.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,5 @@ coder 用户:{ "message": "获取 TYPE-C 充电线。", "isFullPath": false }
173173
174174
分析: 这里没有 TYPE-C 类型的充电线, 只能将 isExist 设为 false , selectors 设为 ""。
175175
`
176+
export const HELP_CONTEXT = `我现在有一个爬虫相关的问题需要请教你。作为爬虫专家和前端专家,需要能帮我解答一下, 只需回答 coder 用户的问题。
177+
`

0 commit comments

Comments
 (0)