Embeddings
The embeddings module is used to obtain a vector representation of a given input that machine learning models and algorithms can quickly consume. We will write a function to generate these vectors using OpenAI API. Our API employs the “text-embedding-ada-002” model, a member of OpenAI’s second generation of embedding models. This model produces embeddings that are 1536 bytes long.
Python3
# function to generate vector for a string def get_embedding(text, model = "text-embedding-ada-002" ): return openai.Embedding.create( input = , model = model)[ 'data' ][ 0 ][ 'embedding' ] |
Let’s try this out on some sample text.
Prompt 1:
Python3
PROMPT = "This is Geeks for Geeks office. How can I help you?" get_embedding(PROMPT) |
Output:
[0.002064391039311886, -0.006849951110780239, -0.0022784520406275988, -0.034846529364585876, -0.010696562007069588, 0.01778651960194111, -0.0037395802792161703, -0.009561389684677124, -0.0055493684485554695, -0.006862924434244633, -0.02698465622961521, -0.015438336879014969, 0.008653252385556698, -0.010417633689939976, ...]
As we can see, we have obtained a vector generated by OpenAI Embeddings module. You can use this vector with any machine learning or deep learning algorithm.
OpenAI Python API – Complete Guide
OpenAI is the leading company in the field of AI. With the public release of software like ChatGPT, DALL-E, GPT-3, and Whisper, the company has taken the entire AI industry by storm. Everyone has incorporated ChatGPT to do their work more efficiently and those who failed to do so have lost their jobs. The age of AI has started and people not adapting to AI could introduce some difficulties for them.
In this article, we will be discussing how you can leverage the power of AI and make your day-to-day tasks a lot easier by using the OpenAI APIs (Application Programming Interface) that allow developers to easily access their AI models and Integrate them into their own applications using Python.
Table of Content
- What is OpenAI?
- What is OpenAI API?
- Generate OpenAI API key
- Installation of OpenAI package
- Prompt Engineering
- Text
- Chat
- Image
- Audio
- Embeddings
- Fine-Tuning
- API Error Codes
- Conclusion
- FAQs on OpenAI Python API
Contact Us