Hi,
I can not install “openai” package as dependency for my AppServices functions.
It fails with:
Failed to install dependencies
failed to transpile node_modules/formdata-node/node_modules/node-domexception/.history/index_20210527213843.js. “formdata-node” is likely not supported yet. unknown: Unexpected token (9:2)
Does anyone how to get openai working?
2 Likes
Hi Robin! Thank you for your question!
The openai
package contains a dependency that is incompatible with the current Functions environment.
Until we resolve this issue, you can call the OpenAI API with an HTTP request. To do this:
-
Add the axios
package as a dependency to Atlas Functions.
-
Store your OpenAI API key as a secret — left sidebar > Values. Note that to be able to access the secret through the function, you should:
- Create a “Secret” with the actual content of the API key.
- Create a “Value” that is linked to the secret.
-
Use a function implementation similar to the following to send requests to the OpenAI API.
exports = async function (arg) {
const axios = require('axios').default;
const API_KEY = context.values.get("OPENAI_API_KEY");
const client = axios.create({
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${API_KEY}`
}
});
const response = await getGenAICompletions(client, arg);
return { result: response };
};
async function getGenAICompletions (client, prompt) {
const messages = [
{
role: 'user',
content: [
{
type: 'text',
text: prompt
}
]
}
];
const params = {
messages: messages,
model: 'gpt-3.5-turbo-1106',
temperature: 1,
max_tokens: 256,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0
};
let response;
try {
const apiUrl = 'https://api.openai.com/v1/chat/completions';
response = await client.post(apiUrl, params);
} catch (error) {
console.error(error);
throw new Error(error.message);
}
return response.data.choices[0].message.content;
};
Thanks for the answer.
Writing the requests by hand and error handling them is what I am doing right now. It is tediuos and error prone, especially when you start using assistant functionalities where you have to handle assistants, threads, messages, files, vector stores.
There is reason why these helper libraries exist.
What would you say if I recommend people using requests for MongoDB instead of pymongo or moongose?
Is there any plan to get this fixed?
You can use one of the official MongoDB drivers (such as pymongo or the Node.js driver) instead of relying on a serverless function. Alternatively, you can host your functions on AWS, Google Cloud, or Azure.
I can’t provide a timeline for resolving the issue with the openai
package at this time.
1 Like