1- import { OpenAIStream } from 'ai' ;
21import * as solidityFunctions from './ai-assistant/function-definitions/solidity.ts' ;
32import * as cairoFunctions from './ai-assistant/function-definitions/cairo.ts' ;
43import * as cairoAlphaFunctions from './ai-assistant/function-definitions/cairo-alpha.ts' ;
54import * as stellarFunctions from './ai-assistant/function-definitions/stellar.ts' ;
65import * as stylusFunctions from './ai-assistant/function-definitions/stylus.ts' ;
76import { saveChatInRedisIfDoesNotExist } from './services/redis.ts' ;
8- import { getOpenAiInstance } from './services/open-ai.ts' ;
9- import { getEnvironmentVariableOr } from './utils/env .ts' ;
10- import type { AiChatBodyRequest , Chat } from './ai-assistant/types/assistant.ts' ;
7+ import type { ChatMessages } from './services/open-ai.ts' ;
8+ import { createOpenAiCompletionStream } from './services/open-ai .ts' ;
9+ import type { AiChatBodyRequest } from './ai-assistant/types/assistant.ts' ;
1110import type { SupportedLanguage } from './ai-assistant/types/languages.ts' ;
1211import type {
1312 AllContractsAIFunctionDefinitions ,
1413 SimpleAiFunctionDefinition ,
1514} from './ai-assistant/types/function-definition.ts' ;
15+ import { Cors } from './utils/cors.ts' ;
1616
1717const getFunctionsContext = < TLanguage extends SupportedLanguage = SupportedLanguage > (
1818 language : TLanguage ,
19- ) : SimpleAiFunctionDefinition [ ] => {
19+ ) : { type : 'function' ; function : SimpleAiFunctionDefinition } [ ] => {
2020 const functionPerLanguages : AllContractsAIFunctionDefinitions = {
2121 solidity : solidityFunctions ,
2222 cairo : cairoFunctions ,
@@ -25,10 +25,15 @@ const getFunctionsContext = <TLanguage extends SupportedLanguage = SupportedLang
2525 stylus : stylusFunctions ,
2626 } ;
2727
28- return Object . values ( functionPerLanguages [ language ] ?? { } ) ;
28+ return ( Object . values ( functionPerLanguages [ language ] ?? { } ) as SimpleAiFunctionDefinition [ ] ) . map (
29+ functionDefinition => ( {
30+ type : 'function' ,
31+ function : functionDefinition ,
32+ } ) ,
33+ ) ;
2934} ;
3035
31- const buildAiChatMessages = ( request : AiChatBodyRequest ) : Chat [ ] => {
36+ const buildAiChatMessages = ( request : AiChatBodyRequest ) : ChatMessages => {
3237 const validatedMessages = request . messages . filter ( ( message : { role : string ; content : string } ) => {
3338 return message . content . length < 500 ;
3439 } ) ;
@@ -51,27 +56,23 @@ export default async (req: Request): Promise<Response> => {
5156 try {
5257 const aiChatBodyRequest : AiChatBodyRequest = await req . json ( ) ;
5358
54- const openai = getOpenAiInstance ( ) ;
59+ const chatMessages = buildAiChatMessages ( aiChatBodyRequest ) ;
5560
56- const aiChatMessages = buildAiChatMessages ( aiChatBodyRequest ) ;
57-
58- const response = await openai . chat . completions . create ( {
59- model : getEnvironmentVariableOr ( 'OPENAI_MODEL' , 'gpt-4o-mini' ) ,
60- messages : aiChatMessages ,
61- functions : getFunctionsContext ( aiChatBodyRequest . language ) ,
62- temperature : 0.7 ,
63- stream : true ,
64- } ) ;
65-
66- const stream = OpenAIStream ( response , {
67- onCompletion : saveChatInRedisIfDoesNotExist ( aiChatBodyRequest . chatId , aiChatMessages ) ,
61+ const openAiStream = createOpenAiCompletionStream ( {
62+ streamParams : {
63+ messages : chatMessages ,
64+ tools : getFunctionsContext ( aiChatBodyRequest . language ) ,
65+ } ,
66+ chatId : aiChatBodyRequest . chatId ,
67+ onAiStreamCompletion : async ( { chatId, chatMessages } , finalStreamResult ) =>
68+ finalStreamResult && ( await saveChatInRedisIfDoesNotExist ( chatId , chatMessages ) ( finalStreamResult ) ) ,
6869 } ) ;
6970
70- return new Response ( stream , {
71+ return new Response ( openAiStream , {
7172 headers : new Headers ( {
72- 'Access-Control-Allow-Origin' : '*' ,
73- 'Access-Control-Allow-Methods ' : 'GET, POST, OPTIONS ' ,
74- 'Content-Type ' : 'text/html; charset=utf-8 ' ,
73+ ... Cors ,
74+ 'Content-Type ' : 'application/x-ndjson; charset=utf-8 ' ,
75+ 'Cache-Control ' : 'no-transform ' ,
7576 } ) ,
7677 } ) ;
7778 } catch ( e ) {
0 commit comments