vrabota
vrabota13mo ago

Using Open AI API

I would like to call with action open API to get some data, but then I need to return somehow this data to my frontend. How can I do that? After action is executed I need to run some query? in your chat example you are executing a mutation. I don't want to store the response in DB.
7 Replies
ballingt
ballingt13mo ago
Storing the response in the DB is the simplest way to do this — you can always delete it later. But if it's important not to store the result (I'd love to hear more about this) you can return the response from the action. Call the action directly from the client, and in the action get the openai response.
vrabota
vrabotaOP13mo ago
How can access response from action on client?
const generateDomains = useAction(api.generateDomains.doSomething);
const generateDomains = useAction(api.generateDomains.doSomething);
I'm using something like this, but how can I access here a response?
ballingt
ballingt13mo ago
generateDomains is an async function which returns a promise of whatever the Convex function returns So something like
const generateDomains = useAction(api.generateDomains.doSomething);
return (<button onClick={async () => {
const response = await generateDomains(userInput);
setResponse(response);
}}Generate</button>);
const generateDomains = useAction(api.generateDomains.doSomething);
return (<button onClick={async () => {
const response = await generateDomains(userInput);
setResponse(response);
}}Generate</button>);
vrabota
vrabotaOP13mo ago
ok got it, thank you
ballingt
ballingt13mo ago
you'll also need to write your action such that it returns a valid Convex object (like { response: "this is the text response" })
vrabota
vrabotaOP13mo ago
but it's safe to do like that? let's say I want to run something with OpenAPI key, when I'm exposing that action to client view, someone can get my key?
ballingt
ballingt13mo ago
The action runs on the server, clients can't see this code. Indeed you shouldn't return a secret key from an action, you should just return the data the client needs.

Did you find this page helpful?