|
1 |
| -import { |
2 |
| - Configuration, |
3 |
| - CreateCompletionResponseUsage, |
4 |
| - OpenAIApi, |
5 |
| -} from 'openai'; |
| 1 | +import { OpenAI } from 'openai'; |
6 | 2 | import { useAppState } from '../state/store';
|
7 | 3 | import { availableActions } from './availableActions';
|
8 | 4 | import { ParsedResponseSuccess } from './parseResponse';
|
@@ -47,44 +43,45 @@ export async function determineNextAction(
|
47 | 43 | return null;
|
48 | 44 | }
|
49 | 45 |
|
50 |
| - const openai = new OpenAIApi( |
51 |
| - new Configuration({ |
52 |
| - apiKey: key, |
53 |
| - }) |
54 |
| - ); |
| 46 | + const openai = new OpenAI({ |
| 47 | + apiKey: key, |
| 48 | + dangerouslyAllowBrowser: true, |
| 49 | + }); |
55 | 50 |
|
56 | 51 | for (let i = 0; i < maxAttempts; i++) {
|
57 | 52 | try {
|
58 |
| - const completion = await openai.createChatCompletion({ |
59 |
| - model: model, |
| 53 | + const completion = await openai.chat.completions.create({ |
| 54 | + model, |
60 | 55 | messages: [
|
61 | 56 | {
|
62 | 57 | role: 'system',
|
63 | 58 | content: systemMessage,
|
64 | 59 | },
|
65 | 60 | { role: 'user', content: prompt },
|
66 | 61 | ],
|
67 |
| - max_tokens: 500, |
68 |
| - temperature: 0, |
| 62 | + max_completion_tokens: 5000, |
| 63 | + reasoning_effort: model === 'o1' ? 'low' : undefined, |
| 64 | + temperature: model === 'o1' ? undefined : 0, |
69 | 65 | stop: ['</Action>'],
|
70 | 66 | });
|
71 | 67 |
|
| 68 | + console.log('completion', completion); |
| 69 | + |
72 | 70 | return {
|
73 |
| - usage: completion.data.usage as CreateCompletionResponseUsage, |
| 71 | + usage: completion.usage, |
74 | 72 | prompt,
|
75 |
| - response: |
76 |
| - completion.data.choices[0].message?.content?.trim() + '</Action>', |
| 73 | + response: completion.choices[0].message?.content?.trim() + '</Action>', |
77 | 74 | };
|
78 | 75 | } catch (error: any) {
|
79 | 76 | console.log('determineNextAction error', error);
|
80 |
| - if (error.response.data.error.message.includes('server error')) { |
| 77 | + if (error.message.includes('server error')) { |
81 | 78 | // Problem with the OpenAI API, try again
|
82 | 79 | if (notifyError) {
|
83 |
| - notifyError(error.response.data.error.message); |
| 80 | + notifyError(error.message); |
84 | 81 | }
|
85 | 82 | } else {
|
86 | 83 | // Another error, give up
|
87 |
| - throw new Error(error.response.data.error.message); |
| 84 | + throw new Error(error.message); |
88 | 85 | }
|
89 | 86 | }
|
90 | 87 | }
|
|
0 commit comments