{"id":16916442,"url":"https://github.com/extremeheat/lxl","last_synced_at":"2025-03-22T10:32:48.867Z","repository":{"id":223207325,"uuid":"706535721","full_name":"extremeheat/LXL","owner":"extremeheat","description":"langxlang, a Node.js library to integrate LLMs into programming languages","archived":false,"fork":false,"pushed_at":"2024-08-04T05:40:24.000Z","size":189,"stargazers_count":5,"open_issues_count":4,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-10-14T19:27:11.104Z","etag":null,"topics":["human","language","natrual","programming"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/extremeheat.png","metadata":{"files":{"readme":"README.md","changelog":"HISTORY.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-10-18T06:30:30.000Z","updated_at":"2024-09-17T12:30:21.000Z","dependencies_parsed_at":"2024-03-07T21:23:55.236Z","dependency_job_id":"66bf4323-806b-403f-a241-2b13ffd7b603","html_url":"https://github.com/extremeheat/LXL","commit_stats":null,"previous_names":["extremeheat/lxl"],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/extremeheat%2FLXL","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/extremeheat%2FLXL/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/extremeheat%2FLXL/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/extremeheat%2FLXL/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/extremeheat","download_url":"https://codeload.github.com/extremeheat/LXL/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221826666,"owners_count":16887211,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["human","language","natrual","programming"],"created_at":"2024-10-13T19:27:37.068Z","updated_at":"2025-03-22T10:32:48.860Z","avatar_url":"https://github.com/extremeheat.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# langxlang\n[![NPM version](https://img.shields.io/npm/v/langxlang.svg)](http://npmjs.com/package/langxlang)\n[![Build Status](https://github.com/extremeheat/LXL/actions/workflows/ci.yml/badge.svg)](https://github.com/extremeheat/LXL/actions/workflows/)\n[![Gitpod ready-to-code](https://img.shields.io/badge/Gitpod-ready--to--code-blue?logo=gitpod)](https://gitpod.io/#https://github.com/extremeheat/LXL)\n\nLangXLang (LXL) is a Node.js library and toolkit for using large language models (LLMs) inside software applications.\n\nLXL supports function calling, caching, prompt templating role play, and building complex conversational flows with LLMs.\n\nSupports OpenAI models and Google's Gemini models, as well as any other models that expose an OpenAI-compatible API. Some supported models include:\n* OpenAI: `gpt-4o`, `gpt-4`, `gpt-3.5-turbo`,  (or any specific gpt- model listed [here](https://platform.openai.com/docs/models/))\n* Google Gemini: `gemini-1.5-pro-latest`, `gemini-1.0-pro` \n\u003c!-- * Google Legacy PaLM2: `text-bison-001`, `text-bison-002`, `palm-2` --\u003e\n\n## Installation\n```coffee\nnpm install langxlang\n```\n\n## Usage\n\n```js\nconst { ChatSession, CompletionService } = require('langxlang')\n```\n\n#### Requesting a basic completion from a model\n\n*Note: as described below in API section, the keys can be read via the file system to avoid hardcoding them in the code or environment variables. The risk of API key leakage is reduced by reading from the file system, so it's recommended that you use that approach if you can.*\n\n```js\nconst service = new CompletionService({ openai: KEY, gemini: KEY })\n\nconst [response] = await service.requestCompletion(\n  'google',                 //  Model author\n  'gemini-1.5-flash',         //  Model name\n  '',                       //  System prompt (optional)\n  'Tell me about yourself'  //  User prompt\n)\nconsole.log(response.text) // Hello! I'm Gemini, a large language model created by Google AI...\n```\n\n#### Chatting with a model\n\nStart a conversation and listen to the response in chunks, streamed to the terminal with `ChatSession`:\n\n```js\nconst { ChatSession } = require('langxlang')\n\nconst session = new ChatSession(service, 'openai', 'gpt-3.5-turbo', /* empty system prompt */ '')\n\nconst q = 'Why is the sky blue?'\nconsole.log('User:', q)\nawait session.sendMessage(q, ({ content }) =\u003e { process.stdout.write(content) })\n\nconst q2 = 'What about on the poles?'\nconsole.log('User:', q2)\nawait session.sendMessage(q2, ({ content }) =\u003e { process.stdout.write(content) })\n```\n\n#### Using functions\n\n`ChatSession` provides abstractions for function calling as well as storing conversations. Models can call functions to get data to answer or \nperform actions based on the user's queries.\n\nIn the example below, we create a ChatSession that is initialized to use the `google` model `gemini-1.5-flash` with an empty system prompt.\nIn the final argument to the ChatSession constructor, we pass in an options object that has  `functions` property. This property is an object that maps function names to functions, those that are callable by the model.\n\nSince the model needs additional descriptions about the function, we add a `.description` property to the function which is passed to the model.\nAs there are no parameters to the function, we don't need to specify any additional parameter information. When called, getTime() will return\na string that will be shown to the model so it can use that data to generate a response to the user's question.\n\n```js\nconst { ChatSession } = require('langxlang')\n\nfunction getTime () {\n  return new Date().toLocaleTimeString()\n}\ngetTime.description = 'Get the current time'\n\nconst session = new ChatSession(service, 'google', 'gemini-1.5-flash', /* empty system prompt */ '', {\n  functions: { getTime }\n})\nsession.sendMessage('What time is it?').then(console.log)\n```\n\nSee a running example in `examples/streaming.js`.\n\n## API\n\n### CompletionService\n\n#### `constructor(apiKeys: { openai: string, google: string })`\n\nCreates an instance of completion service.\nNote: as an alternative to explicitly passing the API keys in the constructor you can: \n* set the `OPENAI_API_KEY` and `GEMINI_API_KEY` environment variables.\n* or, define the keys inside `/.local/share/lxl-cache.json` (linux), `~/Library/Application Support/lxl-cache.json` (mac), or `%appdata%\\lxl-cache.json` (windows) with the structure\n`{\"keys\": {\"openai\": \"your-openai-key\", \"gemini\": \"your-gemini-key\"}}`\n\n#### `async requestCompletion(author: string, model: string, systemPrompt: string, userPrompt: string)`\n\nRequest a non-streaming completion from the model.\n\n#### `requestChatCompletion(author: string, model: Model, options: { messages: Message[], generationOptions: CompletionOptions }, chunkCb: ChunkCb): Promise\u003cCompletionResponse[]\u003e`\n\nRequest a completion from the model with a sequence of chat messages which have roles. A message should look like\n`{ role: 'user', content: 'Hello!' }` or `{ role: 'system', content: 'Hi!' }`. The `role` can be either `user`, `system` or `assistant`, no\nmatter the model in use.\n\n### ChatSession\n\n#### `constructor(completionService: CompletionService, author: string, model: string, systemPrompt: string)`\n\nChatSession is for back and forth conversation between a user an an LLM.\n\n#### `async sendMessage (message: string, chunkCallback: ({ textDelta: string }) =\u003e void)`\n\nSend a message to the LLM and receive a response as return value. The chunkCallback\ncan be defined to listen to bits of the message stream as it's being written by the LLM.\n\n\n### Prompt loading utilities\nLXL provides a templating system, which is described in detail [here](./docs/MarkdownProcessing.md).\nThe relevant exposed LXL functions are:\n* `loadPrompt(text: string, variables: Record\u003cstring, string\u003e): string` - Load a text prompt with the given variables\n```js\nloadPrompt(\"Hello, may name is %%%(NAME)%%%\", { NAME: \"Omega\" })\n// \"Hello, may name is Omega\"\n```\n* `importPromptSync(path: string, variables: Record\u003cstring, string\u003e): string` - Load a prompt from a file with the given variables\n* `importPrompt(path: string, variables: Record\u003cstring, string\u003e): Promise\u003cstring\u003e` - Load a prompt from a file with the given variables, asynchronously returning a Promise\n\n### Flow\n\nFor building complex multi-round conversations or agents, see the `Flow` class. It allows you to define a flow of messages\nand responses, and then run them in a sequence, with the ability to ask follow-up questions based on the previous responses.\n\nSee the documentation [here](./docs/flow.md).\n\n### More information\n\nFor the full API, see the [TypeScript types](./src/index.d.ts). Not all methods are documented in README, the types are\nmore exhaustive in terms of what's available.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fextremeheat%2Flxl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fextremeheat%2Flxl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fextremeheat%2Flxl/lists"}