Welcome to the AI SDK template for the Composabase local playground. This guide will help you get started with the setup and usage of this on your local environment.
Before you begin, make sure you have cloned your project repository.
SSH:
git clone $REPO_SSH_URLHTTPS:
git clone $REPO_URLNext, install the project dependencies using one of the following commands:
yarnor
npm installTo interact with this template, you need to use the Composabase CLI, which should be installed globally. To install it, run one of the following commands:
yarn global add @composabase/clior
npm install -g @composabase/cliAs the first step, you will need to login to the Composabase Dashboard in your terminal. Use the following command:
composabase loginAfter logging in, next step will fetch the necessary environment variables from the Composabase Dashboard using this command:
composabase pull:envPull GraphQL schema from your Composabase project. This command will create the GraphQL schema file in the @composabase/client directory.
composabase pull:schemaThis step ensures that your local GraphQL schema definitions are up-to-date.
Finally, you can start the local development environment using the following command:
yarn startor
npm run startThis command launches the development server and allows you to begin working with your GraphQL queries and resolvers locally.
If everything went well, you should be able to access the Composabase GraphQL Playground at http://localhost:4000.
As part of this template we added some custom resolvers. You can find them in the src/graphql folder.
This query returns a simple string.
Arguments:
name: StringisImportant: Boolean
You can test it by executing the following query in the GraphQL Playground:
query hello {
hello(name: "John Doe", isImportant: true)
}And will answer with the following response:
{
"data": {
"hello": "Hello John Doe!"
}
}You can find this code example at the followig paths:
- Schema: src/graphql/schema.ts
- Resolver: src/graphql/resolvers/queries/hello.ts
This query returns a simple string
Arguments:
input: MyCustomInputname: StringisImportant: Boolean
You can test it by executing the following query in the GraphQL Playground:
query helloCustom {
helloCustom(input: { name: "Composabase", isImportant: true })
}And will answer with the following response:
{
"data": {
"helloCustom": "Hello Composabase!"
}
}You can find this code example at the followig paths:
- Schema: src/graphql/modules/hello-custom/index.ts
- Resolver: src/graphql/modules/hello-custom/resolvers/queries/hello.ts
Before you use this examples, make sure you add your API key tokens for the AI SDK Provider you are testing; OPENAI_API_KEY for Open AI and ANTHROPIC_API_KEY for Anthropic.
At you .env file
OPENAI_API_KEY=
ANTHROPIC_API_KEY=At your project settings https://dashboard.composabase.com/$WORKSPACE/$PROJECT/settings
For more information about AI SDK Providers support read the docs at https://sdk.vercel.ai/providers/ai-sdk-providers
This query take advantage of the generateText function from the ai sdk and allows you via arguments which AI provider you want to use between anthropic or openai.
Arguments:
provider: Provider Enum valuesanthropicandopenaimodel: Stringtype: Provider Enum valuescasual,geek,scifi(optional)
You can test it by executing the following query example in the GraphQL Playground:
query holiday {
holidayAnthropic: holiday(
provider: anthropic
model: "claude-3-haiku-20240307"
)
holidayOpenai: holiday(
provider: openai
model: "gpt-4-turbo"
type: scifi
)
}You can find this code example at the followig paths:
- Schema: src/graphql/modules/ai-sdk/index.ts
- Resolver: src/graphql/modules/ai-sdk/resolvers/queries/holiday.ts
This query take advantage of the generateObject function from the ai sdk and halps you generating structured data from your AI call.
Arguments:
dish: StringmaxIngredients: IntegershowImage: Boolean
You can test it by executing the following query example in the GraphQL Playground:
query foodRecipe {
foodRecipe(
dish: "Lasagna"
maxIngredients : 7
showImage: true
) {
description
ingredients {
name
amount
}
steps
suggestion {
starter
drink
dessert
}
image
}
}You can find this code example at the followig paths:
- Schema: src/graphql/modules/ai-sdk/index.ts
- Resolver: src/graphql/modules/ai-sdk/resolvers/queries/foodRecipe.ts
Remember that this guide assumes you have basic familiarity with command-line tools and development environments. If you encounter any issues, refer to the Composabase documentation or seek assistance from our support team.
Happy coding!