GithubHelp home page GithubHelp logo

samrahimi / g.js Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 866 KB

Node.js framework for creating and interacting with LLM chatbot personas. Supports AI-AI chat with or without a human in the loop

JavaScript 100.00%

g.js's Introduction

g.js

Pure JS framework for interacting with LLMs in a model-agnostic way and creating multimodal pipelines.

//Create a chatbot using one of the newer openai models that use the chatCompletion endpoint, any of the 3.5 turbo or gpt-4 models const adapter="openai_chat", model_id="gpt-3.5-turbo_16k", username="User", ai_name="AI"

//Or if you want to use a legacy openai model, set adapter = "openai" and model_id = "text-davinci-003", no other code changes needed!

const session = createSession("You are a helpful assistant", adapter, model_id, username) while (true) { console.log("\n\n") const query = await stdin.question(username+": ")

//Each query and the AI's reply, is by default appended to the messages[] collection that gets sent to the model
//TODO: manage the context length using various strategies (naive FIFO, summarization, retriever, ...)
const answer = await performInference(session, query, {temperature: parseFloat(temperature), max_tokens: parseFloat(maxtokens)})
console.log(ai_name+": "+ answer)

}

//Summarize text using the prompt structure and settings in templates/summarize.json const summary = await g.fromTemplate("summarize", {text: content_to_summarize})

//Transform the raw output from a computer vision model into a coherent description of the scene const description = await g.fromTemplate("image_comprehension", {concepts: [{name: "aerial", value: 0.992}, {name:"city", value: 0.98}, ...]})

What is the point of this?

Develop user-facing web and mobile apps powered by AI using your favorite web development tech stack, without the use of python. Python is an excellent language for all sorts of machine learning tasks, but it is not the first choice for building modern consumer apps (if you doubt me, check out any HuggingFace space... there is amazing work being done there, but gradio and streamlit are not designed for general purpose apps - for that you use things like React, Next.js, Express)

Extending g.js

g.js uses a plugin architecture that creates an abstract interface for querying large language models (or any model that expects text as an input and that outputs text). To add support for a new family of models, create an adapter in /adapters that takes care of submitting a prompt and parsing the response.

All adaptors must export a submit function in the form async(model, settings, session) which returns a string. If your adapter is talking to a model or API that uses unstructured text as the input and the output, start by copying openai_completion.js and change it to connect with your model.

Templates are similar to langchain templates, but with a JS template literal syntax... templates are useful for structuring traditional instruction-following or question-answering prompts, and for applying the necessary transformations to the input data, before sending it to the model. To use a template:

await g.fromTemplate("template_name", {key1: value1, key2: value2, ...})

Here is an example of a simple template for summarization:

{
"instruction": "Your job is to summarize documents. When the user gives you a document to summarize, please respond with a detailed summary, including the key points that are important, and discarding things that are unimportant. Please write like a highly skilled human author and not like an AI... and please try to capture the emotional essence of the original material in your summary. Do not chat with the user, respond only with the summary of whatever the user has submitted to you.", "context":"${data.text}", "adapter":"openai_chat",
"model":"gpt-3.5-turbo", "temperature": 0.8,
"max_tokens": 500 }

Any JS expression that works in a template literal can be used in your template definitions, as long as strings are properly escaped:

{
"instruction": "Please describe the image or scene in a coherent and detailed way, based on raw data containing features detected by an image recognition model. See context for details", "context":"${data.concepts.map(x => ${x.name} (P = ${x.value})).join('\n')}", "adapter":"openai_chat",
"model":"gpt-4", "temperature": 0.9,
"max_tokens": 512 }

g.fromTemplate generates the prompt from your data and queries the model in one step... As long as your template follows the structure of the examples, it can typically be used with any adapter and model.

Work In Progress

  • publish as npm package
  • save / load sessions from database
  • work with few shot training examples in a structured way
  • context length optimization tools (FIFO, lossy neural compression, etc)
  • create sessions by forking / branching an existing session with variety of context sharing strategies

g.js's People

Contributors

witaka avatar samrahimi avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.