@@ -2,7 +2,8 @@ import OpenAI, { ClientOptions } from 'openai'
2
2
3
3
import {
4
4
PARSE_ELEMENTS_CONTEXT ,
5
- GET_ELEMENT_SELECTORS_CONTEXT
5
+ GET_ELEMENT_SELECTORS_CONTEXT ,
6
+ HELP_CONTEXT
6
7
} from './default'
7
8
8
9
type Model =
@@ -38,6 +39,7 @@ interface RunOption {
38
39
context : string
39
40
HTMLContent : string
40
41
userContent : string
42
+ responseFormatType : 'text' | 'json_object'
41
43
}
42
44
43
45
interface ParseElementsContentOption {
@@ -71,7 +73,13 @@ class AIAssistant {
71
73
}
72
74
73
75
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
75
83
76
84
const completion = await this . openai . chat . completions . create ( {
77
85
model,
@@ -80,12 +88,14 @@ class AIAssistant {
80
88
{ role : 'user' , name : 'x-crawl' , content : HTMLContent } ,
81
89
{ role : 'user' , name : 'coder' , content : userContent }
82
90
] ,
83
- response_format : { type : 'json_object' }
91
+ response_format : { type : responseFormatType }
84
92
} )
85
93
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
89
99
90
100
console . log ( result )
91
101
@@ -113,7 +123,8 @@ class AIAssistant {
113
123
model,
114
124
context : PARSE_ELEMENTS_CONTEXT ,
115
125
HTMLContent : HTML ,
116
- userContent : coderContent
126
+ userContent : coderContent ,
127
+ responseFormatType : 'json_object'
117
128
} )
118
129
119
130
return result
@@ -141,7 +152,22 @@ class AIAssistant {
141
152
model,
142
153
context : GET_ELEMENT_SELECTORS_CONTEXT ,
143
154
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'
145
171
} )
146
172
147
173
return result
0 commit comments