Created
October 30, 2023 13:21
-
-
Save Bennoo/7d45241036bd9230284b7947c1d350de to your computer and use it in GitHub Desktop.
Examples on using LLM's
This file contains hidden or 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
from langchain.document_loaders import YoutubeLoader | |
from langchain.text_splitter import RecursiveCharacterTextSplitter | |
from langchain.embeddings.openai import OpenAIEmbeddings | |
from langchain.vectorstores import FAISS | |
from langchain.chat_models import ChatOpenAI | |
from langchain.chains import LLMChain | |
from langchain.prompts.chat import ( | |
ChatPromptTemplate, | |
SystemMessagePromptTemplate, | |
HumanMessagePromptTemplate, | |
) | |
import textwrap | |
import os | |
from IPython.display import Markdown | |
# Variables | |
# Get connection to the OpenAI API Embeddings model | |
embeddings = OpenAIEmbeddings() | |
openai_api_key = os.getenv('OPENAI_API_KEY') | |
# Create a Vector FAISS database from a youtube video url | |
def create_db_from_youtube_video_url(video_url): | |
loader = YoutubeLoader.from_youtube_url(video_url, language = 'fr', add_video_info=True) | |
transcript = loader.load() | |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100) | |
docs = text_splitter.split_documents(transcript) | |
db = FAISS.from_documents(docs, embeddings) | |
return db | |
# Get response from a LLM model question answering system, using RAG to augment Prompt | |
def get_response_from_query(db, query, k=4): | |
""" | |
gpt-3.5-turbo can handle up to 4097 tokens. | |
""" | |
# Get related part of the transcript | |
docs = db.similarity_search(query, k=k) | |
docs_page_content = " ".join([d.page_content for d in docs]) | |
# Get LLM | |
chat = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0.2) | |
# Template to use for the system message prompt | |
template = """ | |
You are a helpful assistant that that can answer questions about youtube videos | |
based on the video's transcript: {docs} | |
Only use the factual information from the transcript to answer the question. | |
If you feel like you don't have enough information to answer the question, say "I don't know". | |
Your answers should be verbose and detailed. | |
Use a markdown formatting for the answer | |
""" | |
system_message_prompt = SystemMessagePromptTemplate.from_template(template) | |
# Human question prompt | |
human_template = "Answer the following question: {question}" | |
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template) | |
# Create chat history | |
chat_prompt = ChatPromptTemplate.from_messages( | |
[system_message_prompt, human_message_prompt] | |
) | |
# Create chain | |
chain = LLMChain(llm=chat, prompt=chat_prompt) | |
# Get response | |
response = chain.run(question=query, docs=docs_page_content) | |
return response, docs | |
# Example usage: | |
video_url = "https://youtu.be/M86YM6QA4-M" | |
db = create_db_from_youtube_video_url(video_url) | |
query = "Fait moi un résumé de la video" | |
response, docs = get_response_from_query(db, query) | |
display(Markdown(response)) |
This file contains hidden or 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
import os | |
from langchain.chat_models import AzureChatOpenAI | |
from langchain.chains import LLMChain | |
from langchain.prompts.chat import ( | |
ChatPromptTemplate, | |
SystemMessagePromptTemplate, | |
HumanMessagePromptTemplate, | |
) | |
# variables | |
deployment_name = 'gpt-4' | |
openai_api_version = '2023-03-15-preview' | |
openai_api_base = os.environ.get("OPENAI_API_BASE") | |
openai_api_key = os.getenv('OPENAI_API_KEY') | |
my_docs = 'Lorem ipsum' | |
# Get connection to the LLM through OPENAI API | |
chat = AzureChatOpenAI( | |
deployment_name=deployment_name, | |
openai_api_version=openai_api_version, | |
temperature=0.2, | |
openai_api_base=openai_api_base, | |
openai_api_key=openai_api_key, | |
request_timeout=70, | |
max_retries=2, | |
) | |
# Create the system prompt | |
system_message_prompt = SystemMessagePromptTemplate.from_template(""" | |
You are a great assistant, you create your answers always in English with a friendly tone.\ | |
Please use the following pieces of documentation to help yourself\ | |
Here are the documentations:\ | |
{documentations}\ | |
""") | |
# Human question prompt | |
human_template = "Answer the following question: {question}" | |
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template) | |
# Create the chat history | |
chat_prompt = ChatPromptTemplate.from_messages( | |
[system_message_prompt, human_message_prompt] | |
) | |
# Create the CHAIN using langchain | |
chain = LLMChain(llm=chat, prompt=chat_prompt) | |
# Run the chain | |
response = chain.run(question='Please Summarize', documentations=my_docs) |
This file contains hidden or 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
import os | |
from langchain.chat_models import ChatOpenAI | |
from langchain.chains import LLMChain | |
from langchain.prompts.chat import ( | |
ChatPromptTemplate, | |
SystemMessagePromptTemplate, | |
HumanMessagePromptTemplate, | |
) | |
# variables | |
model = 'gpt-4' | |
openai_api_key = os.getenv('OPENAI_API_KEY') | |
my_docs = 'Lorem ipsum' | |
# Get connection to the LLM through OPENAI API | |
chat = ChatOpenAI(model_name=model, temperature=0.2) | |
# Create the system prompt | |
system_message_prompt = SystemMessagePromptTemplate.from_template(""" | |
You are a great assistant, you create your answers always in English with a friendly tone.\ | |
Please use the following pieces of documentation to help yourself\ | |
Here are the documentations:\ | |
{documentations}\ | |
""") | |
# Human question prompt | |
human_template = "Answer the following question: {question}" | |
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template) | |
# Create the chat history | |
chat_prompt = ChatPromptTemplate.from_messages( | |
[system_message_prompt, human_message_prompt] | |
) | |
# Create the CHAIN using langchain | |
chain = LLMChain(llm=chat, prompt=chat_prompt) | |
# Run the chain | |
response = chain.run(question='Please Summarize', documentations=my_docs) |
This file contains hidden or 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
import openai | |
import os | |
openai.api_key = os.getenv('OPENAI_API_KEY') | |
# Variables | |
model = "gpt-3.5-turbo" | |
messages=[ | |
{ | |
"role": "user", | |
"content": "Bonjour" | |
} | |
] | |
# Get response (completion) | |
completion = openai.ChatCompletion.create( | |
model=model, | |
messages=messages, | |
temperature=1 | |
) | |
# Print the message | |
print(completion.choices[0].message.content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment