0.9 से 1.0 पर माइग्रेट करना

Genkit 1.0 में कई सुविधाओं को बेहतर बनाया गया है, ताकि पूरी सुविधा को बेहतर बनाया जा सके. इसमें कुछ बदलाव भी किए गए हैं. अगर Genkit 0.9 का इस्तेमाल करके ऐप्लिकेशन डेवलप किए जा रहे हैं, तो Genkit के नए वर्शन पर अपग्रेड करने के बाद, आपको अपने ऐप्लिकेशन कोड को अपडेट करना होगा. इस गाइड में, सबसे अहम बदलावों के बारे में बताया गया है. साथ ही, अपने मौजूदा ऐप्लिकेशन को आसानी से माइग्रेट करने का तरीका भी बताया गया है.

बीटा एपीआई

हम एक अस्थिर, बीटा एपीआई चैनल पेश कर रहे हैं. साथ ही, सेशन, चैट, और Genkit क्लाइंट एपीआई को बीटा वर्शन में ही छोड़ रहे हैं, ताकि हम उनमें सुधार कर सकें. खास तौर पर, फ़िलहाल ये फ़ंक्शन beta नेमस्पेस में हैं:

  • ai.chat
  • ai.createSession
  • ai.loadSession
  • ai.currentSession
  • ai.defineFormat
  • ai.defineInterrupt

पुराना:

import { genkit } from 'genkit';
const ai = genkit({...})
const session = ai.createSession({ ... })

नया:

import { genkit } from 'genkit/beta';
const ai = genkit({...})
const session = ai.createSession({ ... })

पुराना:

import { runFlow, streamFlow } from 'genkit/client';

नया:

import { runFlow, streamFlow } from 'genkit/beta/client';

पेश है नया @genkit-ai/express पैकेज

इस नए पैकेज में ऐसी सुविधाएं शामिल हैं जिनकी मदद से, Genkit की मदद से Express.js सर्वर को आसानी से बनाया जा सकता है. इस बारे में ज़्यादा जानकारी के लिए, इस पेज पर जाएं.

startFlowServer को genkit ऑब्जेक्ट के हिस्से से हटाकर, इस नए @genkit-ai/express पैकेज में शामिल किया गया है. startFlowServer का इस्तेमाल करने के लिए, आपको अपने इंपोर्ट अपडेट करने होंगे.

पुराना:

const ai = genkit({ ... });
ai.startFlowServer({
  flows: [myFlow1, myFlow2],
});

नया:

import { startFlowServer } from '@genkit-ai/express';
startFlowServer({
  flows: [myFlow1, myFlow2],
});

फ़्लो में बदलाव

1.0 वर्शन में फ़्लो में कई बदलाव किए गए हैं:

  • ai.defineStreamingFlow को ai.defineFlow में मर्ज कर दिया गया है,
  • onFlow को onCallGenkit से बदल दिया गया है,
  • run को ai.run में ले जाया गया है,
  • पुष्टि करने के तरीके में बदलाव हुए हैं.

कस्टम ट्रेस ब्लॉक के लिए run फ़ंक्शन, genkit ऑब्जेक्ट का हिस्सा बन गया है. इसे ट्रिगर करने के लिए, ai.run का इस्तेमाल करें.

पुराना:

ai.defineFlow({name: 'banana'}, async (input) => {
  const step = await run('myCode', async () => {
    return 'something'
  });
})

नया:

ai.defineFlow({name: 'banana'}, async (input) => {
  const step = await ai.run('myCode', async () => {
    return 'something'
  });
})

ai.defineStreamingFlow को हटा दिया गया है. इसके बजाय, ai.defineFlow का इस्तेमाल करें. साथ ही, streamingCallback को फ़्लो फ़ंक्शन के दूसरे आर्ग्युमेंट में मौजूद फ़ील्ड में ले जाया गया है और अब इसे sendChunk कहा जाता है.

पुराना:

const flow = ai.defineStreamingFlow({name: 'banana'}, async (input, streamingCallback) => {
  streamingCallback({chunk: 1});
})

const {stream} = await flow()
for await (const chunk of stream) {
  // ...
}

नया:

const flow = ai.defineFlow({name: 'banana'}, async (input, {context, sendChunk}) => {
  sendChunk({chunk: 1});
})

const {stream, output} = flow.stream(input);
for await (const chunk of stream) {
  // ...
}

FlowAuth auth को अब context कहा जाता है. auth को कॉन्टेक्स्ट में फ़ील्ड के तौर पर ऐक्सेस किया जा सकता है:

पुराना:

ai.defineFlow({name: 'banana'}, async (input) => {
  const auth = getFlowAuth();
  // ...
})

नया:

ai.defineFlow({name: 'banana'}, async (input, { context }) => {
  const auth = context.auth;
})

onFlow को firebase-functions/https पैकेज में ले जाया गया है और इसका नाम बदलकर onCallGenkit कर दिया गया है. नीचे दिए गए स्निपेट में, इसका इस्तेमाल करने का उदाहरण दिया गया है.

पुराना

import { onFlow } from "@genkit-ai/firebase/functions";

export const generatePoem = onFlow(
  ai,
  {
    name: "jokeTeller",
    inputSchema: z.string().nullable(),
    outputSchema: z.string(),
    streamSchema: z.string(),
  },
  async (type, streamingCallback) => {
    const { stream, response } = await ai.generateStream(
      `Tell me a longish ${type ?? "dad"} joke.`
    );
    for await (const chunk of stream) {
      streamingCallback(chunk.text);
    }
    return (await response).text;
  }
);

नया:

import { onCallGenkit } from "firebase-functions/https";
import { defineSecret } from "firebase-functions/params";
import { genkit, z } from "genkit";

const apiKey = defineSecret("GOOGLE_GENAI_API_KEY");

const ai = genkit({
  plugins: [googleAI()],
  model: gemini15Flash,
});

export const jokeTeller = ai.defineFlow(
  {
    name: "jokeTeller",
    inputSchema: z.string().nullable(),
    outputSchema: z.string(),
    streamSchema: z.string(),
  },
  async (type, { sendChunk }) => {
    const { stream, response } = ai.generateStream(
      `Tell me a longish ${type ?? "dad"} joke.`
    );
    for await (const chunk of stream) {
      sendChunk(chunk.text);
    }
    return (await response).text;
  }
);

export const tellJoke = onCallGenkit({ secrets: [apiKey] }, jokeTeller);

पुष्टि करने से जुड़ी नीतियां, defineFlow से हटा दी गई हैं. पुष्टि करने की नीतियों को मैनेज करने की सुविधा अब सर्वर पर निर्भर करती है.

पुराना:

export const simpleFlow = ai.defineFlow(
  {
    name: 'simpleFlow',
    authPolicy: (auth, input) => {
      // auth policy
    },
  },
  async (input) => {
    // Flow logic here...
  }
);

यहां दिया गया स्निपेट, Express में पुष्टि करने का उदाहरण दिखाता है.

नया:

import { UserFacingError } from 'genkit';
import { ContextProvider, RequestData } from 'genkit/context';
import { expressHandler, startFlowServer } from '@genkit-ai/express';

const context: ContextProvider<Context> = (req: RequestData) => {
  return {
    auth: parseAuthToken(req.headers['authorization']),
  };
};

export const simpleFlow = ai.defineFlow(
  {
    name: 'simpleFlow',
  },
  async (input, { context }) => {
    if (!context.auth) {
      throw new UserFacingError("UNAUTHORIZED", "Authorization required.");
    }
    if (input.uid !== context.auth.uid) {
      throw new UserFacingError("UNAUTHORIZED", "You may only summarize your own profile data.");
    }
    // Flow logic here...
  }
);

const app = express();
app.use(express.json());
app.post(
  '/simpleFlow',
  expressHandler(simpleFlow, { context })
);
app.listen(8080);

// or

startFlowServer(
  flows: [withContextProvider(simpleFlow, context)],
  port: 8080
);

ज़्यादा जानकारी के लिए, पुष्टि करने से जुड़ा दस्तावेज़ देखें.

यहां दिया गया स्निपेट, Firebase के लिए Cloud Functions में पुष्टि करने का उदाहरण दिखाता है:

import { genkit } from 'genkit';
import { onCallGenkit } from 'firebase-functions/https';

const ai = genkit({ ... });;

const simpleFlow = ai.defineFlow({
  name: 'simpleFlow',
}, async (input) => {
  // Flow logic here...
});

export const selfSummary = onCallGenkit({
  authPolicy: (auth, data) => auth?.token?.['email_verified'] && auth?.token?.['admin'],
}, simpleFlow);

प्रॉम्प्ट

हमने प्रॉम्प्ट में कई बदलाव और सुधार किए हैं.

प्रॉम्प्ट और सिस्टम मैसेज के लिए अलग-अलग टेंप्लेट तय किए जा सकते हैं:

const hello = ai.definePrompt({
  name: 'hello',
  system: 'talk like a pirate.',
  prompt: 'hello {{ name }}',
  input: {
    schema: z.object({
      name: z.string()
    })
  }
});
const { text } = await hello({name: 'Genkit'});

इसके अलावा, मैसेज फ़ील्ड में एक से ज़्यादा मैसेज के प्रॉम्प्ट तय किए जा सकते हैं:

const hello = ai.definePrompt({
  name: 'hello',
  messages: '{{ role "system" }} talk like a pirate. {{ role "user" }} hello {{ name }}',
  input: {
    schema: z.object({
      name: z.string()
    })
  }
});

प्रॉम्प्ट टेंप्लेट के बजाय, फ़ंक्शन का इस्तेमाल किया जा सकता है:

ai.definePrompt({
  name: 'hello',
  prompt: async (input, { context }) => {
    return `hello ${input.name}`
  },
  input: {
    schema: z.object({
      name: z.string()
    })
  }
});

प्रॉम्प्ट में जाकर, संदर्भ (इसमें पुष्टि करने की जानकारी भी शामिल है) को ऐक्सेस किया जा सकता है:

const hello = ai.definePrompt({
  name: 'hello',
  messages: 'hello {{ @auth.email }}',
});

स्ट्रीमिंग फ़ंक्शन के लिए await की ज़रूरत नहीं होती

पुराना:

const { stream, response } = await ai.generateStream(`hi`);
const { stream, output } = await myflow.stream(`hi`);

नया:

const { stream, response } = ai.generateStream(`hi`);
const { stream, output } = myflow.stream(`hi`);

एम्बेड के लिए, रिटर्न का एक नया टाइप जोड़ा गया है

हमने कई तरह के एम्बेड के लिए सहायता जोड़ी है. एम्बेड करने की सुविधा, सिर्फ़ एक एम्बेडिंग वेक्टर के बजाय, एम्बेडिंग ऑब्जेक्ट का कलेक्शन दिखाती है. हर ऑब्जेक्ट में एक एम्बेडिंग वेक्टर और मेटाडेटा होता है.

पुराना:

const response = await ai.embed({embedder, content, options});  // returns number[]

नया:

const response = await ai.embed({embedder, content, options}); // returns Embedding[]
const firstEmbeddingVector = response[0].embedding;  // is number[]