Develop AI Agents that leverage recommendations
What you'll learn
The ❜embed TypeScript SDK allows you to fetch personalized recommendations using either a Farcaster ID (FID) or a wallet address. This lets your agent access top-ranked content for a given user—providing valuable context to make responses more relevant and personalized.
Let's dive in
To get a sample agent clone the AI Agent sample from Github
bunx degit https://github.com/ZKAI-Network/embed-sdk/examples/ai-agent embed-agent
Once we have the sample up you can go into the directory with cd embed-agent
and install dependencies.
bun install
Fill in the environment variables according to .env.example
in your .env
file and now you're ready to run the agent.
To run the agent execute the following command from the new embed-agent
folder you cloned the sample to.
bun run main.ts
You should be seeing a fun summary of the content like the following in your terminal:
Welcome to the land of intriguing digital escapades, where excitement beams and curiosity reigns supreme! 🎉 Fasten your seatbelts as we embark on a whirlwind journey through this thrilling digital feed:
1. **Auction Adventures & QR Coin Crunch:** Our QR-loving champions over at qrcoin.fun have captured hearts with their auction marvels! With a jaw-dropping $1,680 winning bid and the mesmerizing power of 1,000 $QR up for grabs, they're on a rollercoaster of unique visitors and dizzying market cap heights! 🚀
2. **Token Triumphs on Farcaster:** VjRusmayana took the Farcaster stage by storm with a treasure trove of 1092 PSTACK tokens - now that's the spirit of joyful digital anticipation! 🎊
....
The feed to pipe the top content into OpenAI to supply agent with top content from Farcaster was pulled in using the embed SDK. Let's break down how the sample works so you can code your own.
How the SDK provides context to your Agent
With the SDK we can pull in any recommendation we want. Let's get the top 10 recommended Farcaster posts for a user by their Farcaster username and then provide that to our agent.
First we need to create a SDK client, which exposes the functions we need. So let's import the SDK and get a client. We'll also need to pass our API Key to the client.
import { getClient } from "@embed-ai/sdk"
const key = process.env.API_KEY_EMBED!;
const client = getClient(key);
// now we have a client we can call
The client object we can now use to call the embed API for recommendations. It handles errors and retries for us out of the box!
Let's add some more structure to the code and get a call to OpenAI up and ready which will serve as our Agent. You can plug the context into any of your agentic workflows or AI pipelines too.
The following is a sample using gpt-4o and prepared to summarize a fee in a funny way. Feel free to look through the code calling OpenAI (make sure you have the API_KEY_OPENAI
env variable set.
import { AiLanguageModel } from "@effect/ai"
import { OpenAiClient, OpenAiLanguageModel } from "@effect/ai-openai"
import { NodeHttpClient } from "@effect/platform-node"
import { Config, Console, Effect, Layer, Redacted } from "effect"
const generateDadJoke = Effect.gen(function*() {
// call our AI Agent
const response = yield* AiLanguageModel.generateText({
prompt: `In a fun way summarize the following feed of the most interesting content`
})
console.log(response.text)
return response
})
const Gpt4o = OpenAiLanguageModel.model("gpt-4o")
const main = generateDadJoke.pipe(
Effect.provide(Gpt4o)
)
const OpenAi = OpenAiClient.layerConfig({
apiKey: Config.redacted("API_KEY_OPENAI")
})
const OpenAiWithHttp = Layer.provide(OpenAi, NodeHttpClient.layer)
// Add error handling and debugging
main.pipe(
Effect.provide(OpenAiWithHttp),
Effect.catchAll((error) => {
Console.error("Caught error:", error)
if (error && typeof error === "object" && "_tag" in error) {
Console.error("Error type:", (error as any)._tag)
} else {
Console.error("Error type: unknown")
}
Console.error("Error details:", JSON.stringify(error, null, 2))
return Effect.fail(error)
}),
Effect.runPromise
)
Now that we have a call to OpenAI prepared and working we need to provide our feed. For that we will add the code to use the embed SDK client and call the For You Feed function to get a feed by FID.
Since we query by UserId we will be using that function for getting a feed, but you could also plug in wallet addresses here and get a feed for any given wallet address.
//imports
import { getClient } from "@embed-ai/sdk"
const generateDadJoke = Effect.gen(function*() {
const key = yield* Config.redacted("API_KEY_EMBED")
const client = getClient(Redacted.value(key))
// Convert the Promise to Effect using Effect.tryPromise
const feed = yield* Effect.tryPromise({
try: () => client.feed.byUserId("16085", { top_k: 10 }),
catch: (error) => new Error(`Failed to get feed: ${error}`)
})
const response = yield* AiLanguageModel.generateText({
prompt: `In a fun way summarize the following feed of the most interesting content: ${JSON.stringify(feed)}`
})
console.log(response.text)
return response
})
// rest of the AI Agent is unchanged
Now when you run the agent you should get an output just like the sample shown initially.
We have now reconstructed the sample and fed our AI Agent with additional context about the user adding content they will enjoy.
You're now able to add social context to your AI Agents 🥳
What's next
Check out how to render a feed in a Farcaster Miniapp!
Updated 1 day ago