Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sullivan-sean committed Feb 8, 2023
0 parents commit 98f0df2
Show file tree
Hide file tree
Showing 21 changed files with 942 additions and 0 deletions.
38 changes: 38 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module.exports = {
extends: [
"airbnb-base",
"eslint:recommended",
"prettier",
"plugin:@typescript-eslint/recommended",
],
parserOptions: {
ecmaVersion: 12,
parser: "@typescript-eslint/parser",
sourceType: "module",
},
plugins: ["@typescript-eslint"],
rules: {
"@typescript-eslint/explicit-module-boundary-types": 0,
"@typescript-eslint/no-empty-function": 0,
"@typescript-eslint/no-shadow": ["error"],
"@typescript-eslint/no-use-before-define": ["error", "nofunc"],
"@typescript-eslint/no-unused-vars": ["warn", { args: "none" }],
"comma-dangle": ["error", "never"],
"camelcase": 0,
"class-methods-use-this": 0,
"import/extensions": 0,
"import/no-extraneous-dependencies": ["error", {"devDependencies": ["**/*.test.ts"]}],
"import/no-unresolved": 0,
"import/prefer-default-export": 0,
"keyword-spacing": "error",
"max-classes-per-file": 0,
"max-len": ["error", { code: 100, tabWidth: 2, ignoreComments: true }],
"no-bitwise": "off",
"no-console": 0,
"no-restricted-syntax": 0,
"no-shadow": 0,
"no-underscore-dangle": 0,
"no-use-before-define": 0,
"semi": ["error", "always"],
},
};
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules/
dist/
lib/
.turbo
.eslintcache
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

yarn precommit
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
babel.config.js
jest.config.js
.eslintrc.js
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# langchainjs
2 changes: 2 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//babel.config.js
module.exports = {presets: ['@babel/preset-env']}
11 changes: 11 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest/presets/js-with-ts',
testEnvironment: 'node',
transform: {
"^.+\\.(ts|tsx)$": "ts-jest",
"^.+\\.(js)$": "babel-jest",
},
transformIgnorePatterns: [
],
};
51 changes: 51 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"name": "langchain",
"version": "0.0.1",
"description": "Typescript bindings for langchain",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": [
"dist/"
],
"scripts": {
"build": "tsc --declaration --outDir dist/",
"lint": "eslint src",
"lint:fix": "yarn lint --fix",
"precommit": "tsc --noEmit && lint-staged",
"clean": "rm -rf dist/",
"prepack": "yarn build",
"test": "jest",
"prepare": "husky install"
},
"author": "Langchain",
"license": "MIT",
"devDependencies": {
"@babel/preset-env": "^7.20.2",
"@jest/globals": "^29.4.2",
"@tsconfig/recommended": "^1.0.2",
"@typescript-eslint/eslint-plugin": "^5.51.0",
"@typescript-eslint/parser": "^5.51.0",
"babel-jest": "^29.4.2",
"eslint": "^8.33.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-prettier": "^4.2.1",
"husky": "^8.0.3",
"jest": "^29.4.2",
"lint-staged": "^13.1.1",
"prettier": "^2.8.3",
"ts-jest": "^29.0.5",
"typescript": "^4.9.5"
},
"dependencies": {
"node-fetch": "^3.3.0",
"yaml": "^2.2.1"
},
"lint-staged": {
"src/**/*.{ts,tsx}": [
"prettier --write --ignore-unknown",
"eslint --cache --fix"
]
}
}
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export {
PromptTemplate,
BasePromptTemplate,
FewShotPromptTemplate,
} from "./prompt";
68 changes: 68 additions & 0 deletions src/prompt/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { BaseOutputParser } from "./parser";
import {
PromptTemplateInput,
SerializedPromptTemplate,
PromptTemplate,
FewShotPromptTemplateInput,
SerializedFewShotTemplate,
FewShotPromptTemplate,
} from "./index";

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type InputValues = Record<string, any>;

type SerializedBasePromptTemplate =
| SerializedPromptTemplate
| SerializedFewShotTemplate
| (Omit<SerializedPromptTemplate, "_type"> & { _type: undefined });

export interface BasePromptTemplateInput {
inputVariables: string[];
outputParser?: BaseOutputParser;
}

type ConstructorInput = FewShotPromptTemplateInput | PromptTemplateInput;

export abstract class BasePromptTemplate implements BasePromptTemplateInput {
inputVariables: string[];

outputParser?: BaseOutputParser;

constructor(input: ConstructorInput) {
const { inputVariables } = input;
if (inputVariables.includes("stop")) {
throw new Error(
"Cannot have an input variable named 'stop', as it is used internally, please rename."
);
}
Object.assign(this, input);
}

abstract format(values: InputValues): string;

abstract _getPromptType(): string;

abstract serialize(): SerializedBasePromptTemplate;

// Deserializing needs to be async because templates (e.g. few_shot) can
// reference remote resources that we read asynchronously with a web
// request.
static async deserialize(
data: SerializedBasePromptTemplate
): Promise<BasePromptTemplate> {
switch (data._type) {
case "prompt":
return PromptTemplate.deserialize(data);
case undefined:
return PromptTemplate.deserialize({ ...data, _type: "prompt" });
case "few_shot":
return FewShotPromptTemplate.deserialize(data);
default:
throw new Error(
`Invalid prompt type in config: ${
(data as SerializedBasePromptTemplate)._type
}`
);
}
}
}
Loading

0 comments on commit 98f0df2

Please sign in to comment.