-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add the backend api #10
base: main
Are you sure you want to change the base?
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
deal with the openai error |
I think that we need to draw the backend flow, when the frontend will need to execute a query or mutation and it will help to ensure that everything is okay. |
|
||
const response = await openai.completions.create({ | ||
model: "text-davinci-003", | ||
prompt: `Suggest ${args.numberOfPlayer} words for an topic that is '${args.topic}'`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this prompt is not so specific as it has to be. I just checked it in ChatGPT and the answer is not an array (as expected). You must be very specific, eg:
const prompt = `Give me a list of ${args.nnumberOfPlayer} word related
to the topic "${args.topic}" as a json array format. Don't
include anything else in your response. The json array
must be as this: '{"words": ["word1", "word2", "word3"]}'.`
This prompt is just an example and we're not sure that it works. Maybe you need to test it in ChatGPT and show in this pr some examples of what ChatGPT returned. In your code, you will need to check if the response is in the expected format and try again if it fails. Limit the tries to 10 or something like that to avoid executing infinite calls to openai.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This api I can't test, because I can't use it In China,So if you have the pay key, you can fix this problem
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can test using ChatGPT from https://chat.openai.com/ , but I'll try to get a key
convex/openai.ts
Outdated
// ! TODO: check if this acutally works!!! | ||
const checkTopicResponse = await openai.completions.create({ | ||
model: "text-davinci-003", | ||
prompt: `if the topic '${args.topic}' is a good topic`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You have to send prompts as you're talking with other person. For example here it can be:
const prompt = `Is the topic '${args.topic}' a good topic?
A good topic must be a single or two word topic like 'animal',
'plants', 'cars', 'celebrities', etc. Anything that can be suitable
for all audiences. Your answert must be just false or true, with
any other word included.`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This api I can't test, because I can't use it In China,So if you have the pay key, you can fix this problem
if (games?.words?.includes(args.wordFromUser)) { | ||
const player = await ctx.db.get(args.playerId); | ||
const newScore = player?.score ? player?.score + 1 : 1; | ||
ctx.db.patch(args.playerId, { score: newScore }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You have to check if the user have not answered it yet. If you don't check it, I can spam any of the word and get unlimited score. Also, check that the word is not my word. I shouldn't be able to get score by sending my own word.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, here can change use your idea, thank you
convex/schema.ts
Outdated
import { defineSchema, defineTable } from "convex/server"; | ||
import { v } from "convex/values"; | ||
|
||
// Why is there a different schema for game and room??? - Jakob |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think we can merge the schemas
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's the office code, And I use the Convex at the first time, so I don't know may be you are right, but I don't know, sorry
convex/rooms.ts
Outdated
if (players.length > 8) { | ||
return { | ||
message: "The backend got an error", | ||
error: "the maxinum of a room is 8!", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maximum*
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, Thanks a lot
convex/rooms.ts
Outdated
.collect(); | ||
if (players.length > 8) { | ||
return { | ||
message: "The backend got an error", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why message and error? We can just send one of them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, here I must check the rooms all player's amount, because a room can only maximum 8 player
convex/player.ts
Outdated
.collect(); | ||
for (let i = 0; i < playerList.length; i++) { | ||
const player = playerList[i]; | ||
if (player === undefined) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this needed? I think you can just do something like:
if (player) {
await ctx.db...
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, here I will change the code, thank you
convex/rooms.ts
Outdated
.query("player") | ||
.filter((q) => q.eq(q.field("roomId"), args.roomId)) | ||
.collect(); | ||
if (players.length > 8) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we check if the room is full (players.length === 8)??
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll not let 4 players to play for example? Only 8 players?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, we have to make the maximum amount parametrizable. Is not a good idea to manually write 8
in all the code.
convex/openai.ts
Outdated
} else { | ||
console.error("The Open AI return nothing"); | ||
return { | ||
message: "The backend got an error", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We still has two error messages here
convex/rooms.ts
Outdated
.query("player") | ||
.filter((q) => q.eq(q.field("roomId"), args.roomId)) | ||
.collect(); | ||
if (players.length > 8) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll not let 4 players to play for example? Only 8 players?
convex/rooms.ts
Outdated
.query("player") | ||
.filter((q) => q.eq(q.field("roomId"), args.roomId)) | ||
.collect(); | ||
if (players.length > 8) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, we have to make the maximum amount parametrizable. Is not a good idea to manually write 8
in all the code.
apiKey: process.env.OPENAI_API_KEY, // This is also the default, can be omitted | ||
}); | ||
// start a game | ||
export const startGames = action({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a check that if the user that sends the the request to start the game is the creator of the room
I completed some backend api