|
| 1 | +--- |
| 2 | +title: Utilizing AI Features in SettleMint - OpenAI Nodes and pgvector in Hasura |
| 3 | +description: A Guide to Building an AI-Powered Workflow with OpenAI Nodes and Vector Storage in Hasura |
| 4 | +sidebar_position: 2 |
| 5 | +keywords: [integration studio, OpenAI, Hasura, pgvector, AI, SettleMint] |
| 6 | +--- |
| 7 | + |
| 8 | +This guide will demonstrate how to use the **SettleMint Integration Studio** to create a flow that incorporates OpenAI nodes for vectorization and utilizes the `pgvector` plugin in Hasura for similarity searches. If you are new to SettleMint, check out the [Getting Started Guide](../about-settlemint/0_intro.mdx). |
| 9 | + |
| 10 | +In this guide, you will learn to create workflows that: |
| 11 | +- Use **OpenAI nodes** to vectorize data. |
| 12 | +- Store vectorized data in **Hasura** using `pgvector`. |
| 13 | +- Conduct similarity searches to find relevant matches for new queries. |
| 14 | + |
| 15 | +### Prerequisites |
| 16 | +- A SettleMint Platform account with **Integration Studio** and **Hasura** deployed |
| 17 | +- Access to the Integration Studio and Hasura consoles in your SettleMint environment |
| 18 | +- An OpenAI API key for using the OpenAI nodes |
| 19 | +- A data source to vectorize (e.g., Graph Node, Attestation Indexer, or external API endpoint) |
| 20 | + |
| 21 | +### Example Flow Available |
| 22 | +The Integration Studio includes a pre-built AI example flow that demonstrates these concepts. The flow uses the SettleMint Platform's attestation indexer as a data source, showing how to: |
| 23 | +- Fetch attestation data via HTTP endpoint |
| 24 | +- Process and vectorize the attestation content |
| 25 | +- Store vectors in Hasura |
| 26 | +- Perform similarity searches |
| 27 | + |
| 28 | +You can use this flow as a reference while building your own implementation. Each step described in this guide can be found in the example flow. |
| 29 | + |
| 30 | +--- |
| 31 | + |
| 32 | +## Part 1: Creating a Workflow to Fetch, Vectorize, and Store Data |
| 33 | + |
| 34 | +### Step 1: Set Up Vector Storage in Hasura |
| 35 | + |
| 36 | +1. Access your SettleMint's Hasura instance through the admin console. |
| 37 | + |
| 38 | +2. Create a new table called `document_embeddings` with the following columns: |
| 39 | + - `id` (type: UUID, primary key) |
| 40 | + - `embedding` (type: vector(1536)) - For storing OpenAI embeddings |
| 41 | + |
| 42 | +### Step 2: Set Up the Integration Studio Flow |
| 43 | + |
| 44 | +1. **Open Integration Studio** in SettleMint and click on **Create Flow** to start a new workflow. |
| 45 | + |
| 46 | +### Step 3: Fetch Data from an External API |
| 47 | + |
| 48 | +1. **Add an HTTP Request Node** to retrieve data from an external API, such as a document or product listing service. |
| 49 | +2. Configure the **API endpoint** and any necessary authentication settings. |
| 50 | +3. **Add a JSON Node** to parse the response data, focusing on fields like `id` and `content` for further processing. |
| 51 | + |
| 52 | +### Step 4: Vectorize Data with OpenAI Node |
| 53 | + |
| 54 | +1. **Insert an OpenAI Node** in the workflow: |
| 55 | + - Use this node to generate vector embeddings for the text data using OpenAI's Embedding API. |
| 56 | + - Configure the OpenAI node to use the appropriate model and input data, such as `text-embedding-ada-002`. |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +### Step 5: Store Vectors in Hasura with pgvector |
| 61 | + |
| 62 | +1. **Add a GraphQL Node** to save the vector embeddings and data `id` in Hasura. |
| 63 | +2. Set up a **GraphQL Mutation** to store the vectors and associated IDs in a table enabled with `pgvector`. |
| 64 | + |
| 65 | +Example Mutation: |
| 66 | +```graphql |
| 67 | +mutation insertVector($id: uuid!, $vector: [Float!]!) { |
| 68 | + insert_vectors(objects: {id: $id, vector: $vector}) { |
| 69 | + affected_rows |
| 70 | + } |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +3. Ensure correct data mapping from the fetched data and vectorized output. |
| 75 | + |
| 76 | +### Step 6: Deploy and Test the Workflow |
| 77 | + |
| 78 | +1. **Deploy the Flow** within Integration Studio and **run it** to confirm that data is fetched, vectorized, and stored in Hasura. |
| 79 | +2. **Verify Hasura Data** by checking the table to ensure vectorized entries and corresponding IDs are stored correctly. |
| 80 | + |
| 81 | +--- |
| 82 | + |
| 83 | +## Part 2: Setting Up a Similarity Search Endpoint |
| 84 | + |
| 85 | +### Step 1: Create a POST Endpoint |
| 86 | + |
| 87 | +1. **Add an HTTP POST Node** to accept a JSON payload with a `query` string to be vectorized and compared to stored data. |
| 88 | + |
| 89 | +Payload Example: |
| 90 | +```json |
| 91 | +{ |
| 92 | + "query": "input string for similarity search" |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +2. **Parse the Request** by adding a JSON node to extract the `query` field from the incoming POST request. |
| 97 | + |
| 98 | +### Step 2: Vectorize the Input Query |
| 99 | + |
| 100 | +1. **Add an OpenAI Node** to convert the incoming `query` string into a vector representation. |
| 101 | + |
| 102 | +Example Configuration: |
| 103 | +```text |
| 104 | +Model: text-embedding-ada-002 |
| 105 | +Input: {{msg.payload.query}} |
| 106 | +``` |
| 107 | + |
| 108 | +### Step 3: Perform a Similarity Search with Hasura |
| 109 | + |
| 110 | +1. **Add a GraphQL Node** to perform a vector similarity search within Hasura using the `pgvector` plugin. |
| 111 | +2. Use a **GraphQL Query** to order results by similarity, returning the top 5 most similar records. |
| 112 | + |
| 113 | +Example Query: |
| 114 | +```graphql |
| 115 | +query searchVectors($vector: [Float!]!) { |
| 116 | + vectors(order_by: {vector: {_vector_distance: $vector}}, limit: 5) { |
| 117 | + id |
| 118 | + vector |
| 119 | + } |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +3. Map the vector from the OpenAI node output as the `vector` input for the Hasura query. |
| 124 | + |
| 125 | +### Step 4: Format and Return the Results |
| 126 | + |
| 127 | +1. **Add a Function Node** to format the response, listing the top 5 matches in a structured JSON format. |
| 128 | + |
| 129 | +### Step 5: Test the Flow |
| 130 | + |
| 131 | +1. **Deploy the Flow** and send a POST request to confirm the similarity search functionality. |
| 132 | +2. **Verify Response** to ensure that the flow accurately returns the top 5 matches from the vectorized data in Hasura. |
| 133 | + |
| 134 | +--- |
| 135 | + |
| 136 | +## Next Steps |
| 137 | + |
| 138 | +Now that you have built an AI-powered workflow, here are some blockchain-specific applications you can explore: |
| 139 | + |
| 140 | +### Vectorize On-Chain Data |
| 141 | +- Index and vectorize smart contract events for similarity-based event monitoring |
| 142 | +- Create embeddings from transaction data to detect patterns or anomalies |
| 143 | +- Vectorize NFT metadata for content-based recommendations |
| 144 | +- Build semantic search for on-chain attestations |
| 145 | + |
| 146 | +### Advanced Use Cases |
| 147 | +- Combine transaction data with natural language descriptions for enhanced search |
| 148 | +- Create AI-powered analytics dashboards using vectorized blockchain metrics |
| 149 | +- Implement fraud detection by vectorizing transaction patterns |
| 150 | +- Build a semantic search engine for smart contract code and documentation |
| 151 | + |
| 152 | +### Integration Ideas |
| 153 | +- Connect to multiple blockchain indexers to vectorize data across networks |
| 154 | +- Combine off-chain and on-chain data vectors for comprehensive analysis |
| 155 | +- Set up automated alerts based on similarity to known patterns |
| 156 | +- Create a knowledge base from vectorized blockchain documentation |
| 157 | + |
| 158 | +For further resources, check out: |
| 159 | + |
| 160 | +- [SettleMint Integration Studio Documentation](https://console.settlemint.com/documentation/docs/using-platform/integration-studio/) |
| 161 | +- [Node-RED Documentation](https://nodered.org/docs/) |
| 162 | +- [OpenAI API Documentation](https://beta.openai.com/docs/) |
| 163 | +- [Hasura pgvector Documentation](https://hasura.io/docs/3.0/connectors/postgresql/native-operations/vector-search/) |
| 164 | + |
| 165 | +--- |
| 166 | + |
| 167 | +This guide should enable you to build AI-powered workflows with SettleMint's new OpenAI nodes and `pgvector` support in Hasura for efficient similarity searches. |
0 commit comments