https://github.com/timmikeladze/ai-complete
A toolkit that super-charges your workflow when working with openai.
https://github.com/timmikeladze/ai-complete
ai ai-complete aitranslate javascript nlp openai translation typescript
Last synced: 6 months ago
JSON representation
A toolkit that super-charges your workflow when working with openai.
- Host: GitHub
- URL: https://github.com/timmikeladze/ai-complete
- Owner: TimMikeladze
- License: mit
- Created: 2022-12-13T23:55:46.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2023-04-07T20:48:11.000Z (over 3 years ago)
- Last Synced: 2025-10-29T00:04:53.096Z (9 months ago)
- Topics: ai, ai-complete, aitranslate, javascript, nlp, openai, translation, typescript
- Language: TypeScript
- Homepage:
- Size: 449 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
Awesome Lists containing this project
README
# ai-complete
A toolkit that super-charges your workflow when working with `openai`.
## Install
```bash
npm install ai-complete
# or
yarn add ai-complete
# or
pnpm add ai-complete
```
**Note:** You will need to get an API key from [OpenAI](https://beta.openai.com/login/).
## Usage
In the following example we use `ai-complete` to translate a directory containing a bunch JSON language files. Our goal is to translate the files into another language and write the translated files to a new directory.
> Note: You can run this example by cloning this repo and running `yarn example`.
```js
import 'dotenv/config'
import AIComplete from 'ai-complete'
import { existsSync, mkdirSync, writeFileSync } from 'fs'
const inputDir = 'locales/en'
const outputDir = 'locales/ru'
const aic = new AIComplete({
// Configure the OpenAI API. API key is required.
openAI: {
config: {
apiKey: process.env.OPENAI_API_KEY
}
}
})
await aic.createCompletion({
// Where to read files from. All globby pattern and options are supported.
globby: {
patterns: ['example/locales/en/**/*.json']
},
// This function is called for each file and the results are used as arguments for the OpenAI API.
input: async () => {
return {
// Describe what you want to do with the file.
prompt:
'Translate the JSON below into Russian but keep names of all keys and metadata in English.',
request: {
// controls randomness, as value approaches 0 the output will be more deterministic
temperature: 0
}
}
},
// This function is called with the results from each OpenAI API call.
output: async ({ data, filePath }) => {
// Parse the results from JSON to JS.
const choice = JSON.parse(data.choices[0].text)
// Calculate the new file path of the translated file.
const outputFilePath = filePath.replace(inputDir, outputDir)
const dir = outputFilePath.substring(0, outputFilePath.lastIndexOf('/'))
if (!existsSync(dir)) {
mkdirSync(dir, { recursive: true })
}
// Write the results to the file.
const fileContents = JSON.stringify(choice, null, 2)
try {
writeFileSync(outputFilePath, fileContents, { flag: 'wx' })
console.log('Wrote file: ' + outputFilePath)
} catch (error) {
console.error('Error writing file: ' + outputFilePath)
console.error(error.message)
}
return {
choice
}
}
})
```
In this example we will use `ai-complete` to load some text and ask OpenAI to create an edit.
```js
import 'dotenv/config'
import AIComplete from 'ai-complete'
const aic = new AIComplete({
openAI: {
config: {
apiKey: process.env.OPENAI_API_KEY
}
}
})
const result = await aic.createEdit({
data: [
{
type: 'text',
value: 'One, two, ___, four.'
}
],
input: async () => ({
instruction: 'Fill in the blank with the correct word.'
}),
output: async ({ data }) => {
return {
choice: data.choices[0].text
}
}
})
// Result:
// [
// {
// "choice": "One, two, three, four.",
// }
// ]
```
## Running examples and tests
1. Create a `.env` file in the root of the project and add your `OPENAI_API_KEY` to it.
2. Run `yarn example` to run the `translate.js` example.
3. Run `yarn test` to run all the tests.