skip to Main Content

I am trying to use Firebase Genkit.

I see here that I can use:

import { generate } from '@genkit-ai/ai';

generate({
  model: 'googleai/gemini-1.5-flash-latest',
  prompt: 'You are a helpful AI assistant named Walt. Say hello.',
});

Great, it works.

Now, I want to use a chat history.

As mentioned here, I use:

const result = await multiTurnPrompt.generate({
  model: llama3x70b, 
  history: [
    { role: 'user', content: [{ text: 'Hello.' }] },
    { role: 'model', content: [{ text: 'Hi there!' }] },
  ],
});

Here is my code:

import {dotprompt, promptRef } from "@genkit-ai/dotprompt";
....
const multiTurnPrompt = dotprompt();

const result = await multiTurnPrompt.generate({
  model: llama3x70b, 
  history: [
    { role: 'user', content: [{ text: 'Hello.' }] },
    { role: 'model', content: [{ text: 'Hi there!' }] },
  ],
});

console.log(await result.text());

I get error:

TypeError: multiTurnPrompt.generate is not a function

How to solve this issue ?

UPDATE

Modified code following @Michael Doyle answer:

const multiTurnPrompt = promptRef('nameOfPrompt');

const result = await multiTurnPrompt.generate({

 model: llama3x70b, 

 history: [
    { role: 'user', content: [{ text: 'Hello.' }] },
    { role: 'model', content: [{ text: 'Hi there!' }] },
  ],

});

I get the error:

"GenkitError: dotprompt: NOT_FOUND: Could not find 'nameOfPrompt.prompt' in the prompts folder." 

I don’t want to use a chat described in a chat file, I want to define the chat in my JS code as mentioned in the doc. How to do that ? Thanks.

2

Answers


  1. Generally speaking, you are getting errors, because you are mixing and matching calls to generate(...), which let’s you prompt models in an adhoc way, and dotprompt, which has prompt management features, as well as a shortcut to "run" a prompt (i.e. prompt.generate(...).

    If you want to use chat history, we can stick to generate and modify your original code as follows:

    import { generate } from '@genkit-ai/ai';
    
    generate({
      model: 'googleai/gemini-1.5-flash-latest',
      prompt: 'You are a helpful AI assistant named Walt. Say hello.',
      history: [
        { role: 'user', content: [{ text: 'Hello.' }] },
        { role: 'model', content: [{ text: 'Hi there!' }] },
      ],
    });
    

    You can save the results from generate(...) to a variable, and call toHistory() on it, and then pass that history in to the next request. For example:

    import { generate } from '@genkit-ai/ai';
    import { gemini15Flash } from '@genkit-ai/googleai';
    
    let response = await generate({
      model: gemini15Flash,
      prompt: "How do you say 'dog' in French?",
    });
    let history = response.toHistory();
    
    response = await generate({
      model: gemini15Flash,
      prompt: 'How about in Spanish?',
      history,
    });
    history = response.toHistory();
    

    The Genkit docs describe in more detail: https://firebase.google.com/docs/genkit/models#recording_message_history

    Login or Signup to reply.
  2. You should try the new chat capabilities with genkit 0.9: https://firebase.google.com/docs/genkit/chat

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search