From 58c0a91918bd87f62fa1e11fcf19406d98cb0191 Mon Sep 17 00:00:00 2001 From: DJ Papzin Date: Wed, 14 Aug 2024 18:04:55 +0200 Subject: [PATCH] feat: Migrate from OpenAI to Google Gemini Pro Replaces OpenAI's language models with Google's Gemini Pro for enhanced performance and capabilities. The code now utilizes `ChatGoogleGenerativeAI` and leverages environment variables for API key management. --- .../few_short.py | 12 +++++++++-- .../chain_prompting.py | 20 +++++++++++++++---- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/Projects/2. Large Language Models and LangChain/few_short.py b/Projects/2. Large Language Models and LangChain/few_short.py index a59110f..a4e70fe 100644 --- a/Projects/2. Large Language Models and LangChain/few_short.py +++ b/Projects/2. Large Language Models and LangChain/few_short.py @@ -1,7 +1,15 @@ from langchain import PromptTemplate from langchain import FewShotPromptTemplate -from langchain.chat_models import ChatOpenAI +from langchain_google_genai import ChatGoogleGenerativeAI # Import ChatGoogleGenerativeAI from langchain import LLMChain +from dotenv import load_dotenv +import os + +# Load environment variables from .env file +load_dotenv() + +# Create ChatGoogleGenerativeAI instance +llm = ChatGoogleGenerativeAI(model="gemini-pro", google_api_key=os.getenv("GEMINI_API_KEY")) # create our examples dictionery examples = [ @@ -49,7 +57,7 @@ ) # load the model -chat = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0.9) +chat = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.9, google_api_key=os.getenv("GEMINI_API_KEY")) # Use 'gemini-pro' instead of 'gemini' chain = LLMChain(llm=chat, prompt=few_shot_prompt_template, verbose=True) diff --git a/Projects/3. Learning How to Prompt/chain_prompting.py b/Projects/3. Learning How to Prompt/chain_prompting.py index 209f604..83627b4 100644 --- a/Projects/3. Learning How to Prompt/chain_prompting.py +++ b/Projects/3. Learning How to Prompt/chain_prompting.py @@ -1,8 +1,20 @@ from langchain import PromptTemplate, LLMChain -from langchain.llms import OpenAI +from langchain_google_genai import ChatGoogleGenerativeAI +import os +from dotenv import load_dotenv + + +# Load environment variables from .env file +load_dotenv() # Initialize LLM -llm = OpenAI(model_name="text-davinci-003", temperature=0) +api_key = os.getenv("GEMINI_API_KEY") +llm = ChatGoogleGenerativeAI(model="gemini-pro", google_api_key=api_key) +print( + llm.invoke( + "What are some of the pros and cons of Python as a programming language?" + ) +) # Prompt 1 template_question = """What is the name of the famous scientist who developed the theory of general relativity? @@ -36,5 +48,5 @@ # Run the LLMChain for the second prompt response_fact = chain_fact.run(input_data) -print("Scientist:", scientist) -print("Fact:", response_fact) +print("Scientist:", scientist.text) +print("Fact:", response_fact.text) \ No newline at end of file