# OpenAI

OpenAI's GPT engine is a powerful language model that you can integrate into your chatbot to enhance the chatbot's conversational abilities and experience for your end users.  This guide walks you through integrating OpenAI into your BotDistrikt chatbot.

To integrate OpenAI into your BotDistrikt chatbot:&#x20;

1. Ensure you have **Created an active Access Token** on **Personality -> Settings**
2. Go to **Integrations --> Artificial Intelligence --> OpenAI**
3. Enter a valid API key\* and click **Save**

Navigate to **Trainings** to train the OpenAI model

<figure><img src="https://2535542804-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LirAdLo22OkAW9w3tvY%2Fuploads%2FBKs57Pv9u9vP17vUsnwT%2FGroup%20221.png?alt=media&#x26;token=a22fd76a-c815-47b8-b6fe-6549a5db08c2" alt="" width="188"><figcaption><p>OpenAI --> Trainings (After Integrating OpenAI Through Valid API)</p></figcaption></figure>

1. Select the available chat model from the dropdown.
2. Choose the appropriate temperature according to your chatbot's personality. Increased temperature indicates more random responses to the prompts. Decreased temperature ensures more stable and predictable responses.&#x20;
3. Maximum tokens to generate indicates your chatbot's response length. Requests use up to 2048 to 4000 tokens (shared between prompt and completion). Your limit varies by the model you choose in 1. A token (approximately) equals 4 characters in plain English).
4. **Top P** indicates the normalcy of words within a response. A higher number indicates using more normal words and less includes the inclusion of more unique words in your chatbot's responses. The diversity is controlled through a nucleus sampling of 0.5 (consideration of half of all likelihood-weighted options).
5. Adjust the **Frequency Penalty** to control how often your chatbot repeats the same words. High-frequency penalty for less repetition and low-frequency penalty for more repetition.
6. Adjust the **Presence Penalty** to control how much your chatbot focuses on specific topics. Set a high presence penalty for more variety in responses and a low presence penalty for more focus on a topic.
7. Set the **Max Conversation Tokens** to indicate the maximum number of tokens to keep in the chatbot's conversation history. As the conversation history grows, the oldest messages are removed (if tokens exceed the specified number).
8. Toggle **Generate Embeddings for Text Responses** to retrieve the results of a similarity search of text responses (from the websites and documents in **Sources**) and the users' input message.
9. Toggle **Generate Embeddings for Cards** to retrieve the results of a similarity search of cards (from the cards tab in **Responses** and the users' input message.
10. Select the **Embedding Model** between the website and document sources. The model represents words or phrases as numerical vectors in a continuous vector space, allowing computers to process and understand textual data more effectively.
11. Adjust the **Embedding Dimensions** to control the length of the embeddings to store and query for similarity searches.
12. To optimize responses from the document or web resources, enter a number in **Embedding Top K.** This feature presents the top K most semantically related entries or documents, enabling the RAG model to use these documents as reference to generate more informed, accurate, and relevant responses to the given input.
13. Toggle **Score RAGAS Metrics** to observe RAGAS scores for generated RAG completions.
14. Enter an **Intro Prompt** to provide context, instructions, or other information relevant to the model and use case. The prompt can determine the character, behaviour, disposition and function of the chatbot. To write effective prompts, describe the task you want it to complete and provide background information. Give the model some examples of the desired output. Also note that the order of presenting information matters.
15. **Import a Story**
16. The number of tokens used is displayed in the bottom right corner of the **Intro Prompt** box. Tokens are units of text that these models process and generate which the model can turn into embeddings. Example: "I am a chatbot" utilizes 5 tokens.

Click **Save.**

To test your bot, navigate to **Testing** and test your bot accordingly.&#x20;

***

\*To generate a valid OpenAI API key:

1. Create an OpenAI account
2. Log in to your OpenAI account via API login
3. Click on API
4. In the left sidebar, select API Keys
5. Click on **+ Create new secret key** button

<figure><img src="https://2535542804-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LirAdLo22OkAW9w3tvY%2Fuploads%2FQuuAs7wUEg5CV4biLonO%2FScreenshot%202024-07-10%20at%204.06.05%E2%80%AFPM.png?alt=media&#x26;token=e926851d-71bf-4454-8e77-ca6ef860f5db" alt="" width="375"><figcaption><p>Create new secret key</p></figcaption></figure>

6. Name your API key (for reference)
7. Choose an appropriate project
8. Set the level of permission required
9. Click **Create secret key**
10. In the popup, copy the secret key generated
11. Enter in BotDistrikt **Integrations --> Artificial Intelligence --> OpenAI -->  API key**
