Skip to content

Instantly share code, notes, and snippets.

@fernandojunior
Last active September 10, 2024 15:11
Show Gist options
  • Save fernandojunior/9f4bf868f7f393e30578fd6e8109893c to your computer and use it in GitHub Desktop.
Save fernandojunior/9f4bf868f7f393e30578fd6e8109893c to your computer and use it in GitHub Desktop.
Simple LangChain translator code to abstract LLM models
! pip install langchain
! pip install langchain-openai
! pip install langchain-google-genai
! pip install langchain-community llama-cpp-python
! wget https://huggingface.co/NousResearch/Hermes-2-Pro-Llama-3-8B-GGUF/resolve/main/Hermes-2-Pro-Llama-3-8B-Q8_0.gguf
from langchain_openai import ChatOpenAI
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_community.chat_models import ChatLlamaCpp
os.environ["OPENAI_API_KEY"] = "<OPENAI_API_KEY>"
os.environ["GOOGLE_API_KEY"] = "<GOOGLE_API_KEY>"
llma_local_model = "./Hermes-2-Pro-Llama-3-8B-Q8_0.gguf"
llms = {
"gpt": lambda: ChatOpenAI(model="gpt-4"),
"gemini": lambda: ChatGoogleGenerativeAI(model="gemini-1.5-flash"),
"llma": lambda: ChatLlamaCpp(model_path=llma_model_path)
}
model = llms["gemini"]() # or `llms["llma"]` or `llms["gpt"]`
messages = [
("system", "Translate the user sentence from English to French."),
("human", "I love programming.")
]
print(model.invoke(messages))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment