Spaces:
Running
Running
import { OpenAIStream, StreamingTextResponse } from 'ai'; | |
import { auth } from '@/auth'; | |
import { | |
AIStream, | |
type AIStreamParser, | |
type AIStreamCallbacksAndOptions, | |
} from 'ai'; | |
import { MessageBase } from '../../../lib/types'; | |
import { fetcher } from '@/lib/utils'; | |
export const runtime = 'edge'; | |
function parseVisionAgentStream(): AIStreamParser { | |
let previous = ''; | |
return data => { | |
console.log('[Ming] ~ parseVisionAgentStream ~ data:', data); | |
// const json = JSON.parse(data) as { | |
// completion: string; | |
// stop: string | null; | |
// stop_reason: string | null; | |
// truncated: boolean; | |
// log_id: string; | |
// model: string; | |
// exception: string | null; | |
// }; | |
// // Anthropic's `completion` field is cumulative unlike OpenAI's | |
// // deltas. In order to compute the delta, we must slice out the text | |
// // we previously received. | |
// const text = json.completion; | |
// console.log('[Ming] ~ parseVisionAgentStream ~ text:', text); | |
// const delta = text.slice(previous.length); | |
// previous = text; | |
return data; | |
}; | |
} | |
function visionAgentStream( | |
res: Response, | |
cb?: AIStreamCallbacksAndOptions, | |
): ReadableStream { | |
return AIStream(res, parseVisionAgentStream(), cb); | |
} | |
function readChunks(reader: any) { | |
return { | |
async *[Symbol.asyncIterator]() { | |
let readResult = await reader.read(); | |
while (!readResult.done) { | |
yield readResult.value; | |
readResult = await reader.read(); | |
} | |
}, | |
}; | |
} | |
export async function POST(req: Request) { | |
const json = await req.json(); | |
const { messages, url } = json as { | |
messages: MessageBase[]; | |
id: string; | |
url: string; | |
}; | |
const session = await auth(); | |
if (!session?.user?.email) { | |
return new Response('Unauthorized', { | |
status: 401, | |
}); | |
} | |
const formData = new FormData(); | |
formData.append('input', JSON.stringify(messages)); | |
formData.append('image', url); | |
const fetchResponse = await fetch( | |
'https://api.dev.landing.ai/v1/agent/chat?agent_class=vision_agent', | |
// 'http://localhost:5050/v1/agent/chat?agent_class=vision_agent', | |
{ | |
method: 'POST', | |
headers: { | |
apikey: 'land_sk_DKeoYtaZZrYqJ9TMMiXe4BIQgJcZ0s3XAoB0JT3jv73FFqnr6k', | |
}, | |
body: formData, | |
}, | |
); | |
// console.log('[Ming] ~ POST ~ fetchResponse:', fetchResponse); | |
if (fetchResponse.body) { | |
return new StreamingTextResponse(fetchResponse.body); | |
} else { | |
return fetchResponse; | |
} | |
} | |