Spaces:
Runtime error
Runtime error
File size: 8,156 Bytes
e26f7dd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# -*- coding: utf-8 -*-
"""week2assignment_shakes.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1OfNmkHMwkuJONUG4yQHDwJiuzLJvF1kJ
"""
!pip install openai langchain python-dotenv -q
!echo openai_api_key="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" > .env
import os
import openai
from dotenv import load_dotenv
load_dotenv(".env")
openai.api_key = os.environ.get("openai_api_key")
from IPython.display import display, Markdown
def disp_markdown(text: str) -> None:
display(Markdown(text))
from langchain.chat_models import ChatOpenAI
from langchain.schema import HumanMessage
chat_model = ChatOpenAI(model_name="gpt-3.5-turbo", openai_api_key=os.environ.get("openai_api_key"))
from langchain.schema import (
AIMessage,
HumanMessage,
SystemMessage
)
# The SystemMessage is associated with the system role
system_message = SystemMessage(content="You are a food critic.")
# The HumanMessage is associated with the user role
user_message = HumanMessage(content="Do you think Kraft Dinner constitues fine dining?")
# The AIMessage is associated with the assistant role
assistant_message = AIMessage(content="Egads! No, it most certainly does not!")
second_user_message = HumanMessage(content="What about Red Lobster, surely that is fine dining!")
# create the list of prompts
list_of_prompts = [
system_message,
user_message,
assistant_message,
second_user_message
]
# we can just call our chat_model on the list of prompts!
chat_model(list_of_prompts)
from langchain.prompts.chat import (
ChatPromptTemplate,
SystemMessagePromptTemplate,
HumanMessagePromptTemplate
)
# we can signify variables we want access to by wrapping them in {}
system_prompt_template = "You are an expert in {SUBJECT}, and you're currently feeling {MOOD}"
system_prompt_template = SystemMessagePromptTemplate.from_template(system_prompt_template)
user_prompt_template = "{CONTENT}"
user_prompt_template = HumanMessagePromptTemplate.from_template(user_prompt_template)
# put them together into a ChatPromptTemplate
chat_prompt = ChatPromptTemplate.from_messages([system_prompt_template, user_prompt_template])
formatted_chat_prompt = chat_prompt.format_prompt(SUBJECT="cheeses", MOOD="quite tired", CONTENT="Hi, what are the finest cheeses?").to_messages()
disp_markdown(chat_model(formatted_chat_prompt).content)
from langchain.chains import LLMChain
chain = LLMChain(llm=chat_model, prompt=chat_prompt)
disp_markdown(chain.run(SUBJECT="classic cars", MOOD="angry", CONTENT="Is the 67 Chevrolet Impala a good vehicle?"))
!wget https://erki.lap.ee/failid/raamatud/guide1.txt
with open("guide1.txt") as f:
hitchhikersguide = f.read()
from langchain.text_splitter import CharacterTextSplitter
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0, separator = "\n")
texts = text_splitter.split_text(hitchhikersguide)
from langchain.embeddings.openai import OpenAIEmbeddings
os.environ["OPENAI_API_KEY"] = openai.api_key
embeddings = OpenAIEmbeddings()
# !pip install chromadb==0.3.22 tiktoken -q
# !pip install chromadb -U
!pip install pydantic -q
import chromadb
from langchain.vectorstores.chroma import Chroma
docsearch = Chroma.from_texts(texts, embeddings, metadatas=[{"source": str(i)} for i in range(len(texts))]).as_retriever()
query = "What makes towels important?"
docs = docsearch.get_relevant_documents(query)
docs[0]
from langchain.chains.question_answering import load_qa_chain
from langchain.llms import OpenAI
chain = load_qa_chain(OpenAI(temperature=0), chain_type="stuff")
query = "What makes towels important?"
chain.run(input_documents=docs, question=query)
"""# Assignment 2
"""
!git clone https://github.com/TheMITTech/shakespeare
from glob import glob
files = glob("./shakespeare/**/*.html")
import shutil
import os
os.mkdir('./data')
destination_folder = './data/'
for html_file in files:
shutil.move(html_file, destination_folder + html_file.split("/")[-1])
!pip install beautifulsoup4 -q
from langchain.document_loaders import BSHTMLLoader, DirectoryLoader
bshtml_dir_loader = DirectoryLoader('./data/', loader_cls=BSHTMLLoader)
data = bshtml_dir_loader.load()
from langchain.text_splitter import RecursiveCharacterTextSplitter
text_splitter = RecursiveCharacterTextSplitter(
chunk_size = 1000,
chunk_overlap = 20,
length_function = len,
)
documents = text_splitter.split_documents(data)
persist_directory = "vector_db"
vectordb = Chroma.from_documents(documents=documents, embedding=embeddings, persist_directory=persist_directory)
vectordb.persist()
vectordb = None
vectordb = Chroma(persist_directory=persist_directory, embedding_function=embeddings)
llm = ChatOpenAI(temperature=0, model="gpt-4")
doc_retriever = vectordb.as_retriever()
from langchain.chains import RetrievalQA
shakespeare_qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=doc_retriever)
shakespeare_qa.run("Who is Hamlet'?")
!pip install google-search-results -q
os.environ["SERPAPI_API_KEY"] = "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"
from langchain.utilities import SerpAPIWrapper
search = SerpAPIWrapper()
from langchain.agents import initialize_agent, Tool
from langchain.agents import AgentType
from langchain.tools import BaseTool
from langchain.llms import OpenAI
from langchain import LLMMathChain, SerpAPIWrapper
tools = [
Tool(
name = "Shakespeare QA System",
func=shakespeare_qa.run,
description="useful for when you need to answer questions about Shakespeare's works. Input should be a fully formed question."
),
Tool(
name = "SERP API Search",
func=search.run,
description="useful for when you need to answer questions about ruff (a python linter). Input should be a fully formed question."
),
]
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
agent.run("What is Hamlet and more importantly who is hamlet?")
from langchain.memory import ConversationBufferMemory, ReadOnlySharedMemory
memory = ConversationBufferMemory(memory_key="chat_history")
readonlymemory = ReadOnlySharedMemory(memory=memory)
shakespeare_qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=doc_retriever, memory=readonlymemory)
tools = [
Tool(
name = "Shakespeare QA System",
func=shakespeare_qa.run,
description="useful for when you need to answer questions about Shakespeare's works. Input should be a fully formed question."
),
Tool(
name = "SERP API Search",
func=search.run,
description="useful for when you need to answer questions about ruff (a python linter). Input should be a fully formed question."
),
]
from langchain.agents import ZeroShotAgent, Tool, AgentExecutor
prefix = """Have a conversation with a human, answering the following questions as best you can. You have access to the following tools:"""
suffix = """Begin!"
{chat_history}
Question: {input}
{agent_scratchpad}"""
prompt = ZeroShotAgent.create_prompt(
tools,
prefix=prefix,
suffix=suffix,
input_variables=["input", "chat_history", "agent_scratchpad"]
)
from langchain import OpenAI, LLMChain, PromptTemplate
llm_chain = LLMChain(llm=llm, prompt=prompt)
agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools, verbose=True)
agent_chain = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True, memory=memory)
agent_chain.run(input="Who is Hamlet and What is Hamlet?")
agent_chain.run(input="What age was he in the play?")
agent_chain.run(input="Did he live through the play?")
agent_chain.run(input="What age did you think he was if you approximate without directly reading it from the play? You make the inference on his acts and ages of people in his life")
!pip install gradio
import gradio as gr
def the_app(text):
return agent_chain.run(input=text)
x=the_app('who is gertrude?')
x
iface = gr.Interface(fn= the_app, inputs= "text", outputs="text",title="Shakespearean")
iface.launch()
7688iu78u87u78i |