1
1
import { openai } from "@ai-sdk/openai" ;
2
- import {
3
- logger ,
4
- metadata ,
5
- runs ,
6
- schemaTask ,
7
- task ,
8
- toolTask ,
9
- wait ,
10
- } from "@trigger.dev/sdk/v3" ;
11
- import { streamText , type TextStreamPart , tool } from "ai" ;
12
- import { setTimeout } from "node:timers/promises" ;
2
+ import { logger , metadata , schemaTask , toolTask } from "@trigger.dev/sdk/v3" ;
3
+ import { streamText , type TextStreamPart } from "ai" ;
13
4
import { z } from "zod" ;
14
5
15
6
export type STREAMS = {
16
7
openai : TextStreamPart < { getWeather : typeof weatherTask . tool } > ;
17
8
} ;
18
9
19
- export const openaiConsumer = schemaTask ( {
20
- id : "openai-consumer" ,
21
- schema : z . object ( {
22
- model : z . string ( ) . default ( "gpt-3.5-turbo" ) ,
23
- prompt : z . string ( ) . default ( "Hello, how are you?" ) ,
24
- } ) ,
25
- run : async ( { model, prompt } ) => {
26
- const handle = await openaiStreaming . trigger ( { model, prompt } ) ;
27
-
28
- let openaiCompletion = "" ;
29
-
30
- for await ( const part of runs
31
- . subscribeToRun ( handle )
32
- . withStreams < STREAMS > ( ) ) {
33
- switch ( part . type ) {
34
- case "run" : {
35
- logger . info ( "Received run chunk" , { run : part . run } ) ;
36
- break ;
37
- }
38
- case "openai" : {
39
- logger . info ( "Received OpenAI chunk" , {
40
- chunk : part . chunk ,
41
- run : part . run ,
42
- } ) ;
43
-
44
- switch ( part . chunk . type ) {
45
- case "text-delta" : {
46
- openaiCompletion += part . chunk . textDelta ;
47
- break ;
48
- }
49
- case "tool-call" : {
50
- switch ( part . chunk . toolName ) {
51
- case "getWeather" : {
52
- console . log ( "Calling getWeather tool with args" , {
53
- args : part . chunk . args ,
54
- } ) ;
55
- }
56
- }
57
- break ;
58
- }
59
- case "tool-result" : {
60
- switch ( part . chunk . toolName ) {
61
- case "getWeather" : {
62
- console . log ( "Received getWeather tool result" , {
63
- result : part . chunk . result ,
64
- } ) ;
65
- }
66
- }
67
- break ;
68
- }
69
- }
70
- }
71
- }
72
- }
73
-
74
- return { openaiCompletion } ;
75
- } ,
76
- } ) ;
77
-
78
- export const waitUntilExamples = task ( {
79
- id : "wait-until-examples" ,
80
- run : async ( ) => {
81
- await setTimeout ( 30_000 ) ;
82
- } ,
83
- } ) ;
84
-
85
10
export const weatherTask = toolTask ( {
86
11
id : "weather" ,
87
12
description : "Get the weather for a location" ,
@@ -97,11 +22,11 @@ export const weatherTask = toolTask({
97
22
} ,
98
23
} ) ;
99
24
100
- export const openaiStreaming = schemaTask ( {
101
- id : "openai-streaming " ,
102
- description : "Stream data from OpenAI to get the weather " ,
25
+ export const aiWeather = schemaTask ( {
26
+ id : "ai-weather " ,
27
+ description : "Send the fullStream from the ai SDK to the metadata system " ,
103
28
schema : z . object ( {
104
- model : z . string ( ) . default ( "chatgpt -4o-latest " ) ,
29
+ model : z . string ( ) . default ( "gpt -4o-mini " ) ,
105
30
prompt : z . string ( ) . default ( "Hello, how are you?" ) ,
106
31
} ) ,
107
32
run : async ( { model, prompt } ) => {
@@ -131,33 +56,3 @@ export const openaiStreaming = schemaTask({
131
56
return { text } ;
132
57
} ,
133
58
} ) ;
134
-
135
- export const openaiO1Model = schemaTask ( {
136
- id : "openai-o1-model" ,
137
- description : "Stream data from OpenAI to get the weather" ,
138
- schema : z . object ( {
139
- model : z . string ( ) . default ( "o1-preview" ) ,
140
- prompt : z . string ( ) . default ( "Hello, how are you?" ) ,
141
- } ) ,
142
- run : async ( { model, prompt } ) => {
143
- logger . info ( "Running OpenAI model" , { model, prompt } ) ;
144
-
145
- const result = await streamText ( {
146
- model : openai ( model ) ,
147
- prompt,
148
- experimental_continueSteps : true ,
149
- } ) ;
150
-
151
- const stream = await metadata . stream ( "openai" , result . textStream ) ;
152
-
153
- let text = "" ;
154
-
155
- for await ( const chunk of stream ) {
156
- logger . log ( "Received chunk" , { chunk } ) ;
157
-
158
- text += chunk ;
159
- }
160
-
161
- return { text } ;
162
- } ,
163
- } ) ;
0 commit comments