|
| 1 | +import { |
| 2 | + TypeChatLanguageModel, |
| 3 | + createJsonTranslator, |
| 4 | + TypeChatJsonTranslator, |
| 5 | + MultimodalPromptContent, |
| 6 | + PromptContent, |
| 7 | +} from "typechat"; |
| 8 | +import { TypeScriptJsonValidator } from "typechat/ts"; |
| 9 | + |
| 10 | +export function createCrosswordActionTranslator<T extends object>( |
| 11 | + model: TypeChatLanguageModel, |
| 12 | + validator: TypeScriptJsonValidator<T>, |
| 13 | + crosswordImage: string |
| 14 | +): TypeChatJsonTranslator<T> { |
| 15 | + const _imageContent = crosswordImage; |
| 16 | + |
| 17 | + const _translator = createJsonTranslator(model, validator); |
| 18 | + _translator.createRequestPrompt = createRequestPrompt |
| 19 | + |
| 20 | + return _translator; |
| 21 | + |
| 22 | + function createRequestPrompt(request: string): PromptContent { |
| 23 | + const screenshotSection = getScreenshotPromptSection(_imageContent); |
| 24 | + const contentSections = [ |
| 25 | + { |
| 26 | + type: "text", |
| 27 | + text: "You are a virtual assistant that can help users to complete requests by interacting with the UI of a webpage.", |
| 28 | + }, |
| 29 | + ...screenshotSection, |
| 30 | + { |
| 31 | + type: "text", |
| 32 | + text: ` |
| 33 | + Use the layout information provided to answer user queries. |
| 34 | + The responses should be translated into JSON objects of type ${_translator.validator.getTypeName()} using the typescript schema below: |
| 35 | + |
| 36 | + ''' |
| 37 | + ${_translator.validator.getSchemaText()} |
| 38 | + ''' |
| 39 | + `, |
| 40 | + }, |
| 41 | + { |
| 42 | + type: "text", |
| 43 | + text: ` |
| 44 | + The following is a user request: |
| 45 | + ''' |
| 46 | + ${request} |
| 47 | + ''' |
| 48 | + The following is the assistant's response translated into a JSON object with 2 spaces of indentation and no properties with the value undefined: |
| 49 | + `, |
| 50 | + }, |
| 51 | + ] as MultimodalPromptContent[]; |
| 52 | + |
| 53 | + return contentSections; |
| 54 | + } |
| 55 | + |
| 56 | + function getScreenshotPromptSection(screenshot: string | undefined) { |
| 57 | + let screenshotSection = []; |
| 58 | + if (screenshot) { |
| 59 | + screenshotSection.push({ |
| 60 | + type: "text", |
| 61 | + text: "Here is a screenshot of the currently visible webpage", |
| 62 | + }); |
| 63 | + |
| 64 | + screenshotSection.push({ |
| 65 | + type: "image_url", |
| 66 | + image_url: { |
| 67 | + url: screenshot, |
| 68 | + detail: "high" |
| 69 | + }, |
| 70 | + }); |
| 71 | + |
| 72 | + screenshotSection.push({ |
| 73 | + type: "text", |
| 74 | + text: `Use the top left corner as coordinate 0,0 and draw a virtual grid of 1x1 pixels, |
| 75 | + where x values increase for each pixel as you go from left to right, and y values increase |
| 76 | + as you go from top to bottom. |
| 77 | + `, |
| 78 | + }); |
| 79 | + } |
| 80 | + return screenshotSection; |
| 81 | + } |
| 82 | +} |
0 commit comments