├── .gitattributes ├── 2023_08_19_YT_Summaries ├── requirements.txt └── yt_summary.py ├── 2023_08_26_Prompt_Improver ├── prompt_generator.py ├── prompt_generator_streamlit.py ├── prompts.py └── requirements.txt ├── 2023_09_02_Assistant_AI ├── .env_EXAMPLE ├── prompts.py ├── requirements.txt └── speech_to_text.py ├── 2023_09_16_PDF_QA ├── .env_EXAMPLE ├── pdf_qa.py └── requirements.txt └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /2023_08_19_YT_Summaries/requirements.txt: -------------------------------------------------------------------------------- 1 | langchain 2 | youtube-transcript-api 3 | openai 4 | tiktoken -------------------------------------------------------------------------------- /2023_08_19_YT_Summaries/yt_summary.py: -------------------------------------------------------------------------------- 1 | from langchain.document_loaders import YoutubeLoader 2 | from langchain.chains.summarize import load_summarize_chain 3 | from langchain.chat_models import ChatOpenAI 4 | from langchain.text_splitter import TokenTextSplitter 5 | 6 | # Load Transcript 7 | loader = YoutubeLoader.from_youtube_url("https://www.youtube.com/watch?v=5sLYAQS9sWQ", language=["en", "en-US"]) 8 | transcript = loader.load() 9 | 10 | # Split Transcript 11 | splitter = TokenTextSplitter(model_name="gpt-3.5-turbo-16k", chunk_size=10000, chunk_overlap=100) 12 | chunks = splitter.split_documents(transcript) 13 | 14 | # Set up LLM 15 | openai_api_key = "YOUR_OPENAI_API_KEY" 16 | llm = ChatOpenAI(openai_api_key=openai_api_key, model="gpt-3.5-turbo-16k", temperature=0.3) 17 | 18 | # Summarize 19 | summarize_chain = load_summarize_chain(llm=llm, chain_type="refine", verbose=True) 20 | summary = summarize_chain.run(chunks) 21 | 22 | # Write summary to file 23 | with open("summary.txt", "w") as f: 24 | f.write(summary) -------------------------------------------------------------------------------- /2023_08_26_Prompt_Improver/prompt_generator.py: -------------------------------------------------------------------------------- 1 | from langchain import LLMChain 2 | from langchain.chat_models import ChatOpenAI 3 | from prompts import PROMPT_IMPROVER_PROMPT 4 | 5 | initial_prompt = "Generate a workout schedule" 6 | 7 | # Initialize LLM 8 | openai_api_key = "YOUR_OPENAI_API_KEY" 9 | llm = ChatOpenAI(openai_api_key=openai_api_key, model="gpt-3.5-turbo-16k", temperature=0.4) 10 | 11 | # Initialize LLMChain 12 | prompt_improver_chain = LLMChain(llm=llm, prompt=PROMPT_IMPROVER_PROMPT) 13 | 14 | # Run LLMChain 15 | improved_prompt = prompt_improver_chain.run(initial_prompt) 16 | print(improved_prompt) -------------------------------------------------------------------------------- /2023_08_26_Prompt_Improver/prompt_generator_streamlit.py: -------------------------------------------------------------------------------- 1 | from langchain import LLMChain 2 | from langchain.chat_models import ChatOpenAI 3 | import streamlit as st 4 | from prompts import PROMPT_IMPROVER_PROMPT 5 | 6 | # This Python Script is a Streamlit App that allows you to generate a prompt using the Prompt Improver Template 7 | # You can run this file by running the following command in your terminal: 8 | # streamlit run prompt_generator_streamlit.py 9 | 10 | # Set up Streamlit Interface 11 | 12 | with st.container(): 13 | st.markdown(""" 14 | ## Enter initial prompt here: 15 | """) 16 | initial_prompt = st.text_area(label="Prompt Input", label_visibility='collapsed', placeholder="Generate a workout schedule", key="prompt_input") 17 | 18 | if initial_prompt: 19 | # Initialize LLM 20 | openai_api_key = "YOUR_OPENAI_API_KEY" 21 | llm = ChatOpenAI(openai_api_key=openai_api_key, model="gpt-3.5-turbo-16k", temperature=0.4) 22 | 23 | # Initialize LLMChain 24 | prompt_improver_chain = LLMChain(llm=llm, prompt=PROMPT_IMPROVER_PROMPT) 25 | 26 | # Run LLMChain 27 | with st.spinner("Generating..."): 28 | improved_prompt = prompt_improver_chain.run(initial_prompt) 29 | st.markdown(""" 30 | ## Improved Prompt: 31 | """) 32 | st.code(improved_prompt) -------------------------------------------------------------------------------- /2023_08_26_Prompt_Improver/prompts.py: -------------------------------------------------------------------------------- 1 | from langchain.prompts import PromptTemplate 2 | 3 | prompt_improver_template = """ 4 | You are an expert Prompt Writer for Large Language Models. 5 | 6 | Your goal is to improve the prompt given below: 7 | -------------------- 8 | 9 | {text} 10 | 11 | -------------------- 12 | 13 | Here are several tips on writing great prompts: 14 | 15 | ------- 16 | 17 | Start the prompt by stating that it is an expert in the subject. 18 | 19 | Put instructions at the beginning of the prompt and use ### or to separate the instruction and context 20 | 21 | Be specific, descriptive and as detailed as possible about the desired context, outcome, length, format, style, etc 22 | --------- 23 | Here's an example of a great prompt: 24 | 25 | As a certified nutritionist, create a 7-day meal plan for a vegan athlete. 26 | 27 | The meal plan should provide all necessary nutrients, including protein, carbohydrates, fats, vitamins, and minerals. 28 | 29 | Each day should include breakfast, lunch, dinner, and two snacks. 30 | 31 | Please include a brief description of each meal and its nutritional benefits. 32 | 33 | The output should be in a daily format, with each meal detailed. 34 | 35 | Example: 36 | 37 | Day 1: 38 | Breakfast: Tofu scramble with vegetables (Provides protein and fiber) 39 | Snack 1: A handful of mixed nuts (Provides healthy fats and protein) 40 | ... 41 | 42 | 43 | Now, improve the prompt below: 44 | 45 | IMPROVE PROMPT: 46 | 47 | """ 48 | 49 | PROMPT_IMPROVER_PROMPT = PromptTemplate(template=prompt_improver_template, input_variables=["text"]) 50 | -------------------------------------------------------------------------------- /2023_08_26_Prompt_Improver/requirements.txt: -------------------------------------------------------------------------------- 1 | langchain 2 | openai 3 | streamlit 4 | tiktoken 5 | -------------------------------------------------------------------------------- /2023_09_02_Assistant_AI/.env_EXAMPLE: -------------------------------------------------------------------------------- 1 | OPENAI_API_KEY = 'YOUR_OPENAI_API_KEY' -------------------------------------------------------------------------------- /2023_09_02_Assistant_AI/prompts.py: -------------------------------------------------------------------------------- 1 | from langchain.prompts import PromptTemplate 2 | 3 | # Create a prompt template to be used in the chain. 4 | 5 | template = """ 6 | 7 | You are a management assistant who writes meeting minutes. You always manage to capture the important points. 8 | 9 | Below you will find a transcript of a recorded meeting. 10 | 11 | This report needs to be clearly and concisely written in English. 12 | 13 | Please conclude with action points at the bottom. 14 | 15 | Also, provide suggestions for topics to discuss in the next meeting. 16 | 17 | Transcript: 18 | ---------------- 19 | {transcript} 20 | ---------------- 21 | 22 | Response in markdown: 23 | 24 | """ 25 | 26 | prompt = PromptTemplate( 27 | input_variables=["transcript"], 28 | template=template, 29 | ) -------------------------------------------------------------------------------- /2023_09_02_Assistant_AI/requirements.txt: -------------------------------------------------------------------------------- 1 | langchain 2 | openai 3 | tabulate 4 | tiktoken 5 | python-dotenv -------------------------------------------------------------------------------- /2023_09_02_Assistant_AI/speech_to_text.py: -------------------------------------------------------------------------------- 1 | import openai 2 | from langchain.chat_models import ChatOpenAI 3 | from dotenv import load_dotenv 4 | import os 5 | from langchain import LLMChain 6 | from prompts import prompt 7 | 8 | # Load env files 9 | load_dotenv() 10 | openai_api_key = os.environ.get('OPENAI_API_KEY') 11 | 12 | # Transcribe audio with Whisper API 13 | audio_file_path = "path/to/your/audio/file" 14 | transcript_raw = openai.Audio.transcribe("whisper-1", file=audio_file_path) 15 | 16 | # Create LLM 17 | llm = ChatOpenAI(openai_api_key=openai_api_key, model_name="gpt-3.5-turbo", temperature=0.3) 18 | 19 | # Create prompt 20 | prompt_with_transcript = prompt.format(transcript=str(transcript_raw)) 21 | 22 | # Create chain 23 | chain = LLMChain(llm=llm, prompt=prompt_with_transcript) 24 | 25 | # Run chain 26 | summary = chain.run() 27 | -------------------------------------------------------------------------------- /2023_09_16_PDF_QA/.env_EXAMPLE: -------------------------------------------------------------------------------- 1 | OPENAI_API_KEY = 'YOUR_OPENAI_API_KEY' -------------------------------------------------------------------------------- /2023_09_16_PDF_QA/pdf_qa.py: -------------------------------------------------------------------------------- 1 | from langchain.document_loaders import PyPDFLoader 2 | from langchain.text_splitter import TokenTextSplitter 3 | from langchain.chat_models import ChatOpenAI 4 | from langchain.embeddings.openai import OpenAIEmbeddings 5 | from langchain.vectorstores import Chroma 6 | from langchain.chains import RetrievalQA 7 | from dotenv import load_dotenv 8 | 9 | # Load environment variables from .env file 10 | load_dotenv() 11 | 12 | # Load the PDF document using PyPDFLoader 13 | loader = PyPDFLoader("/Users/jorisdejong/Documents/GitHub/ai_unchained/2023_09_16/files/TSLA-Q1-2023-Update.pdf") 14 | 15 | # Extract the text data from the PDF document 16 | data = loader.load() 17 | 18 | # Split the text into chunks using TokenTextSplitter 19 | text_splitter = TokenTextSplitter(chunk_size=1000, chunk_overlap=100) 20 | chunks = text_splitter.split_documents(data) 21 | 22 | # Generate embeddings using OpenAIEmbeddings 23 | embeddings = OpenAIEmbeddings() 24 | 25 | # Create a retriever using Chroma and the generated embeddings 26 | retriever = Chroma.from_documents(chunks, embeddings).as_retriever() 27 | 28 | # Initialize the ChatOpenAI model 29 | llm = ChatOpenAI(model="gpt-3.5-turbo-16k", temperature=0.1) 30 | 31 | # Create a RetrievalQA instance with the ChatOpenAI model and the retriever 32 | qa = RetrievalQA.from_chain_type(llm, chain_type="stuff", retriever=retriever) 33 | 34 | # While loop to keep asking questions 35 | while True: 36 | question = input("What is your question? ") 37 | answer = qa.run(question) 38 | print(answer) 39 | print("------\n") 40 | -------------------------------------------------------------------------------- /2023_09_16_PDF_QA/requirements.txt: -------------------------------------------------------------------------------- 1 | langchain 2 | openai 3 | tiktoken 4 | chromadb 5 | pypdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AI Unchained Newsletter GitHub Repo 2 | 3 | Welcome to the AI Unchained Newsletter GitHub repository! Here, you'll find a collection of code snippets, resources, and examples discussed in our newsletter editions. Our aim is to empower you with practical insights and tools to enhance your journey in the world of AI and machine learning. 4 | 5 | ## Getting Started 6 | 7 | To make the most of this repository, follow these steps: 8 | 9 | 1. **Clone the Repository**: Clone this repository to your local machine using the following command: 10 | ``` 11 | git clone https://github.com/JorisdeJong123/AI_Unchained_Code_Snippets.git 12 | ``` 13 | 14 | 2. **Navigate to Sections**: Browse through the repository's sections that interest you the most, such as prompt engineering, prompt generator, and advanced features. 15 | 16 | 3. **Explore Code Snippets**: Inside each section, you'll find code snippets and examples discussed in the AI Unchained newsletter. Review the code, comments, and explanations to understand how to implement different concepts. 17 | 18 | 4. **Experiment and Learn**: Feel free to experiment with the provided code snippets in your own projects. Modify and adapt them to your specific needs, and observe how different prompts and strategies impact AI model outputs. 19 | 20 | ## Contributing 21 | 22 | We encourage contributions from the AI community to enrich this repository. If you have valuable code snippets, examples, or resources related to prompt engineering, AI research insights, or any AI-related topics, feel free to open a pull request. Your contributions can benefit fellow enthusiasts and learners in the field. 23 | 24 | ## Contact 25 | 26 | For feedback, questions, or to share your projects inspired by the AI Unchained newsletter, you can reach out to us on Twitter [@JorisTechTalk](https://twitter.com/JorisTechTalk). We're excited to see how you're advancing in the AI world! 27 | 28 | Remember, the journey in AI is unchained, and this repository is here to support your exploration and growth. Happy coding and learning! 29 | 30 | — The AI Unchained Team --------------------------------------------------------------------------------