In this article we are lookin about how to create first AI Hello World Application using LangChain and Open AI API. We are using Open AI API in this project but you can also use Hugging face if you want.
Installing Required Packages
To create this project we need two packages to install which are
Step 1: Installing LangChain
pip install langchain
using this project we are install LangChain. LangChain enables building application that connect external sources of data and computation to LLMs.
Step 2: Installing Open AI
To install OpenAI run this commad.
pip install openai
The Open AI Library provides ease access to OPENAI REST API from any application. This library includes type directions for al the request param and response fields, and offers synchtonous and asynchronous clients.
Step 3: Set OpenAI API Key
In this step we are initializing code.
import os os.environ["OPENAI_API_KEY"]="INSERT YOUR OWN KEY" from langchain.llms import OpenAI llm = OpenAI(temperature=0.9) text="why sky is blue?" print(llm(text))
This line sets the OpenAI GPT-3 API key as an environment variable. The API key is a secret token that authorizes your code to access the OpenAI GPT-3 service.
We are set the OpenAI API Key to OPENAI_API_KEY
. Now replace you own key here: INSERT YOUR OWN KEY.
Note: To get Open AI API Key visit openai.com, and you can able to generate new open api key.
Next, we initializes instance of OpenAI Language model. The temperature paraneter adjust randomness of your model’s output.
To get accurate answer change temperature to 1.0 and to get creative answer cange tempereture to 0. The best way to get aswwer is 0.6 so, it will generate good answers.
In the text
we can write out questions. for example I wrote why sky is blue?. You can add your answer to this text variable.
Finally, The generated content is printed to console. This output represents the response generated by OpenAI API model in reponse to question ask on text
.
Conslusion
This is the simple Hello World Example using LangChain and Open AI API. Practice this project by own dep dive in to AI world.