Skip to content

Commit 6f16c12

Browse files
committed
fix: parse json
1 parent c9a9740 commit 6f16c12

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

src/client.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Actions from '@cocreate/actions'
1010
// console.log('test', apifromCrud)
1111
const apiKey = localStorage.getItem('openAiKey');
1212
const apiUrl = 'https://api.openai.com/v1/chat/completions';
13-
const model = 'gpt-3.5-turbo'
13+
const model = 'gpt-4-1106-preview'
1414
const max_tokens = 3300;
1515
const temperature = 0.6;
1616
const n = 1;
@@ -84,11 +84,15 @@ async function send(conversation) {
8484
})
8585

8686
if (data) {
87-
const content = data.chat.choices[0].message.content;
88-
console.log(content)
87+
let content = data.chat.choices[0].message.content;
88+
content = content.replace(/```json\n|\n```/g, '');
89+
content = JSON.parse(content)
8990
let responseElement = document.querySelector('[openai="response"]')
90-
if (responseElement)
91-
responseElement.setValue(content)
91+
if (responseElement) {
92+
let preValue = responseElement.getValue()
93+
let newValue = [...content, ...preValue]
94+
responseElement.setValue(newValue)
95+
}
9296
}
9397

9498
// const object = extractObjectFromCode(content);
@@ -213,8 +217,21 @@ async function openaiAction(form) {
213217
if (!['system', 'user', 'assistant'].includes(role))
214218
continue
215219
let content = await element.getValue()
220+
if (!content)
221+
continue
222+
223+
if (element.getAttribute('key') === 'careers') {
224+
let Content = []
225+
if (typeof content === 'string')
226+
content = JSON.parse(content)
227+
for (let contentItem of content)
228+
Content.push(contentItem.name)
229+
content = { previousCareers: Content }
230+
}
231+
216232
if (typeof content === 'string')
217233
conversation.push({ role, content })
234+
218235
// if (Array.isArray(content))
219236
// conversation.push({ role, content })
220237
if (typeof content === 'object') {

0 commit comments

Comments
 (0)