@@ -39,54 +39,52 @@ Start by creating a new Trigger.dev task that takes in a Sequin change event as
3939 In your ` src/trigger/tasks ` directory, create a new file called ` create-embedding-for-post.ts ` and add the following code:
4040
4141 <CodeGroup >
42- ``` ts trigger/create-embedding-for-post.ts
43- import { task } from " @trigger.dev/sdk" ;
44- import { OpenAI } from " openai" ;
45- import { upsertEmbedding } from " ../util" ;
42+ ``` ts trigger/create-embedding-for-post.ts
43+ import { task } from " @trigger.dev/sdk" ;
44+ import { OpenAI } from " openai" ;
45+ import { upsertEmbedding } from " ../util" ;
4646
4747const openai = new OpenAI ({
48- apiKey: process .env .OPENAI_API_KEY ,
48+ apiKey: process .env .OPENAI_API_KEY ,
4949});
5050
5151export const createEmbeddingForPost = task ({
52- id: " create-embedding-for-post" ,
53- run : async (payload : {
54- record: {
55- id: number ;
56- title: string ;
57- body: string ;
58- author: string ;
59- createdAt: string ;
60- embedding: string | null ;
61- },
62- metadata: {
63- table_schema: string ,
64- table_name: string ,
65- consumer: {
66- id: string ;
67- name: string ;
68- };
69- };
70- }) => {
71- // Create an embedding using the title and body of payload.record
72- const content = ` ${payload .record .title }\n\n ${payload .record .body } ` ;
73- const embedding = (await openai .embeddings .create ({
74- model: " text-embedding-ada-002" ,
75- input: content ,
76- })).data [0 ].embedding ;
77-
78- // Upsert the embedding in the database. See utils.ts for the implementation -> ->
79- await upsertEmbedding (embedding , payload .record .id );
80-
81- // Return the updated record
82- return {
83- ... payload .record ,
84- embedding: JSON .stringify (embedding ),
52+ id: " create-embedding-for-post" ,
53+ run : async (payload : {
54+ record: {
55+ id: number ;
56+ title: string ;
57+ body: string ;
58+ author: string ;
59+ createdAt: string ;
60+ embedding: string | null ;
61+ },
62+ metadata: {
63+ table_schema: string ,
64+ table_name: string ,
65+ consumer: {
66+ id: string ;
67+ name: string ;
8568 };
86- }
87-
69+ };
70+ }) => {
71+ // Create an embedding using the title and body of payload.record
72+ const content = ` ${payload .record .title }\n\n ${payload .record .body } ` ;
73+ const embedding = (await openai .embeddings .create ({
74+ model: " text-embedding-ada-002" ,
75+ input: content ,
76+ })).data [0 ].embedding ;
77+
78+ // Upsert the embedding in the database. See utils.ts for the implementation -> ->
79+ await upsertEmbedding (embedding , payload .record .id );
80+
81+ // Return the updated record
82+ return {
83+ ... payload .record ,
84+ embedding: JSON .stringify (embedding ),
85+ };
86+ }
8887});
89-
9088````
9189
9290` ` ` ts utils.ts
0 commit comments