/hypercode

The hypercode npm library built with Node.js and Typescript

Primary LanguageTypeScriptMIT LicenseMIT

Hypercode

Hypercode is a npm package for Node.js that allows you to consume the Hyper APIs easily in your JavaScript or TypeScript projects with complete type-safety. Hypercode handles the complexity of context management and response formatting, allowing you to focus on creating dynamic and intelligent features that enhance the user experience. It is a friendly npm package that makes it easy to get live, structured LLM responses with custom contexts in useful formats like integers, booleans, strings, and more.

Create a free account today on Hyper to start building your own custom contexts, integrating them into your applications, generating API keys, and using them in your projects with Hypercode.

Find the npm package here

npm version

There are 3 main components to Hypercode:

  • Context Management (get information about the created contexts, more methods are coming soon) - Learn More
  • Response Formatting (get a response in a specific format or data type) - Learn More
  • Embeddings Search (perform nuanced searches across integrated third-party data sources and internal documents) - Learn More

Here's a quick example of how you can use Hypercode to get a boolean response:

const { data: isEarthFlat } = await hyper.types.boolean('Is the earth flat?');

console.log(isEarthFlat); // false

You can also pass the information along with your queries in the form of context. Context represents bundles of live data with relevance to the query, ensuring the LLM is given all the information necessary to produce an accurate response. You can build context objects in the Hyper app, then use them in Hypercode:

const { data: productLaunch } = await hyper.types.string(
  'When is the product launch?',
  {
    contextId: 'product-roadmap-context-id',
  },
);

console.log(productLaunch); // "The product launch is on December 11, 2023"

NOTE: A context object is a collection of resources made up of files, web pages, and data from integrations like Google Drive, Slack, and GitHub. When you link a resource to a Context in Hyper, we generate embeddings that stay automatically synced with changes to your data.

The combination of structure and context in Hypercode is a powerful tool for adding sophisticated natural language understanding to your apps. Instead of complex setups, you can simply write async code that directly leverages AI insights for real-time decision-making and content creation:

async function prepareEmailCampaign() {
  const todayIsHoliday = await hyper.types.boolean('Is today a holiday?', {
    contextId: 'company-holidays-context-id',
  });

  const emailSubject = await hyper.types.string(
    todayIsHoliday
      ? 'Generate a catchy subject for a post-holiday campaign'
      : 'Generate a catchy subject for a regular workday campaign',
    {
      contextId: 'spring-promo-context-id',
    },
  );

  return {
    campaign: 'Spring Promo',
    recipientList: '/lists/spring-campaign.csv',
    emailTemplate: 'templates/spring-promo.html',
    emailSubject,
    sendDate: todayIsHoliday ? 'next business day' : 'today',
  };
}

prepareEmailCampaign().then((requestBody) => {
  // Make an HTTP request to send the email
});

Getting Started

Step 1: Install Hypercode

Start by installing the Hypercode package through your preferred package manager:

Using npm

npm i hypercode

or

npm install --save hypercode

Using Yarn

yarn add hypercode

Using pnpm

pnpm add hypercode

Step 2: Set your Hyper API Key

NOTE: You need to generate an API key from the API Key Settings page in the Hyper app.

In your .env file, set your Hyper API Key:

HYPER_API_KEY="your_api_key_here"

Make sure to replace your_api_key_here with your actual Hyper API key obtained from the Hyper app.

Step 3: Import Hypercode into Your Project

Import Hypercode in your JavaScript or TypeScript file to start using it:

import { Hyper } from 'hypercode';
import dotenv from 'dotenv';

dotenv.config();

const hyper = new Hyper(process.env.HYPER_API_KEY);

// Use the `hyper` object to make different function calls

Step 4: Start Querying

Now you're ready to start querying language models with your own custom contexts created with Hypercode!

Examples

You can find different example codes in the examples folder.

Context Management

Hypercode allows you to programmatically manage your contexts that are created inside the Hyper App. You can get information about the created contexts, and more methods are coming soon. More information about contexts can be found here.

Here's a quick example of how you can use Hypercode to get all the contexts:

const { data: contexts, error } = await hyper.contexts.all();

console.log(contexts); // [{ id: 'context-id-1', name: 'Context 1', created_at: '2023-10-28T04:28:13.971776+00:00' }, { id: 'context-id-2', name: 'Context 2', created_at: '2023-11-02T22:18:44.978052+00:00' }]

Response Formatting - Types in Hypercode

Hypercode provides a variety of structured query types, allowing you to seamlessly integrate LLM responses into your applications. You can utilize context with contextId when you need the model to consider specific background information for generating a response. More information about types can be found here Here's a breakdown of the types and how they can be used:

  • string: Get a simple string answer.

    const { data: color, error } = await hyper.types.string(
      "What's the color of the sky?",
    );
    
    console.log(color); // "blue"

You can pass the contextId as an optional parameter to all the types methods. The contextId is the id of the context that you want to use for the query. Here's one example with the integer method:

  • integer: Get an integer answer.

    const { data: numberOfPatentsFiled, error } = await hyper.types.integer(
      'How many patents has the company filed since its inception?',
      { contextId: 'company-history-context-id' },
    );
    
    console.log(numberOfPatentsFiled); // 50
  • float: Get a floating-point number answer.

    const { data: averageRevenueGrowth, error } = await hyper.types.float(
      "What has been the company's average revenue growth rate over the last five years?",
      { contextId: 'financial-report-context-id' },
    );
    
    console.log(averageRevenueGrowth); // 4.7
  • boolean: Get a true or false answer.

    const { data: canCatsSeeInTheDark, error } = await hyper.types.boolean(
      'Can cats see in the dark?',
    );
    
    console.log(canCatsSeeInTheDark); // true

You can also get the result as an array of the above types. Here are the methods for that:

  • stringArray, integerArray, floatArray, booleanArray methods: Get an array of the respective type as the answer.

    // Get an array of strings as the answer
    const { data } = await hyper.types.stringArray('List all department names', {
      contextId: 'company-structure-context-id',
    });
    
    console.log(data); // ['Human Resources', 'Finance', 'Research and Development', 'Sales', 'Customer Support']
    
    // Get an array of integers as the answer
    const { data } = await hyper.types.integerArray(
      'What is the headcount for each department?',
      { contextId: 'company-structure-context-id' },
    );
    
    // Get an array of floats as the answer
    const { data } = await hyper.types.floatArray(
      'What were the customer satisfaction ratings from the last survey?',
      { contextId: 'customer-reviews-context-id' },
    );
    
    // Get an array of booleans as the answer
    const { data } = await hyper.types.booleanArray(
      'Are services meeting performance targets?',
      { contextId: 'performance-reviews-context-id' },
    );

Embeddings Search

Utilize our Embeddings Search API to perform nuanced searches across integrated third-party data sources and internal documents. Leverage the contextId to scope searches to specific business contexts for enhanced relevance. More information about search can be found here.

const { data, error } = await hyper.search.execute('quarterly sales report', {
  contextId: 'company-sales-reports-context-id',
});

console.log(data);

Wrapping Up

With Hypercode, integrating live, structured responses in specific types from language models into your application has never been easier. By handling the complexity of managing the contexts and response formatting, Hypercode empowers you to focus on creating dynamic and intelligent features that enhance the user experience. With the powerful search feature, developers can perform nuanced searchs across integrated third-party data sources and internal documents to get the most relevant results from the context.

Remember, the examples provided are just a starting point. The potential uses of Hypercode are limited only by your imagination and the needs of your application. Whether you're building smart assistants, data analysis tools, content generators, or any other AI-driven application, Hypercode is designed to streamline your workflow and bring the power of LLMs to your fingertips.

Create a free account on Hyper to start building your own custom contexts and integrating them into your applications today!

Bugs and Features

See the issues for a list of proposed features and known issues. Feel free to raise new issues.

License

Distributed under the MIT License. See LICENSE for more information. © Hyper AI Inc.