Skip to content

Commit

Permalink
feat(nl-to-mquery): support type defensive search
Browse files Browse the repository at this point in the history
  • Loading branch information
XuHaoJun committed Sep 26, 2024
1 parent a5dad9e commit e70b887
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 32 deletions.
2 changes: 2 additions & 0 deletions backend/backend/nl_to_mquery/myopenai.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ def create_prompt(question: str) -> str:
parts.append('3. Translated Mongodb Query, do not include db.*.find, just json, and do not include any description or explaintion.')
parts.append('4. Should not duplicate key, put conditions to "$and" query')
parts.append('5. Column "typesV2" is same thing to pokemon "types"')
parts.append('6. Pokemon has resitance(or strengths) to some attack types, is meaning column "typeDefensives" effective < 1 (include 0), else if weaknesses is effective > 1, effective == 1 is meaing no strengths or weaknesses')
parts.append('7. Someone ask 4x type resitance is meaning effective 0.25, 2x type resitance is meaning effective 0.5; 4x type weakness is meaning effective 4, 2x type weakness is meaning effective 2')
parts.append('Translate this question into Mongodb Query:')
parts.append(question)
return '\n'.join(parts)
Expand Down
74 changes: 74 additions & 0 deletions backend/backend/nl_to_mquery/pokedex.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,80 @@
"nameDisplay": "Bulbasaur",
"defaultFormNameDisplay": "",
"typesV2": ["poison", "grass"],
"typeDefensives": [
{
"attackType": "normal",
"effective": 1
},
{
"attackType": "fighting",
"effective": 2
},
{
"attackType": "flying",
"effective": 4
},
{
"attackType": "poison",
"effective": 0
},
{
"attackType": "ground",
"effective": 0.25
},
{
"attackType": "rock",
"effective": 0.5
},
{
"attackType": "bug",
"effective": 1
},
{
"attackType": "ghost",
"effective": 0
},
{
"attackType": "steel",
"effective": 1
},
{
"attackType": "fire",
"effective": 1
},
{
"attackType": "water",
"effective": 1
},
{
"attackType": "grass",
"effective": 1
},
{
"attackType": "electric",
"effective": 1
},
{
"attackType": "psychic",
"effective": 1
},
{
"attackType": "ice",
"effective": 1
},
{
"attackType": "dragon",
"effective": 1
},
{
"attackType": "dark",
"effective": 1
},
{
"attackType": "fairy",
"effective": 1
}
],
"abilities": [
{
"is_hidden": false,
Expand Down
21 changes: 19 additions & 2 deletions src/app/[lang]/pokedex/OpenAISearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useLingui } from "@lingui/react"
import { useQueryClient } from "@tanstack/react-query"
import { Search } from "lucide-react"
import * as R from "remeda"
import sift from "sift"

import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
Expand Down Expand Up @@ -40,19 +41,31 @@ export const OpenAISearch = ({ mquery, onChange }: OpenAISearchProps) => {
msg`Fairy and Psychic dual-type with at least 125 Special Attack and 100 Special Defense, knowing Psychic or Moonblast.`,
msg`Not a Fire-type, but can Flamethrower, Special Attack is at least 90, Speed is 80 or higher`,
msg`Name contain "cat", Ability is "Intimidate"`,
msg`Have type resistance to fire, grass, fairy, fighting types, and have move that is ground type and special attack power is 70 or higher`,
],
[]
)

const queryClient = useQueryClient()

const siftError = React.useMemo<any>(() => {
if (query.data?.mquery) {
try {
sift(query.data?.mquery)
} catch (error) {
return error
}
}
return null
}, [query.data?.mquery])

return (
<div className="flex flex-col gap-2">
<div className="flex items-center gap-1">
<Input
type="search"
className="min-h-[40px] md:max-w-[40%]"
placeholder={i18n._(msg`Find pokemons by question...`)}
className="md:max-w-[40%]"
value={question}
onChange={(e) => {
setEnableQuery(false)
Expand Down Expand Up @@ -80,7 +93,10 @@ export const OpenAISearch = ({ mquery, onChange }: OpenAISearchProps) => {
{query.error && (
<div className="text-red-500">{formatResponseError(query.error)}</div>
)}
<div className="flex flex-wrap gap-1">
{!query.error && siftError && (
<div className="text-red-500">can not find pokemon</div>
)}
<div className="flex flex-wrap gap-3">
{sampleQuestions.map((question, i) => (
<Button
key={`sampleQuestions[${i}]`}
Expand All @@ -90,6 +106,7 @@ export const OpenAISearch = ({ mquery, onChange }: OpenAISearchProps) => {
setEnableQuery(true)
setQuestion(i18n._(question))
}}
disabled={query.isFetching}
>
<Search className="mr-2 h-4 w-4" />
{i18n._(question)}
Expand Down
24 changes: 24 additions & 0 deletions src/components/ui/textarea.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as React from "react"

import { cn } from "@/lib/utils"

export interface TextareaProps
extends React.TextareaHTMLAttributes<HTMLTextAreaElement> {}

const Textarea = React.forwardRef<HTMLTextAreaElement, TextareaProps>(
({ className, ...props }, ref) => {
return (
<textarea
className={cn(
"flex min-h-[80px] w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50",
className
)}
ref={ref}
{...props}
/>
)
}
)
Textarea.displayName = "Textarea"

export { Textarea }
6 changes: 6 additions & 0 deletions src/domain/pokemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ export interface Pokemon2 extends Pokemon {
>
}
>
typeDefensives: TypeDefensive[]
}

interface TypeDefensive {
attackType: string
effective: number
}

export interface PokemonAbilityFk2 extends PokemonAbilityFk {
Expand Down
12 changes: 8 additions & 4 deletions src/locales/en.po
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ msgstr "Envolution"
msgid "Export All"
msgstr "Export All"

#: src/app/[lang]/pokedex/OpenAISearch.tsx:40
#: src/app/[lang]/pokedex/OpenAISearch.tsx:41
msgid "Fairy and Psychic dual-type with at least 125 Special Attack and 100 Special Defense, knowing Psychic or Moonblast."
msgstr "Fairy and Psychic dual-type with at least 125 Special Attack and 100 Special Defense, knowing Psychic or Moonblast."

#: src/app/[lang]/pokedex/OpenAISearch.tsx:54
#: src/app/[lang]/pokedex/OpenAISearch.tsx:68
msgid "Find pokemons by question..."
msgstr "Find pokemons by question..."

Expand All @@ -55,6 +55,10 @@ msgstr "Found <0>{0}</0> pokemons"
msgid "gmax"
msgstr "Gmax"

#: src/app/[lang]/pokedex/OpenAISearch.tsx:45
msgid "Have type resistance to fire, grass, fairy, fighting types, and have move that is ground type and special attack power is 70 or higher"
msgstr "Have type resistance to fire, grass, fairy, fighting types, and have move that is ground type and special attack power is 70 or higher"

#: src/app/[lang]/pokedex/[id]/PokemonDetailPage.tsx:151
msgid "Hidden Ability"
msgstr "Hidden Ability"
Expand Down Expand Up @@ -88,15 +92,15 @@ msgstr "Mega-X"
msgid "mega-y"
msgstr "Mega-Y"

#: src/app/[lang]/pokedex/OpenAISearch.tsx:42
#: src/app/[lang]/pokedex/OpenAISearch.tsx:43
msgid "Name contain \"cat\", Ability is \"Intimidate\""
msgstr "Name contain \"cat\", Ability is \"Intimidate\""

#: src/components/CommandMenu.tsx:150
msgid "No results found."
msgstr "No results found."

#: src/app/[lang]/pokedex/OpenAISearch.tsx:41
#: src/app/[lang]/pokedex/OpenAISearch.tsx:42
msgid "Not a Fire-type, but can Flamethrower, Special Attack is at least 90, Speed is 80 or higher"
msgstr "Not a Fire-type, but can Flamethrower, Special Attack is at least 90, Speed is 80 or higher"

Expand Down
12 changes: 8 additions & 4 deletions src/locales/ja.po
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ msgstr ""
msgid "Export All"
msgstr ""

#: src/app/[lang]/pokedex/OpenAISearch.tsx:40
#: src/app/[lang]/pokedex/OpenAISearch.tsx:41
msgid "Fairy and Psychic dual-type with at least 125 Special Attack and 100 Special Defense, knowing Psychic or Moonblast."
msgstr ""

#: src/app/[lang]/pokedex/OpenAISearch.tsx:54
#: src/app/[lang]/pokedex/OpenAISearch.tsx:68
msgid "Find pokemons by question..."
msgstr ""

Expand All @@ -55,6 +55,10 @@ msgstr ""
msgid "gmax"
msgstr ""

#: src/app/[lang]/pokedex/OpenAISearch.tsx:45
msgid "Have type resistance to fire, grass, fairy, fighting types, and have move that is ground type and special attack power is 70 or higher"
msgstr ""

#: src/app/[lang]/pokedex/[id]/PokemonDetailPage.tsx:151
msgid "Hidden Ability"
msgstr ""
Expand Down Expand Up @@ -88,15 +92,15 @@ msgstr ""
msgid "mega-y"
msgstr ""

#: src/app/[lang]/pokedex/OpenAISearch.tsx:42
#: src/app/[lang]/pokedex/OpenAISearch.tsx:43
msgid "Name contain \"cat\", Ability is \"Intimidate\""
msgstr ""

#: src/components/CommandMenu.tsx:150
msgid "No results found."
msgstr ""

#: src/app/[lang]/pokedex/OpenAISearch.tsx:41
#: src/app/[lang]/pokedex/OpenAISearch.tsx:42
msgid "Not a Fire-type, but can Flamethrower, Special Attack is at least 90, Speed is 80 or higher"
msgstr ""

Expand Down
12 changes: 8 additions & 4 deletions src/locales/ko.po
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ msgstr ""
msgid "Export All"
msgstr ""

#: src/app/[lang]/pokedex/OpenAISearch.tsx:40
#: src/app/[lang]/pokedex/OpenAISearch.tsx:41
msgid "Fairy and Psychic dual-type with at least 125 Special Attack and 100 Special Defense, knowing Psychic or Moonblast."
msgstr ""

#: src/app/[lang]/pokedex/OpenAISearch.tsx:54
#: src/app/[lang]/pokedex/OpenAISearch.tsx:68
msgid "Find pokemons by question..."
msgstr ""

Expand All @@ -55,6 +55,10 @@ msgstr ""
msgid "gmax"
msgstr ""

#: src/app/[lang]/pokedex/OpenAISearch.tsx:45
msgid "Have type resistance to fire, grass, fairy, fighting types, and have move that is ground type and special attack power is 70 or higher"
msgstr ""

#: src/app/[lang]/pokedex/[id]/PokemonDetailPage.tsx:151
msgid "Hidden Ability"
msgstr ""
Expand Down Expand Up @@ -88,15 +92,15 @@ msgstr ""
msgid "mega-y"
msgstr ""

#: src/app/[lang]/pokedex/OpenAISearch.tsx:42
#: src/app/[lang]/pokedex/OpenAISearch.tsx:43
msgid "Name contain \"cat\", Ability is \"Intimidate\""
msgstr ""

#: src/components/CommandMenu.tsx:150
msgid "No results found."
msgstr ""

#: src/app/[lang]/pokedex/OpenAISearch.tsx:41
#: src/app/[lang]/pokedex/OpenAISearch.tsx:42
msgid "Not a Fire-type, but can Flamethrower, Special Attack is at least 90, Speed is 80 or higher"
msgstr ""

Expand Down
12 changes: 8 additions & 4 deletions src/locales/zh-Hans.po
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ msgstr ""
msgid "Export All"
msgstr ""

#: src/app/[lang]/pokedex/OpenAISearch.tsx:40
#: src/app/[lang]/pokedex/OpenAISearch.tsx:41
msgid "Fairy and Psychic dual-type with at least 125 Special Attack and 100 Special Defense, knowing Psychic or Moonblast."
msgstr ""

#: src/app/[lang]/pokedex/OpenAISearch.tsx:54
#: src/app/[lang]/pokedex/OpenAISearch.tsx:68
msgid "Find pokemons by question..."
msgstr ""

Expand All @@ -55,6 +55,10 @@ msgstr ""
msgid "gmax"
msgstr ""

#: src/app/[lang]/pokedex/OpenAISearch.tsx:45
msgid "Have type resistance to fire, grass, fairy, fighting types, and have move that is ground type and special attack power is 70 or higher"
msgstr ""

#: src/app/[lang]/pokedex/[id]/PokemonDetailPage.tsx:151
msgid "Hidden Ability"
msgstr ""
Expand Down Expand Up @@ -88,15 +92,15 @@ msgstr ""
msgid "mega-y"
msgstr ""

#: src/app/[lang]/pokedex/OpenAISearch.tsx:42
#: src/app/[lang]/pokedex/OpenAISearch.tsx:43
msgid "Name contain \"cat\", Ability is \"Intimidate\""
msgstr ""

#: src/components/CommandMenu.tsx:150
msgid "No results found."
msgstr ""

#: src/app/[lang]/pokedex/OpenAISearch.tsx:41
#: src/app/[lang]/pokedex/OpenAISearch.tsx:42
msgid "Not a Fire-type, but can Flamethrower, Special Attack is at least 90, Speed is 80 or higher"
msgstr ""

Expand Down
Loading

0 comments on commit e70b887

Please sign in to comment.