Notes
- Polygon.io is a Stock Data API with a generous free tier.
- gpt-4 is ideal for text generation. gpt-3.5-turbo is an acceptable cheaper alternative.
- When communicating with an AI for a user you will send a messages array.
- This will include a systems object. This contains instructions for the AI on how to behave and what to expect from the user.
- The system object message should be structured as what you want, what you're expecting, and how to behave.
- This will also include a user object. This contains the users input.
- Once the user object is added the messages array is sent to the AI.
- The AI will respond with an assistant object as the output.
- This object will contain the AI's response to the users input fallowing the layed out guidlines.
- To install the package for OpenAI use
npm install openai - The OpenAI Dependancy
- The OpenAI API will prevent you from exposing your API key
import OpenAI from 'openai' const openai = new OpenAI({ apiKey: '123456', }); console.log(openai.apiKey) . In this example you will reseive a console error stating "Error: It looks like you're running in a browser-like environment. This is disabled by default, as it risks exposing your secret API credentials to attackers. If you understand the risks and have appropriate mitigations in place, you can set the `dangerouslyAllowBrowser` option to `true`, e.g., new OpenAI({ apiKey, dangerouslyAllowBrowser: true }); https://help.openai.com/en/articles/5112595-best-practices-for-api-key-safety". - To override this safeguard you can add
dangerouslyAllowBrowser and set it totrue . - Adding
dangerouselyAllowBrowser looks likeimport OpenAI from 'openai' const openai = new OpenAI({ apiKey: '123456', dangerouslyAllowBrowser: true }) console.log(openai.apiKey)
- The OpenAI API will prevent you from exposing your API key
- Instructions on text generation prompting with OpenAI
- Chat Completions API: Chat models take a list of messages as inputs and return a model-generated message as an output. Although the chat format is designed to make multi-turn conversations easy, it's just as useful for single-turn tasks without any conversation.
- You will need to send an array of message objects:
const messages = [ { role: 'system', content: 'Tell the AI its purpose' }, { role: 'user', content: 'Users input' } ] const response = await openai.chat.completions.create({ model: 'gpt-4', messages: messages }) - You will get an object in return:
{ role: "assistant", content: "Whatever the response is" } -
-
-
-
-
Resources
Definitions
- Large Language Model
- An Algorithm that uses training data to recognize patterns and make predictions or decisions.