@@ -115,47 +115,47 @@ export default {
115115 return new Response ( 'ok' ) ;
116116 } )
117117 . on ( ':photo' , async ( bot : TelegramExecutionContext ) => {
118+ await bot . sendTyping ( ) ;
118119 const file_id : string = bot . update . message ?. photo ?. pop ( ) ?. file_id ?? '' ;
119120 const file_response = await bot . getFile ( file_id ) ;
120121 const blob = await file_response . arrayBuffer ( ) ;
121- if ( bot . update . message ?. caption ) {
122- const inputs = {
123- prompt : bot . update . message . caption ,
124- image : [ ...new Uint8Array ( blob ) ] ,
125- } ;
126- let response ;
127- try {
128- response = await env . AI . run ( '@cf/runwayml/stable-diffusion-v1-5-img2img' , inputs ) ;
129- } catch ( e ) {
130- console . log ( e ) ;
131- await bot . reply ( `Error: ${ e as string } ` ) ;
132- return new Response ( 'ok' ) ;
133- }
134- const id = crypto . randomUUID ( ) ;
135- await env . R2 . put ( id , response ) ;
136- await bot . replyPhoto ( `https://r2.seanbehan.ca/${ id } ` ) ;
137- ctx . waitUntil (
138- wrapPromise ( async ( ) => {
139- await env . R2 . delete ( id ) ;
140- } , 500 ) ,
141- ) ;
142- } else {
143- const input = {
144- image : [ ...new Uint8Array ( blob ) ] ,
145- prompt : 'Generate a caption for this image' ,
146- max_tokens : 512 ,
147- } ;
148- let response : AiImageToTextOutput ;
149- try {
150- // @ts -expect-error broken bindings
151- response = await env . AI . run ( '@cf/meta/llama-3.2-11b-vision-instruct' , input ) ;
152- } catch ( e ) {
153- console . log ( e ) ;
154- await bot . reply ( `Error: ${ e as string } ` ) ;
155- return new Response ( 'ok' ) ;
122+ const prompt = bot . update . message ?. caption ?? '' ;
123+ const { results } = await env . DB . prepare ( 'SELECT * FROM Messages WHERE userId=?' ) . bind ( bot . update . message ?. from . id ) . all ( ) ;
124+ const message_history = results . map ( ( col ) => ( { role : 'system' , content : col . content as string } ) ) ;
125+ const messages = [
126+ { role : 'system' , content : 'You are a friendly assistant named TuxRobot. Use lots of emojis in your responses.' } ,
127+ ...message_history ,
128+ {
129+ role : 'user' ,
130+ content : prompt ,
131+ } ,
132+ ] ;
133+ let response : AiTextGenerationOutput ;
134+ try {
135+ // @ts -expect-error broken bindings
136+ response = await env . AI . run ( '@cf/meta/llama-3.2-11b-vision-instruct' , { messages, image : [ ...new Uint8Array ( blob ) ] } ) ;
137+ } catch ( e ) {
138+ console . log ( e ) ;
139+ await bot . reply ( `Error: ${ e as string } ` ) ;
140+ return new Response ( 'ok' ) ;
141+ }
142+ if ( 'response' in response ) {
143+ if ( response . response ) {
144+ await bot . reply ( await markdown_to_html ( response . response ?? '' ) , 'HTML' ) ;
145+ await env . DB . prepare ( 'INSERT INTO Messages (id, userId, content) VALUES (?, ?, ?)' )
146+ . bind ( crypto . randomUUID ( ) , bot . update . message ?. from . id , `'[INST] ${ prompt } [/INST] \n ${ response . response } ` )
147+ . run ( ) ;
156148 }
157- await bot . replyPhoto ( file_id , ( response as unknown as { response : string } ) . response ) ;
158149 }
150+
151+ const id = crypto . randomUUID ( ) ;
152+ await env . R2 . put ( id , ( response as unknown as { response : string } ) . response ) ;
153+ await bot . replyPhoto ( file_id , ( response as unknown as { response : string } ) . response ) ;
154+ ctx . waitUntil (
155+ wrapPromise ( async ( ) => {
156+ await env . R2 . delete ( id ) ;
157+ } , 500 ) ,
158+ ) ;
159159 return new Response ( 'ok' ) ;
160160 } )
161161 . on ( 'photo' , async ( bot : TelegramExecutionContext ) => {
0 commit comments