UNPKG

godly-ai

Version:

Personlize your OpenAI completions with custom context.

82 lines (59 loc) 2.74 kB
## GodlyAI Extend your OpenAI GPT3 completions with custom context to personalize youre responses. [Godly.ai](https://godly.ai) ### Installation ```bash $ npm install godly-ai ``` ### Usage You need an OpenAI api key which you can generate on the [OpenAI Website](https://beta.openai.com/account/api-keys). You also need a Godly API key which you can generate on the [Godly Dashboard](https://godly.ai/dashboard/settings) Create a `Project` to get started via the [Godly Website](https://godly.ai) #### Adding Context To a Project `ContextItems` are snippets of text with relevant information that may be useful to prompts related to a query. You can add `contextItems` via the website, or using the SDK. To use the SDK just call the `createContextItem` function and pass through the `PROJECT_ID` you want to add context to. You can find the `PROJECT_ID` via the Godly Dashboard. ```javascript const configuration = new Configuration({ accessToken: process.env.GODLY_API_KEY, }); const godlyApi = new GodlyApi(configuration); // Add some information as conext to the project await godlyApi.createContextItem(PROJECT_ID, { value: 'Some information', }); ``` Congratulations, you've just uploaded your first piece of context. You're ready to start getting personalized completions. #### Using Context with OpenAI Completions When you send a prompt to OpenAI via Godly it will append the most relevant pieces of Context to your query allowing GPT-3 to better understand the intent of your query and give you more accurate completions. ```javascript const { Configuration, GodlyApi } = require('godly-ai'); const configuration = new Configuration({ accessToken: process.env.GODLY_API_KEY, }); const godlyApi = new GodlyApi(configuration); const completion = await godlyApi.completionWithContext(PROJECT_ID, { model: 'text-davinci-003', prompt: 'Hello world', }); console.log(completion.data.choices[0].text); ``` You can manage projects and context items via the Godly API or sdk. Check out the [full API documentation](https://godly.readme.io/) for examples of all the available actions. ### Error handling API requests can potentially return errors due to invalid inputs or other issues. These errors can be handled with a `try...catch` statement, and the error details can be found in either `error.response` or `error.message`: ```javascript try { const completion = await godlyApi.createCompletionWithContext(CONTEXT_ID, { model: 'text-davinci-003', prompt: 'Hello world', }); console.log(completion.data.choices[0].text); } catch (error) { if (error.response) { console.log(error.response.status); console.log(error.response.data); } else { console.log(error.message); } } ```