MCPcopy Create free account
hub / github.com/dzoba/gptrpg / callOpenAI

Method callOpenAI

agent/ServerAgent.js:69–91  ·  view source on GitHub ↗
(prompt, attempt)

Source from the content-addressed store, hash-verified

67 }
68
69 async callOpenAI(prompt, attempt) {
70 if (attempt > 3) {
71 return null;
72 }
73
74 if (attempt > 0) {
75 prompt = "YOU MUST ONLY RESPOND WITH VALID JSON OBJECTS\N" + prompt;
76 }
77
78 const response = await openai.createChatCompletion({
79 model: "gpt-3.5-turbo",
80 messages: [{ role: "user", content: prompt }],
81 });
82
83 console.log('OpenAI response', response.data.choices[0].message.content)
84
85 const responseObject = this.cleanAndProcess(response.data.choices[0].message.content);
86 if (responseObject) {
87 return responseObject;
88 }
89
90 return await this.callOpenAI(prompt, attempt + 1);
91 }
92
93 cleanAndProcess(text) {
94 const extractedJson = extract(text)[0];

Callers 1

processMessageMethod · 0.95

Calls 1

cleanAndProcessMethod · 0.95

Tested by

no test coverage detected