-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add Cloudflare AiSearch component
#6777
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
Closed
Closed
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9e73fb2
Add Cloudflare `AiSearch` component
msdbcardoso c77ee43
Merge branch 'dev' into add-cloudflare-ai-search
msdbcardoso 5ba3167
Merge branch 'dev' into add-cloudflare-ai-search
msdbcardoso 76beafc
Fix AiSearch syntax errors, add type mappings, and add example
msdbcardoso 289ff1c
Expand AI Search example with full lifecycle
msdbcardoso 8df49c1
Clarify default namespace in AI Search example
msdbcardoso c1716f2
Rewrite AI Search examples for completeness and consistency
msdbcardoso File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,216 @@ | ||
| import { ComponentResourceOptions, Output, output } from "@pulumi/pulumi"; | ||
| import { Component } from "../component"; | ||
| import { Link } from "../link"; | ||
| import { Input } from "../input"; | ||
| import { binding } from "./binding"; | ||
|
|
||
| export interface AiSearchArgs { | ||
| /** | ||
| * The name of the AI Search instance to bind to. | ||
| * | ||
| * Use this when you know which instance you need at deploy time. The instance | ||
| * must exist in the `default` namespace. | ||
| * | ||
| * You must specify either `instance` or `namespace`, but not both. | ||
| * | ||
| * @example | ||
| * ```ts title="sst.config.ts" | ||
| * const search = new sst.cloudflare.AiSearch("MySearch", { | ||
| * instance: "my-docs-index" | ||
| * }); | ||
| * ``` | ||
| */ | ||
| instance?: Input<string>; | ||
| /** | ||
| * The name of the AI Search namespace to bind to. | ||
| * | ||
| * Use this when you need access to multiple instances at runtime. You can | ||
| * get, create, list, and delete instances within the namespace. | ||
| * | ||
| * A default namespace is created automatically for every account. If the | ||
| * namespace does not exist, it will be created on deploy. | ||
| * | ||
| * You must specify either `instance` or `namespace`, but not both. | ||
| * | ||
| * @example | ||
| * ```ts title="sst.config.ts" | ||
| * const search = new sst.cloudflare.AiSearch("MySearch", { | ||
| * namespace: "my-namespace" | ||
| * }); | ||
| * ``` | ||
| */ | ||
| namespace?: Input<string>; | ||
| } | ||
|
|
||
| /** | ||
| * The `AiSearch` component lets you add a [Cloudflare AI Search](https://developers.cloudflare.com/ai-search/) | ||
| * binding to your app. | ||
| * | ||
| * AI Search is a managed search service. Connect a website, an R2 bucket, or upload | ||
| * your own documents, and AI Search indexes your content for natural language queries. | ||
| * | ||
| * There are two types of bindings: | ||
| * | ||
| * - **Instance binding**: Binds directly to a single AI Search instance. Use this when | ||
| * you know which instance you need at deploy time. | ||
| * - **Namespace binding**: Binds to a namespace that can contain multiple instances. | ||
| * Use this when you need to access or manage multiple instances at runtime. | ||
| * | ||
| * @example | ||
| * | ||
| * #### Instance binding | ||
| * | ||
| * Bind to a specific AI Search instance. | ||
| * | ||
| * ```ts title="sst.config.ts" | ||
| * const search = new sst.cloudflare.AiSearch("MySearch", { | ||
| * instance: "my-docs-index" | ||
| * }); | ||
| * ``` | ||
| * | ||
| * #### Namespace binding | ||
| * | ||
| * Bind to a namespace to access multiple instances. | ||
| * | ||
| * ```ts title="sst.config.ts" | ||
| * const search = new sst.cloudflare.AiSearch("MySearch", { | ||
| * namespace: "my-namespace" | ||
| * }); | ||
| * ``` | ||
| * | ||
| * #### Link to a worker | ||
| * | ||
| * You can link AI Search to a worker. | ||
| * | ||
| * ```ts {3} title="sst.config.ts" | ||
| * new sst.cloudflare.Worker("MyWorker", { | ||
| * handler: "./index.ts", | ||
| * link: [search], | ||
| * url: true | ||
| * }); | ||
| * ``` | ||
| * | ||
| * Once linked, you can use the binding to search your indexed content. | ||
| * | ||
| * For an **instance binding**, call methods directly: | ||
| * | ||
| * ```ts title="index.ts" | ||
| * const results = await env.MySearch.search({ | ||
| * messages: [{ role: "user", content: "What is Cloudflare?" }] | ||
| * }); | ||
| * ``` | ||
| * | ||
| * For a **namespace binding**, get an instance handle first: | ||
| * | ||
| * ```ts title="index.ts" | ||
| * const instance = env.MySearch.get("my-docs-index"); | ||
| * const results = await instance.search({ | ||
| * messages: [{ role: "user", content: "What is Cloudflare?" }] | ||
| * }); | ||
| * ``` | ||
| */ | ||
| export class AiSearch extends Component implements Link.Linkable { | ||
| private _instance?: Output<string>; | ||
| private _namespace?: Output<string>; | ||
|
|
||
| constructor( | ||
| name: string, | ||
| args: AiSearchArgs, | ||
| opts?: ComponentResourceOptions, | ||
| ) { | ||
| super(__pulumiType, name, args, opts); | ||
|
|
||
| if (args.instance && args.namespace) { | ||
| throw new Error( | ||
| `Cannot specify both "instance" and "namespace" for AiSearch "${name}". Choose one.`, | ||
| ); | ||
| } | ||
| if (!args.instance && !args.namespace) { | ||
| throw new Error( | ||
| `Must specify either "instance" or "namespace" for AiSearch "${name}".`, | ||
| ); | ||
| } | ||
|
|
||
| if (args.instance) { | ||
| this._instance = output(args.instance); | ||
| } | ||
| if (args.namespace) { | ||
| this._namespace = output(args.namespace); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * The name of the AI Search instance, if using an instance binding. | ||
| */ | ||
| public get instance() { | ||
| return this._instance; | ||
| } | ||
|
|
||
| /** | ||
| * The name of the AI Search namespace, if using a namespace binding. | ||
| */ | ||
| public get namespace() { | ||
| return this._namespace; | ||
| } | ||
|
|
||
| /** | ||
| * When you link AI Search, it will be available to the worker and you can | ||
| * interact with it using its binding methods. | ||
| * | ||
| * For an instance binding: | ||
| * @example | ||
| * ```ts title="index.ts" | ||
| * const results = await env.MySearch.search({ | ||
| * messages: [{ role: "user", content: "What is Cloudflare?" }] | ||
| * }); | ||
| * ``` | ||
| * | ||
| * For a namespace binding: | ||
| * @example | ||
| * ```ts title="index.ts" | ||
| * const instance = env.MySearch.get("my-docs-index"); | ||
| * const results = await instance.search({ | ||
| * messages: [{ role: "user", content: "What is Cloudflare?" }] | ||
| * }); | ||
| * ``` | ||
| * | ||
| * @internal | ||
| */ | ||
| getSSTLink() { | ||
| if (this._instance) { | ||
| const instanceName = this._instance; | ||
| return { | ||
| properties: { | ||
| instanceName, | ||
| }, | ||
| include: [ | ||
| binding({ | ||
| type: "aiSearchBindings", | ||
| properties: { | ||
| instanceName, | ||
| }, | ||
| }), | ||
| ], | ||
| }; | ||
| } | ||
|
|
||
| const namespace = this._namespace!; | ||
| return { | ||
| properties: { | ||
| namespace, | ||
| }, | ||
| include: [ | ||
| binding({ | ||
| type: "aiSearchNamespaceBindings", | ||
| properties: { | ||
| namespace, | ||
| }, | ||
| }), | ||
| ], | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| const __pulumiType = "sst:cloudflare:AiSearch"; | ||
| // @ts-expect-error | ||
| AiSearch.__pulumiType = __pulumiType; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.