Skip to content

Instantly share code, notes, and snippets.

@nikogamulin
Created October 29, 2023 19:09
Show Gist options
  • Save nikogamulin/09a933eb3c4d68de33625e5e5d521f12 to your computer and use it in GitHub Desktop.
Save nikogamulin/09a933eb3c4d68de33625e5e5d521f12 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import textwrap\n",
"from pathlib import Path\n",
"import shutil\n",
"\n",
"from dotenv import find_dotenv, load_dotenv, dotenv_values\n",
"\n",
"import openai\n",
"from langchain.chains import LLMChain\n",
"from langchain.vectorstores import FAISS\n",
"from langchain.chat_models import ChatOpenAI\n",
"from langchain.document_loaders import YoutubeLoader\n",
"from langchain.embeddings.openai import OpenAIEmbeddings\n",
"from langchain.text_splitter import RecursiveCharacterTextSplitter\n",
"from langchain.prompts.chat import (ChatPromptTemplate,SystemMessagePromptTemplate, HumanMessagePromptTemplate)\n",
"from pytube import Playlist, YouTube\n",
"\n",
"import cohere\n",
"\n",
"import autogen"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"config = dotenv_values(\"../.env\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"embeddings = OpenAIEmbeddings(openai_api_key=config['OPENAI_API_KEY'])\n",
"co = cohere.Client(config['COHERE_API_KEY'])"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"db = FAISS.load_local(\"../data/Huberman_Lab_Episodes_with_Guests\", embeddings)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def get_response_from_query(query, k=4, rerank=False):\n",
" \"\"\"\n",
" gpt-3.5-turbo can handle up to 4097 tokens. Setting the chunksize to 1000 and k to 4 maximizes\n",
" the number of tokens to analyze.\n",
" \"\"\"\n",
"\n",
" if rerank:\n",
" k_ = k * 5\n",
" else:\n",
" k_ = k\n",
" docs = db.similarity_search(query, k=k_)\n",
" if rerank:\n",
" list_doc_contents = list(map(lambda x: x.page_content, list(docs)))\n",
" reranked_docs = co.rerank(\n",
" model = 'rerank-english-v2.0',\n",
" query = query,\n",
" documents = list_doc_contents,\n",
" top_n = k,\n",
" )\n",
" indices = list(map(lambda x: x.index, reranked_docs.results))\n",
" reranked_subset = [docs[i] for i in indices]\n",
" docs = reranked_subset\n",
" \n",
" docs_page_content = \" \".join([d.page_content for d in docs])\n",
"\n",
" chat = ChatOpenAI(model_name=\"gpt-3.5-turbo\", temperature=0.2)\n",
"\n",
" # Template to use for the system message prompt\n",
" template = \"\"\"\n",
" You are a helpful assistant that that can answer questions about youtube videos \n",
" based on the video's transcript: {docs}\n",
" \n",
" Only use the factual information from the transcript to answer the question.\n",
" \n",
" If you feel like you don't have enough information to answer the question, say \"I don't know\".\n",
" \n",
" Your answers should be verbose and detailed.\n",
" \"\"\"\n",
"\n",
" system_message_prompt = SystemMessagePromptTemplate.from_template(template)\n",
"\n",
" # Human question prompt\n",
" human_template = \"Answer the following question: {question}\"\n",
" human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)\n",
"\n",
" chat_prompt = ChatPromptTemplate.from_messages(\n",
" [system_message_prompt, human_message_prompt]\n",
" )\n",
"\n",
" chain = LLMChain(llm=chat, prompt=chat_prompt)\n",
"\n",
" response = chain.run(question=query, docs=docs_page_content)\n",
" response = response.replace(\"\\n\", \"\")\n",
" return response"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'The sympathetic and parasympathetic nervous systems are two branches of the autonomic nervous system, which controls involuntary bodily functions. While they both play important roles in regulating our body\\'s responses, they have distinct functions and effects.The sympathetic nervous system is often referred to as the \"fight or flight\" response system. It is responsible for preparing the body for action in response to perceived threats or stressors. When activated, the sympathetic nervous system increases heart rate, dilates blood vessels, and redirects blood flow to the muscles, preparing the body for physical exertion. It also triggers the release of stress hormones like adrenaline, which heightens alertness and increases energy levels. In essence, the sympathetic nervous system helps us respond to emergencies and mobilize our resources for survival.On the other hand, the parasympathetic nervous system is often referred to as the \"rest and digest\" response system. Its main function is to promote relaxation, conserve energy, and restore the body to a state of balance after the sympathetic response. When activated, the parasympathetic nervous system slows heart rate, constricts blood vessels, and promotes digestion and nutrient absorption. It also helps regulate bodily functions during rest, such as promoting sleep, reducing stress, and supporting the immune system. In essence, the parasympathetic nervous system helps us recover, rejuvenate, and maintain overall well-being.These two systems work in a dynamic balance, with the sympathetic system preparing the body for action and the parasympathetic system promoting relaxation and restoration. This balance is crucial for maintaining optimal health and functioning. Imbalances or dysregulation in either system can lead to various health issues, such as chronic stress, anxiety, digestive problems, or sleep disturbances.In summary, the sympathetic and parasympathetic nervous systems have different functions and effects on the body. The sympathetic system prepares us for action and stress, while the parasympathetic system promotes relaxation and restoration. Understanding and maintaining the balance between these two systems is essential for overall well-being.'"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"get_response_from_query(\"What is the difference between the sympathetic and parasympathetic nervous system?\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"config_list = [\n",
" {\n",
" 'model': 'gpt-4',\n",
" 'api_key': config['OPENAI_API_KEY']\n",
" }\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"llm_config = {\n",
" \"seed\": 42, # change the seed for different trials\n",
" \"temperature\": 0,\n",
" \"config_list\": config_list,\n",
" \"request_timeout\": 120,\n",
" \"functions\": [\n",
" {\n",
" \"name\": \"answer_neuroscience_question\",\n",
" \"description\": \"Get fact-based responses related to neuroscience and the effectiveness of different supplements and interventions\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"query\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The question to ask the model\"\n",
" }\n",
" }\n",
" },\n",
" \"required\": [\"question\"],\n",
" }\n",
" ]\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"article_writer_prompt_simple = \"\"\"\n",
"Article writer. The article must be appealing to the wider audience and the information has to be fact-checked. \n",
"In order to check facts, ask the fact checker for the information you need to perform the search. After you have written the article by iterating with fact checker, ask the user if the content is ready to publish or it needs more work.\n",
"When you respond with the status add the word TERMINATE\n",
"\"\"\"\n",
"\n",
"article_writer_prompt_complex = \"\"\"\n",
"You are an article writer. Before diving into the writing process, it's crucial to understand who you're writing for. What interests them? What questions are they seeking answers to? This will not only make your article more appealing but also help you gauge which facts to prioritize.\n",
"Once you have a clear idea of your topic and the direction of your article, loop in a fact-checker. They will be your ally in ensuring that your content is not only compelling but also accurate.\n",
"\n",
"Tip: It's essential to maintain open communication with your fact-checker and SEO optimizer. Whenever you come across a piece of information you're unsure about, reach out. They are there to help!\n",
"With your audience in mind and a fact-checker by your side, start writing. Remember, the first draft is just that - a draft. Don't be disheartened if it's not perfect. Writing is an iterative process.\n",
"After your initial draft, revisit your work. Look for areas that can be enhanced, both in terms of content and accuracy. This is where your fact-checker will shine. Engage with them, ask questions, and make revisions based on their feedback.\n",
"Once you're satisfied with your piece, do one final check. This is the time to ensure that your article flows well, is free from errors, and, most importantly, is factually sound.\n",
"After you have written the article by iterating with fact checker, ask the user if the content is ready to publish or it needs more work.\n",
"When you respond with the status add the word TERMINATE\n",
"\"\"\"\n",
"\n",
"fact_checker = autogen.AssistantAgent(\n",
" name=\"fact_checker\",\n",
" llm_config=llm_config,\n",
" human_input_mode=\"NEVER\",\n",
" code_execution_config=False,\n",
" system_message=\"\"\"Fact checker, you help the user find the relevant, fact-based information. \n",
" Only use the tools provided to do the search. Only execute the search after you have all the information needed. \n",
" Ask the article writer for the information you need to perform the search, always add the word \"BRKT\"\" at the end of your question.\n",
" When you respond with the status add the word TERMINATE\n",
" \"\"\",\n",
" function_map={\n",
" \"answer_neuroscience_question\": get_response_from_query\n",
" }\n",
")\n",
"\n",
"article_writer = autogen.AssistantAgent(\n",
" name=\"article_writer\",\n",
" llm_config=llm_config,\n",
" human_input_mode=\"NEVER\",\n",
" code_execution_config=False,\n",
" system_message=article_writer_prompt_complex,\n",
" function_map={\n",
" \"answer_neuroscience_question\": get_response_from_query\n",
" }\n",
")\n",
"\n",
"seo_optimizer = autogen.AssistantAgent(\n",
" name=\"seo_optimizer\",\n",
" llm_config=llm_config,\n",
" human_input_mode=\"NEVER\",\n",
" code_execution_config=False,\n",
" system_message=\"\"\"\n",
" SEO optimizer, you help the article writer suggest text modifications for search engine optimization. Ask the article writer to provide you with the text you need to optimize.\n",
" When you respond with the status add the word TERMINATE.\n",
" \"\"\",\n",
" function_map={\n",
" \"answer_neuroscience_question\": get_response_from_query\n",
" }\n",
")\n",
"\n",
"def check_for_termination(message):\n",
" # print(\"check_for_termination\", message)\n",
" content = message.get(\"content\")\n",
" if content != None and (\"BRKT\" in content or \"TERMINATE\" in content):\n",
" print(\"should terminate\")\n",
" return True\n",
" return False\n",
"\n",
"user_proxy = autogen.UserProxyAgent(\n",
" name=\"user_proxy\",\n",
" human_input_mode=\"TERMINATE\",\n",
" max_consecutive_auto_reply=5,\n",
" is_termination_msg=check_for_termination,\n",
" code_execution_config=False,\n",
" function_map={\n",
" \"answer_neuroscience_question\": get_response_from_query\n",
" }\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [],
"source": [
"groupchat = autogen.GroupChat(agents=[user_proxy, fact_checker, article_writer, seo_optimizer], messages=[], max_round=50)\n",
"manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=llm_config)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[33muser_proxy\u001b[0m (to chat_manager):\n",
"\n",
"\n",
" Write an article on the benefits of ashwagandha for people interested in nutrition, exercise, and a healthy lifestyle.\n",
"\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001b[33marticle_writer\u001b[0m (to chat_manager):\n",
"\n",
"Title: Unveiling the Benefits of Ashwagandha: A Powerhouse for Nutrition, Exercise, and a Healthy Lifestyle\n",
"\n",
"Introduction\n",
"\n",
"In the realm of nutrition and wellness, Ashwagandha, a revered herb in Ayurvedic medicine, has been making waves. Known scientifically as Withania somnifera, Ashwagandha is often referred to as the \"Indian ginseng\" due to its rejuvenating properties. But what exactly are the benefits of this ancient herb, and how can it contribute to a healthy lifestyle, particularly for those interested in nutrition and exercise? Let's delve into the world of Ashwagandha and uncover its potential benefits.\n",
"\n",
"The Power of Adaptogens\n",
"\n",
"Ashwagandha belongs to a class of herbs known as \"adaptogens,\" which are believed to help the body manage stress. Stress, whether physical from exercise or mental from our daily lives, can have a significant impact on our overall health and well-being. Adaptogens like Ashwagandha may help to balance our bodies and minds, promoting a sense of calm and resilience in the face of stress.\n",
"\n",
"Boosting Exercise Performance\n",
"\n",
"For fitness enthusiasts, Ashwagandha may offer some intriguing benefits. Some research suggests that it can enhance cardiorespiratory endurance, making it a potential ally for those engaging in high-intensity workouts or endurance sports. Additionally, Ashwagandha might aid in muscle recovery post-exercise, reducing muscle damage and improving strength.\n",
"\n",
"Enhancing Cognitive Function\n",
"\n",
"Beyond physical health, Ashwagandha may also support cognitive function. Some studies indicate that it can improve memory, reaction time, and task performance, making it a potential asset for those seeking to boost their mental agility alongside their physical fitness.\n",
"\n",
"Promoting a Balanced Diet\n",
"\n",
"In terms of nutrition, Ashwagandha is a powerhouse. It's packed with iron, antioxidants, and a variety of other nutrients that can complement a balanced diet. Plus, it's believed to have anti-inflammatory properties, which can be beneficial for individuals following an exercise regimen, as inflammation is often a by-product of intense workouts.\n",
"\n",
"Conclusion\n",
"\n",
"Incorporating Ashwagandha into your lifestyle could offer a range of benefits, from enhancing exercise performance to supporting cognitive function and promoting a balanced diet. As with any supplement, it's important to consult with a healthcare provider before starting a new regimen. But for those interested in nutrition, exercise, and a healthy lifestyle, Ashwagandha is certainly worth exploring.\n",
"\n",
"Now, let's fact-check this information to ensure its accuracy.\n",
"\u001b[32m***** Suggested function Call: answer_neuroscience_question *****\u001b[0m\n",
"Arguments: \n",
"\n",
"{\n",
" \"query\": \"What are the benefits of Ashwagandha?\"\n",
"}\n",
"\u001b[32m*****************************************************************\u001b[0m\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001b[35m\n",
">>>>>>>> EXECUTING FUNCTION answer_neuroscience_question...\u001b[0m\n",
"\u001b[33mfact_checker\u001b[0m (to chat_manager):\n",
"\n",
"\u001b[32m***** Response from calling function \"answer_neuroscience_question\" *****\u001b[0m\n",
"According to the transcript, Ashwagandha has several potential benefits. Firstly, it is considered a double adaptogen, meaning it may be helpful for individuals with both high and low cortisol levels. It has been traditionally used in Ayurvedic medicine for thousands of years, suggesting its potential effectiveness. While there is limited human data available, it is widely used in Integrative Medicine.Additionally, Ashwagandha has been shown to increase testosterone levels modestly and has been associated with increases in lean mass. However, it is noted that the increase in testosterone may not fully explain the increase in lean mass. It is possible that Ashwagandha's ability to decrease stress hormones, such as cortisol, contributes to its positive effects on lean mass.Overall, Ashwagandha shows promise as a supplement for stress reduction and potential benefits in testosterone levels and lean mass. However, more research is needed to fully understand its mechanisms and effectiveness.\n",
"\u001b[32m*************************************************************************\u001b[0m\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001b[33marticle_writer\u001b[0m (to chat_manager):\n",
"\n",
"Title: Unveiling the Benefits of Ashwagandha: A Powerhouse for Nutrition, Exercise, and a Healthy Lifestyle\n",
"\n",
"Introduction\n",
"\n",
"In the realm of nutrition and wellness, Ashwagandha, a revered herb in Ayurvedic medicine, has been making waves. Known scientifically as Withania somnifera, Ashwagandha is often referred to as the \"Indian ginseng\" due to its rejuvenating properties. But what exactly are the benefits of this ancient herb, and how can it contribute to a healthy lifestyle, particularly for those interested in nutrition and exercise? Let's delve into the world of Ashwagandha and uncover its potential benefits.\n",
"\n",
"The Power of Adaptogens\n",
"\n",
"Ashwagandha belongs to a class of herbs known as \"adaptogens,\" which are believed to help the body manage stress. Stress, whether physical from exercise or mental from our daily lives, can have a significant impact on our overall health and well-being. Adaptogens like Ashwagandha may help to balance our bodies and minds, promoting a sense of calm and resilience in the face of stress.\n",
"\n",
"Boosting Exercise Performance\n",
"\n",
"For fitness enthusiasts, Ashwagandha may offer some intriguing benefits. Some research suggests that it can enhance cardiorespiratory endurance, making it a potential ally for those engaging in high-intensity workouts or endurance sports. Additionally, Ashwagandha might aid in muscle recovery post-exercise, reducing muscle damage and improving strength.\n",
"\n",
"Enhancing Cognitive Function\n",
"\n",
"Beyond physical health, Ashwagandha may also support cognitive function. Some studies indicate that it can improve memory, reaction time, and task performance, making it a potential asset for those seeking to boost their mental agility alongside their physical fitness.\n",
"\n",
"Promoting a Balanced Diet\n",
"\n",
"In terms of nutrition, Ashwagandha is a powerhouse. It's packed with iron, antioxidants, and a variety of other nutrients that can complement a balanced diet. Plus, it's believed to have anti-inflammatory properties, which can be beneficial for individuals following an exercise regimen, as inflammation is often a by-product of intense workouts.\n",
"\n",
"Conclusion\n",
"\n",
"Incorporating Ashwagandha into your lifestyle could offer a range of benefits, from enhancing exercise performance to supporting cognitive function and promoting a balanced diet. As with any supplement, it's important to consult with a healthcare provider before starting a new regimen. But for those interested in nutrition, exercise, and a healthy lifestyle, Ashwagandha is certainly worth exploring.\n",
"\n",
"Fact Check: Ashwagandha is considered a double adaptogen, potentially beneficial for individuals with both high and low cortisol levels. It has been traditionally used in Ayurvedic medicine for thousands of years. Ashwagandha has been shown to increase testosterone levels modestly and has been associated with increases in lean mass. However, the increase in testosterone may not fully explain the increase in lean mass. It is possible that Ashwagandha's ability to decrease stress hormones, such as cortisol, contributes to its positive effects on lean mass. More research is needed to fully understand its mechanisms and effectiveness.\n",
"\n",
"TERMINATE\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001b[33mseo_optimizer\u001b[0m (to chat_manager):\n",
"\n",
"The article is well-written and covers the benefits of Ashwagandha comprehensively. However, to optimize it for search engines, I suggest the following modifications:\n",
"\n",
"Title: \"Ashwagandha Benefits: Boosting Nutrition, Exercise Performance, and Healthy Lifestyle\"\n",
"\n",
"Introduction\n",
"\n",
"In the world of wellness and nutrition, Ashwagandha, a prized herb in Ayurvedic medicine, is gaining popularity. Known scientifically as Withania somnifera, Ashwagandha, often dubbed the \"Indian ginseng,\" is renowned for its rejuvenating properties. But what makes this ancient herb beneficial, and how does it enhance a healthy lifestyle, especially for those keen on nutrition and exercise? Let's explore the benefits of Ashwagandha.\n",
"\n",
"The Power of Adaptogens\n",
"\n",
"Ashwagandha is a prominent member of the \"adaptogens\" herb class, believed to aid the body in stress management. Stress, whether physical from exercise or mental from daily life, can significantly affect our overall health. Adaptogens like Ashwagandha may help balance our bodies and minds, fostering calmness and resilience against stress.\n",
"\n",
"Boosting Exercise Performance\n",
"\n",
"Ashwagandha offers intriguing benefits for fitness enthusiasts. Research suggests it can enhance cardiorespiratory endurance, making it a potential companion for high-intensity workouts or endurance sports. Moreover, Ashwagandha may aid in post-exercise muscle recovery, reducing muscle damage and improving strength.\n",
"\n",
"Enhancing Cognitive Function\n",
"\n",
"Beyond physical health, Ashwagandha may also bolster cognitive function. Studies indicate it can improve memory, reaction time, and task performance, making it a potential tool for those seeking to enhance their mental agility alongside physical fitness.\n",
"\n",
"Promoting a Balanced Diet\n",
"\n",
"In the nutrition sphere, Ashwagandha is a powerhouse. It's rich in iron, antioxidants, and various other nutrients that can supplement a balanced diet. Additionally, it's believed to possess anti-inflammatory properties, beneficial for individuals following an exercise regimen, as inflammation often results from intense workouts.\n",
"\n",
"Conclusion\n",
"\n",
"Incorporating Ashwagandha into your lifestyle could offer a myriad of benefits, from enhancing exercise performance to bolstering cognitive function and promoting a balanced diet. As with any supplement, it's crucial to consult with a healthcare provider before starting a new regimen. However, for those interested in nutrition, exercise, and a healthy lifestyle, exploring Ashwagandha is certainly worthwhile.\n",
"\n",
"Fact Check: Ashwagandha is a double adaptogen, potentially beneficial for individuals with both high and low cortisol levels. It has been used in Ayurvedic medicine for thousands of years. Ashwagandha has been associated with modest increases in testosterone levels and lean mass. However, the increase in testosterone may not fully explain the increase in lean mass. It's possible that Ashwagandha's ability to decrease stress hormones, such as cortisol, contributes to its positive effects on lean mass. More research is needed to fully understand its mechanisms and effectiveness.\n",
"\n",
"TERMINATE\n",
"\n",
"--------------------------------------------------------------------------------\n",
"should terminate\n"
]
}
],
"source": [
"user_proxy.initiate_chat(\n",
" manager,\n",
" clear_history=True,\n",
" message=\"\"\"\n",
" Write an article on the benefits of ashwagandha for people interested in nutrition, exercise, and a healthy lifestyle.\n",
"\"\"\",\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "autogen",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment