@imaginary-dev/util
Advanced tools
Comparing version 0.0.4 to 0.0.5
import { JSONSchema7 } from "json-schema"; | ||
import { CreateCompletionRequest, CreateChatCompletionRequest } from "openai"; | ||
import { ConfigurationParameters, CreateChatCompletionRequest, CreateCompletionRequest } from "openai"; | ||
import { ValueOf } from "ts-essentials"; | ||
export interface ImaginaryFunctionDefinition { | ||
@@ -15,7 +16,15 @@ funcName: string; | ||
} | ||
type OpenAIParameters = Partial<Omit<CreateCompletionRequest & CreateChatCompletionRequest, "prompt">> & { | ||
apiConfig?: ConfigurationParameters; | ||
}; | ||
export interface ServiceParameters { | ||
openai?: Partial<Omit<CreateCompletionRequest & CreateChatCompletionRequest, "prompt">>; | ||
openai?: OpenAIParameters; | ||
} | ||
declare const safeServiceParameterKeys: ("max_tokens" | "temperature")[]; | ||
type SafeKeys = ValueOf<typeof safeServiceParameterKeys>; | ||
/** Extract only the exact keys that we should be passing to OpenAI's api calls */ | ||
export declare function getSafeOpenAIServiceParameters(serviceParameters: ServiceParameters): Pick<OpenAIParameters, SafeKeys>; | ||
export declare const AI_SERVICES: (keyof ServiceParameters)[]; | ||
/** Come up with a unique hash to represent an imaginary function definition, for use in tracking changes, etc */ | ||
export declare function hashFunctionDefinition(definition: ImaginaryFunctionDefinition): string; | ||
export {}; |
@@ -6,4 +6,17 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.hashFunctionDefinition = exports.AI_SERVICES = void 0; | ||
exports.hashFunctionDefinition = exports.AI_SERVICES = exports.getSafeOpenAIServiceParameters = void 0; | ||
const object_hash_1 = __importDefault(require("object-hash")); | ||
const safeServiceParameterKeys = [ | ||
"temperature", | ||
"max_tokens", | ||
]; | ||
/** Extract only the exact keys that we should be passing to OpenAI's api calls */ | ||
function getSafeOpenAIServiceParameters(serviceParameters) { | ||
const { max_tokens, temperature } = serviceParameters.openai ?? {}; | ||
return { | ||
...(max_tokens !== undefined ? { max_tokens } : {}), | ||
...(temperature !== undefined ? { temperature } : {}), | ||
}; | ||
} | ||
exports.getSafeOpenAIServiceParameters = getSafeOpenAIServiceParameters; | ||
exports.AI_SERVICES = ["openai"]; | ||
@@ -10,0 +23,0 @@ /** Come up with a unique hash to represent an imaginary function definition, for use in tracking changes, etc */ |
{ | ||
"name": "@imaginary-dev/util", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "", | ||
@@ -18,4 +18,6 @@ "main": "dist/util/src/index.js", | ||
"devDependencies": { | ||
"@types/object-hash": "^3.0.2" | ||
"@jest/globals": "^29.5.0", | ||
"@types/object-hash": "^3.0.2", | ||
"jest": "^29.5.0" | ||
} | ||
} |
@@ -62,3 +62,3 @@ Imaginary Programming is a way to use AI to define implementation-free functions in TypeScript. When you have installed `imaginary-dev` into your project, you can define a function like this: | ||
## Imaginary functions need to live server-side | ||
### Imaginary functions need to live server-side | ||
@@ -138,3 +138,3 @@ OpenAI's API is not intended to be used directly from browsers. If you do so, you will be revealing your OpenAI API key to the world and potentially allow other people to rack up charges on your OpenAI account. For this reason, you should always use the OpenAI API on the server-side and make an API call from your browser frontend to the server. This keeps your API key safe and hidden from potential bad actors. | ||
## Installing the dependencies | ||
### Installing the dependencies | ||
@@ -148,3 +148,3 @@ First, install the necessary dependencies to your project: | ||
## Swap out `tsc` for `ttsc` | ||
### Swap out `tsc` for `ttsc` | ||
@@ -173,3 +173,3 @@ Next, find where in your project `tsc` is invoked as part of your project's build. Commonly, this in an npm script defined in `package.json`. Replace `tsc` with `ttsc` (note the extra "t" at the beginning). For example, if your project is built with the command `npm run build`, look in your `package.json` file, and you'll probably see a line like this: | ||
## Adding `imaginary-dev` to your TypeScript configuration | ||
### Adding `imaginary-dev` to your TypeScript configuration | ||
@@ -176,0 +176,0 @@ Third, add the following `plugins` line to the `compilerOptions` section of your `tsconfig.json` file: |
@@ -7,6 +7,2 @@ // Import the required libraries and functions | ||
} from "./function-definition"; // Replace with the correct module path | ||
// Import Jest functions | ||
import { expect } from "@jest/globals"; | ||
// Test the hashFunctionDefinition function | ||
@@ -13,0 +9,0 @@ describe("hashFunctionDefinition", () => { |
import { JSONSchema7 } from "json-schema"; | ||
import objectHash from "object-hash"; | ||
import { CreateCompletionRequest, CreateChatCompletionRequest } from "openai"; | ||
import { | ||
ConfigurationParameters, | ||
CreateChatCompletionRequest, | ||
CreateCompletionRequest, | ||
} from "openai"; | ||
import { ValueOf } from "ts-essentials"; | ||
export interface ImaginaryFunctionDefinition { | ||
@@ -14,8 +19,32 @@ funcName: string; | ||
type OpenAIParameters = Partial< | ||
Omit<CreateCompletionRequest & CreateChatCompletionRequest, "prompt"> | ||
> & { | ||
apiConfig?: ConfigurationParameters; | ||
}; | ||
export interface ServiceParameters { | ||
openai?: Partial< | ||
Omit<CreateCompletionRequest & CreateChatCompletionRequest, "prompt"> | ||
>; | ||
openai?: OpenAIParameters; | ||
} | ||
type SafeKeyOf<T> = T extends undefined ? never : keyof T; | ||
type OpenaiServiceParameterKey = SafeKeyOf<OpenAIParameters>; | ||
const safeServiceParameterKeys = [ | ||
"temperature", | ||
"max_tokens", | ||
] satisfies OpenaiServiceParameterKey[]; | ||
type SafeKeys = ValueOf<typeof safeServiceParameterKeys>; | ||
/** Extract only the exact keys that we should be passing to OpenAI's api calls */ | ||
export function getSafeOpenAIServiceParameters( | ||
serviceParameters: ServiceParameters | ||
): Pick<OpenAIParameters, SafeKeys> { | ||
const { max_tokens, temperature } = serviceParameters.openai ?? {}; | ||
return { | ||
...(max_tokens !== undefined ? { max_tokens } : {}), | ||
...(temperature !== undefined ? { temperature } : {}), | ||
}; | ||
} | ||
export const AI_SERVICES: (keyof ServiceParameters)[] = ["openai"]; | ||
@@ -22,0 +51,0 @@ |
Sorry, the diff of this file is not supported yet
41714
735
3