Skip to content
This repository was archived by the owner on Oct 8, 2024. It is now read-only.

Conversation

@kevinmingtarja
Copy link
Contributor

@kevinmingtarja kevinmingtarja commented Jul 29, 2024

API References:

Out of scope (for another PR):

  • Functions calling
  • Code execution

Example usage:

import { JSON } from "json-as";
import { models } from "@hypermode/functions-as";

import {
  GeminiGenerateModel,
  UserTextContent,
  ModelTextContent,
  SystemTextContent,
  GenerationConfig,
  FinishReason
} from "@hypermode/models-as/models/gemini/generate";

// This model name should match the one defined in the hypermode.json manifest file.
const modelName: string = "text-generator";

// This function generates some text based on the instruction and prompt provided.
export function generateText(instruction: string, prompt: string): string {
  // The imported ChatModel interface follows the OpenAI Chat completion model input format.
  const model = models.getModel<GeminiGenerateModel>(modelName);
  const input = model.createInput([
    new UserTextContent("Hello"),
    new ModelTextContent("Great to meet you. What would you like to know?"),
    new UserTextContent(prompt), // equivalent to new UserContent([{ text: prompt }]),
  ]);
  input.systemInstruction = new SystemTextContent(instruction);

  input.generationConfig = <GenerationConfig>{
    stopSequences: ["."],
    maxOutputTokens: 100,
    temperature: 2,
    topK: 40,
    topP: 1.0,
  };

  const output = model.invoke(input);
  const resp = output.candidates[0]
  if (resp.finishReason === FinishReason.SAFETY) {
    return JSON.stringify(resp.safetyRatings)
  }

  return output.candidates[0].content!.parts[0].text.trim();
}

Example hypermode.json

{
  "$schema": "https://manifest.hypermode.com/hypermode.json",
  "models": {
    "text-generator": {
      "sourceModel": "gemini-1.5-flash",
      "host": "gemini",
      "path": "v1beta/models/gemini-1.5-flash:generateContent"
    }
  },
  "hosts": {
    "gemini": {
      "baseUrl": "https://generativelanguage.googleapis.com/",
      "headers": {
        "X-goog-api-key": "{{API_KEY}}"
      }
    }
  }
}

@kevinmingtarja kevinmingtarja marked this pull request as ready for review July 29, 2024 18:06
@kevinmingtarja kevinmingtarja requested a review from a team July 29, 2024 18:06
@kevinmingtarja kevinmingtarja enabled auto-merge (squash) July 30, 2024 01:51
Copy link
Contributor

@mattjohnsonpint mattjohnsonpint left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great. Thanks! 🎉

@kevinmingtarja kevinmingtarja merged commit 4559e06 into main Aug 2, 2024
@kevinmingtarja kevinmingtarja deleted the kevinm/hyp-1704-add-support-for-gemini-models branch August 2, 2024 16:34
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants