11#include " LLM.h"
2+ #include " rnexecutorch/models/llm/Types.h"
23
34#include < executorch/extension/tensor/tensor.h>
45#include < filesystem>
@@ -87,8 +88,8 @@ std::string LLM::generateMultimodal(std::string prompt,
8788 throw RnExecutorchError (RnExecutorchErrorCode::InvalidUserInput,
8889 " This model does not support multimodal input." );
8990 }
90- if (mutlimodalInputs.imageToken . empty () &&
91- mutlimodalInputs.audioToken . empty ()) {
91+ if (! mutlimodalInputs.images . has_value () &&
92+ ! mutlimodalInputs.audios . has_value ()) {
9293 throw RnExecutorchError (
9394 RnExecutorchErrorCode::InvalidUserInput,
9495 " At least one of imageToken/audioToken must be non-empty" );
@@ -99,12 +100,12 @@ std::string LLM::generateMultimodal(std::string prompt,
99100 std::vector<llm::MultimodalInput> inputs;
100101 size_t imageIdx = 0 , audioIdx = 0 , pos = 0 ;
101102 while (pos < prompt.size ()) {
102- size_t imgAt = mutlimodalInputs.imageToken . empty ()
103- ? std::string::npos
104- : prompt. find (mutlimodalInputs. imageToken , pos) ;
105- size_t audAt = mutlimodalInputs.audioToken . empty ()
106- ? std::string::npos
107- : prompt. find (mutlimodalInputs. audioToken , pos) ;
103+ size_t imgAt = mutlimodalInputs.images . has_value ()
104+ ? prompt. find (mutlimodalInputs. images . value (). token , pos)
105+ : std::string::npos ;
106+ size_t audAt = mutlimodalInputs.audios . has_value ()
107+ ? prompt. find (mutlimodalInputs. audios . value (). token , pos)
108+ : std::string::npos ;
108109 if (imgAt == std::string::npos && audAt == std::string::npos) {
109110 inputs.push_back (llm::make_text_input (prompt.substr (pos)));
110111 break ;
@@ -116,27 +117,30 @@ std::string LLM::generateMultimodal(std::string prompt,
116117 inputs.push_back (llm::make_text_input (prompt.substr (pos, at - pos)));
117118 }
118119 if (imageFirst) {
119- if (imageIdx >= mutlimodalInputs.imagePaths .size ()) {
120+ auto &images = mutlimodalInputs.images .value ();
121+ if (imageIdx >= images.paths .size ()) {
120122 throw RnExecutorchError (RnExecutorchErrorCode::InvalidUserInput,
121- " More '" + mutlimodalInputs. imageToken +
123+ " More '" + images. token +
122124 " ' placeholders than image paths" );
123125 }
124- inputs.push_back (
125- llm::make_image_input (mutlimodalInputs.imagePaths [imageIdx++]));
126- pos = at + mutlimodalInputs.imageToken .size ();
126+ inputs.push_back (llm::make_image_input (images.paths [imageIdx++]));
127+ pos = at + images.token .size ();
127128 } else {
128- if (audioIdx >= mutlimodalInputs.audioWaveforms .size ()) {
129+ auto &audios = mutlimodalInputs.audios .value ();
130+ if (audioIdx >= audios.waveforms .size ()) {
129131 throw RnExecutorchError (RnExecutorchErrorCode::InvalidUserInput,
130- " More '" + mutlimodalInputs. audioToken +
132+ " More '" + audios. token +
131133 " ' placeholders than audio waveforms" );
132134 }
133- inputs.push_back (llm::make_audio_input (
134- std::move (mutlimodalInputs. audioWaveforms [audioIdx++])));
135- pos = at + mutlimodalInputs. audioToken .size ();
135+ inputs.push_back (
136+ llm::make_audio_input ( std::move (audios. waveforms [audioIdx++])));
137+ pos = at + audios. token .size ();
136138 }
137139 }
138- if (imageIdx < mutlimodalInputs.imagePaths .size () ||
139- audioIdx < mutlimodalInputs.audioWaveforms .size ()) {
140+ if ((mutlimodalInputs.images .has_value () &&
141+ imageIdx < mutlimodalInputs.images .value ().paths .size ()) ||
142+ (mutlimodalInputs.audios .has_value () &&
143+ audioIdx < mutlimodalInputs.audios .value ().waveforms .size ())) {
140144 throw RnExecutorchError (
141145 RnExecutorchErrorCode::InvalidUserInput,
142146 " More image/audio paths provided than placeholders in prompt" );
0 commit comments