|
| 1 | +/* |
| 2 | + * This example is part of the Modus project, licensed under the Apache License 2.0. |
| 3 | + * You may modify and use this example in accordance with the license. |
| 4 | + * See the LICENSE file that accompanied this code for further details. |
| 5 | + */ |
| 6 | + |
| 7 | +import { models, http } from "@hypermode/modus-sdk-as"; |
| 8 | + |
| 9 | +import { |
| 10 | + OpenAIChatModel, |
| 11 | + DeveloperMessage, |
| 12 | + SystemMessage, |
| 13 | + UserMessage, |
| 14 | + TextContentPart, |
| 15 | + AudioContentPart, |
| 16 | + ImageContentPart, |
| 17 | + Image, |
| 18 | + Audio, |
| 19 | +} from "@hypermode/modus-sdk-as/models/openai/chat"; |
| 20 | + |
| 21 | +// These examples demonstrate how to use audio or image data with OpenAI chat models. |
| 22 | +// Currently, audio can be used for input or output, but images can be used only for input. |
| 23 | + |
| 24 | +/** |
| 25 | + * This type is used in these examples to represent images or audio. |
| 26 | + */ |
| 27 | +class Media { |
| 28 | + // The content type of the media. |
| 29 | + contentType!: string; |
| 30 | + |
| 31 | + // The binary data of the media. |
| 32 | + // This value will be base64 encoded when used in an API response. |
| 33 | + data!: Uint8Array; |
| 34 | + |
| 35 | + // A text description or transcription of the media. |
| 36 | + text!: string; |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * This function generates an audio response based on the instruction and prompt provided. |
| 41 | + */ |
| 42 | +export function generateAudio(instruction: string, prompt: string): Media { |
| 43 | + // Note, this is similar to the generateText example, but with audio output requested. |
| 44 | + |
| 45 | + // We'll generate the audio using an audio-enabled OpenAI chat model. |
| 46 | + const model = models.getModel<OpenAIChatModel>("audio-model"); |
| 47 | + |
| 48 | + const input = model.createInput([ |
| 49 | + new SystemMessage(instruction), |
| 50 | + new UserMessage(prompt), |
| 51 | + ]); |
| 52 | + |
| 53 | + input.temperature = 0.7; |
| 54 | + |
| 55 | + // Request audio output from the model. |
| 56 | + // Note, this is a convenience method that requests audio modality and sets the voice and format. |
| 57 | + // You can also set these values manually on the input object, if you prefer. |
| 58 | + input.requestAudioOutput("ash", "wav"); |
| 59 | + |
| 60 | + const output = model.invoke(input); |
| 61 | + |
| 62 | + // Return the audio and its transcription. |
| 63 | + // Note that the message Content field will be empty for audio responses. |
| 64 | + // Instead, the text will be in the Message.Audio.Transcript field. |
| 65 | + const audio = output.choices[0].message.audio!; |
| 66 | + |
| 67 | + const media = <Media>{ |
| 68 | + contentType: "audio/wav", |
| 69 | + data: audio.data, |
| 70 | + text: audio.transcript.trim(), |
| 71 | + }; |
| 72 | + |
| 73 | + return media; |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * This function generates text that describes the image at the provided url. |
| 78 | + * In this example the image url is passed to the model, and the model retrieves the image. |
| 79 | + */ |
| 80 | +export function describeImage(url: string): string { |
| 81 | + // Note that because the model retrieves the image, any URL can be used. |
| 82 | + // However, this means that there is a risk of sending data to an unauthorized host, if the URL is not hardcoded or sanitized. |
| 83 | + // See the describeRandomImage function below for a safer approach. |
| 84 | + |
| 85 | + const model = models.getModel<OpenAIChatModel>("text-generator"); |
| 86 | + |
| 87 | + const input = model.createInput([ |
| 88 | + UserMessage.fromParts([ |
| 89 | + new TextContentPart("Describe this image."), |
| 90 | + new ImageContentPart(Image.fromURL(url)), |
| 91 | + ]), |
| 92 | + ]); |
| 93 | + |
| 94 | + const output = model.invoke(input); |
| 95 | + |
| 96 | + return output.choices[0].message.content.trim(); |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * This function fetches a random image, and then generates text that describes it. |
| 101 | + * In this example the image is retrieved by the function before passing it as data to the model. |
| 102 | + */ |
| 103 | +export function describeRandomImage(): Media { |
| 104 | + // Because this approach fetches the image directly, it is safer than the describeImage function above. |
| 105 | + // The host URL is allow-listed in the modus.json file, so we can trust the image source. |
| 106 | + |
| 107 | + // Fetch a random image from the Picsum API. We'll just hardcode the size to make the demo simple to call. |
| 108 | + const response = http.fetch("https://picsum.photos/640/480"); |
| 109 | + const data = Uint8Array.wrap(response.body); |
| 110 | + const contentType = response.headers.get("Content-Type")!; |
| 111 | + |
| 112 | + // Describe the image using the OpenAI chat model. |
| 113 | + const model = models.getModel<OpenAIChatModel>("text-generator"); |
| 114 | + |
| 115 | + const input = model.createInput([ |
| 116 | + UserMessage.fromParts([ |
| 117 | + new TextContentPart("Describe this image."), |
| 118 | + new ImageContentPart(Image.fromData(data, contentType)), |
| 119 | + ]), |
| 120 | + ]); |
| 121 | + |
| 122 | + input.temperature = 0.7; |
| 123 | + |
| 124 | + const output = model.invoke(input); |
| 125 | + |
| 126 | + // Return the image and its generated description. |
| 127 | + const text = output.choices[0].message.content.trim(); |
| 128 | + const media = <Media>{ |
| 129 | + contentType, |
| 130 | + data, |
| 131 | + text, |
| 132 | + }; |
| 133 | + |
| 134 | + return media; |
| 135 | +} |
| 136 | + |
| 137 | +/** |
| 138 | + * This function fetches a random "Harvard Sentences" speech file from OpenSpeech, and then generates a transcript from it. |
| 139 | + * The sentences are from https://www.cs.columbia.edu/~hgs/audio/harvard.html |
| 140 | + */ |
| 141 | +export function transcribeRandomSpeech(): Media { |
| 142 | + // Pick a random file number from the list of available here: |
| 143 | + // https://www.voiptroubleshooter.com/open_speech/american.html |
| 144 | + const numbers: i32[] = [ |
| 145 | + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 30, 31, 32, 34, 35, 36, 37, 38, 39, |
| 146 | + 40, 57, 58, 59, 60, 61, |
| 147 | + ]; |
| 148 | + const num = numbers[<i32>(Math.random() * numbers.length)]; |
| 149 | + |
| 150 | + // Fetch the speech file corresponding to the number. |
| 151 | + const url = `https://www.voiptroubleshooter.com/open_speech/american/OSR_us_000_00${num}_8k.wav`; |
| 152 | + const response = http.fetch(url); |
| 153 | + const data = Uint8Array.wrap(response.body); |
| 154 | + |
| 155 | + // Transcribe the audio using an audio-enabled OpenAI chat model. |
| 156 | + const model = models.getModel<OpenAIChatModel>("audio-model"); |
| 157 | + |
| 158 | + const input = model.createInput([ |
| 159 | + new DeveloperMessage( |
| 160 | + "Do not include any newlines or surrounding quotation marks in the response. Omit any explanation beyond the request.", |
| 161 | + ), |
| 162 | + UserMessage.fromParts([ |
| 163 | + new TextContentPart( |
| 164 | + "Provide an exact transcription of the contents of this audio file.", |
| 165 | + ), |
| 166 | + new AudioContentPart(Audio.fromData(data, "wav")), |
| 167 | + ]), |
| 168 | + ]); |
| 169 | + |
| 170 | + const output = model.invoke(input); |
| 171 | + |
| 172 | + // Return the audio file and its transcript. |
| 173 | + const text = output.choices[0].message.content.trim(); |
| 174 | + const media = <Media>{ |
| 175 | + contentType: "audio/wav", |
| 176 | + data, |
| 177 | + text, |
| 178 | + }; |
| 179 | + |
| 180 | + return media; |
| 181 | +} |
0 commit comments