async generateResponse()

in firestore-genai-chatbot/functions/src/generative-client/google_ai.ts [62:112]


  async generateResponse(
    history: Message[],
    latestApiMessage: ApiMessage,
    options: GeminiChatOptions
  ) {
    if (!this.client) {
      throw new Error('Client not initialized.');
    }

    const model = this.client.getGenerativeModel({
      model: this.modelName,
    });

    const chatSession = model.startChat({
      history: this.messagesToApi(history),
      generationConfig: {
        topP: options.topP,
        topK: options.topK,
        temperature: options.temperature,
        maxOutputTokens: options.maxOutputTokens,
        candidateCount: options.candidateCount,
      },
      safetySettings: options.safetySettings,
    });

    let result;
    try {
      result = await chatSession.sendMessage(latestApiMessage.parts[0].text);
    } catch (e) {
      logger.error(e);
      // TODO: the error message provided exposes the API key, so we should handle this/ get the Gemini team to fix it their side.
      throw new Error(
        'Failed to generate response, see function logs for more details.'
      );
    }

    const text = result.response.text();

    if (!text) {
      throw new Error('No text returned candidate');
    }

    return {
      response: text,
      candidates:
        result.response.candidates?.map(c => c.content.parts[0].text ?? '') ??
        [],
      safetyMetadata: result.response.promptFeedback,
      history,
    };
  }