-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: Use ChatGoogleGenerativeAI for single-turn translation
Corrected the import and method call to use ChatGoogleGenerativeAI for single-turn translation requests.
- Loading branch information
Showing
1 changed file
with
12 additions
and
14 deletions.
There are no files selected for viewing
26 changes: 12 additions & 14 deletions
26
Projects/2. Large Language Models and LangChain/chat_models.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,16 @@ | ||
from langchain_google_genai import GoogleGenerativeAI # Import GoogleGenerativeAI from langchain_google_genai | ||
from langchain.schema import ( # Import HumanMessage and SystemMessage from langchain.schema | ||
HumanMessage, | ||
SystemMessage | ||
) | ||
from dotenv import load_dotenv # Import load_dotenv | ||
# Import ChatGoogleGenerativeAI | ||
from langchain_google_genai import ChatGoogleGenerativeAI | ||
# Import load_dotenv | ||
from dotenv import load_dotenv | ||
import os | ||
|
||
load_dotenv() # Load environment variables from .env file | ||
# Load environment variables from .env file | ||
load_dotenv() | ||
|
||
llm = GoogleGenerativeAI(model="gemini-pro", google_api_key=os.getenv("GOOGLE_API_KEY")) # Create GoogleGenerativeAI instance | ||
# Create ChatGoogleGenerativeAI instance | ||
llm = ChatGoogleGenerativeAI(model="gemini-pro", google_api_key=os.getenv("GEMINI_API_KEY")) | ||
|
||
messages = [ # Create a list of messages | ||
SystemMessage(content="You are a helpful assistant that translates English to French."), # Create a SystemMessage with the content "You are a helpful assistant that translates English to French." | ||
HumanMessage(content="Translate the following sentence: I love programming.") # Create a HumanMessage with the content "Translate the following sentence: I love programming." | ||
] | ||
|
||
llm(messages) # Call the llm object with the list of messages | ||
# Use invoke for single messages | ||
response = llm.invoke("Translate the following sentence: I love programming.") | ||
# Print the generated response text only | ||
print(response.content) |