How to create a chatbot with OpenAI’s API: a conceptual cheat sheet

I spent the last couple of weeks diving into AI and I’ve been learning how to build a chatbot that uses my own content.

I’ve outlined the general steps for creating one, as a “cheat sheet” to myself, so I’m sharing it here.

So if you want to build a …


This content originally appeared on DEV Community and was authored by farez

I spent the last couple of weeks diving into AI and I've been learning how to build a chatbot that uses my own content.

I've outlined the general steps for creating one, as a "cheat sheet" to myself, so I'm sharing it here.

So if you want to build a chatbot with your own content, here are the general steps you would take.

Setup

Process your documents

  • Gather all your documents ad convert them into text. Convert line breaks and tabs to spaces (OpenAI’s recommendation).

  • Break the document content into chunks. Each chunk should be small-ish, e.g. 2,000 words (look up OpenAI's tokens for more specific settings).

  • Use OpenAI’s API to create embeddings for each chunk.

  • Save each chunk into the vector database.

Create a similarity search function

  • Use a similarity search function to retrieve the correct chunk, based on a question. OpenAI recommends the cosine similarity calculation.

  • Test it out and tweak the parameters.

Create your user-facing app

  • Create a form to ask questions and display responses.

Write your prompts

There are 3 types of prompts:

  • Information gathering prompt: A prompt for OpenAI to keep asking questions until it has all the information it needs.

  • Summarise question prompt: Another prompt to summarise the question.

  • Answer prompt: A prompt that combines the final form of the question along with the relevant chunk of information.

Get chatting

  • Host your code and database somewhere.

  • When someone asks a question, send the question and the Information gathering prompt to the OpenAI Chat API.

  • Repeat until OpenAI responds that it has enough info.

  • Send the chat transcript to OpenAI with the Summarise question prompt.

Answer the question

  • Use the OpenAI API to convert the summarised question into an embedding.

  • Search the vector database for the chunk that would most likely contain the answer, using the similarity search function you created earlier.

  • Use the Answer prompt to send the summarised question and the chunk to the OpenAI Completion API to get the answer.

  • Wait for OpenAI to respond and display the answer to the user.

That's generally it!

Would love your feedback, especially from those who have already been building chatbots. Especially if you have a different method or process for building it.

Thanks!

I'm sharing a lot more as I learn and build a new AI SaaS, so if you want more, you can find it on my newsletter!


This content originally appeared on DEV Community and was authored by farez


Print Share Comment Cite Upload Translate Updates
APA

farez | Sciencx (2023-05-19T16:27:07+00:00) How to create a chatbot with OpenAI’s API: a conceptual cheat sheet. Retrieved from https://www.scien.cx/2023/05/19/how-to-create-a-chatbot-with-openais-api-a-conceptual-cheat-sheet/

MLA
" » How to create a chatbot with OpenAI’s API: a conceptual cheat sheet." farez | Sciencx - Friday May 19, 2023, https://www.scien.cx/2023/05/19/how-to-create-a-chatbot-with-openais-api-a-conceptual-cheat-sheet/
HARVARD
farez | Sciencx Friday May 19, 2023 » How to create a chatbot with OpenAI’s API: a conceptual cheat sheet., viewed ,<https://www.scien.cx/2023/05/19/how-to-create-a-chatbot-with-openais-api-a-conceptual-cheat-sheet/>
VANCOUVER
farez | Sciencx - » How to create a chatbot with OpenAI’s API: a conceptual cheat sheet. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/05/19/how-to-create-a-chatbot-with-openais-api-a-conceptual-cheat-sheet/
CHICAGO
" » How to create a chatbot with OpenAI’s API: a conceptual cheat sheet." farez | Sciencx - Accessed . https://www.scien.cx/2023/05/19/how-to-create-a-chatbot-with-openais-api-a-conceptual-cheat-sheet/
IEEE
" » How to create a chatbot with OpenAI’s API: a conceptual cheat sheet." farez | Sciencx [Online]. Available: https://www.scien.cx/2023/05/19/how-to-create-a-chatbot-with-openais-api-a-conceptual-cheat-sheet/. [Accessed: ]
rf:citation
» How to create a chatbot with OpenAI’s API: a conceptual cheat sheet | farez | Sciencx | https://www.scien.cx/2023/05/19/how-to-create-a-chatbot-with-openais-api-a-conceptual-cheat-sheet/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.