Spaces:
Runtime error
Runtime error
Create app-original.py
Browse files- app-original.py +134 -0
app-original.py
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
from langchain.agents import Tool
|
| 4 |
+
from langchain.tools import BaseTool
|
| 5 |
+
from langchain.agents import load_tools
|
| 6 |
+
from langchain.memory import ConversationBufferMemory
|
| 7 |
+
from langchain.memory import ConversationBufferWindowMemory
|
| 8 |
+
#from langchain.chat_models import ChatOpenAI
|
| 9 |
+
from langchain.utilities import GoogleSearchAPIWrapper
|
| 10 |
+
from langchain.utilities import GoogleSerperAPIWrapper
|
| 11 |
+
from langchain.agents import initialize_agent
|
| 12 |
+
import gradio as gr
|
| 13 |
+
from langchain.chains.question_answering import load_qa_chain
|
| 14 |
+
from langchain import PromptTemplate, LLMChain
|
| 15 |
+
from langchain import HuggingFaceHub
|
| 16 |
+
from pathlib import Path
|
| 17 |
+
from time import sleep
|
| 18 |
+
from langchain.agents import AgentType
|
| 19 |
+
#from langchain.llms import OpenAI
|
| 20 |
+
|
| 21 |
+
from langchain.agents import AgentOutputParser
|
| 22 |
+
from langchain.agents.conversational_chat.prompt import FORMAT_INSTRUCTIONS
|
| 23 |
+
from langchain.output_parsers.json import parse_json_markdown
|
| 24 |
+
from langchain.schema import AgentAction, AgentFinish
|
| 25 |
+
|
| 26 |
+
import os
|
| 27 |
+
import random
|
| 28 |
+
import string
|
| 29 |
+
from dotenv import load_dotenv
|
| 30 |
+
load_dotenv()
|
| 31 |
+
|
| 32 |
+
from langchain.agents import ZeroShotAgent, Tool, AgentExecutor
|
| 33 |
+
from langchain import OpenAI, SerpAPIWrapper, LLMChain, LLMMathChain
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
OPENAI_API_KEY =os.getenv("OPENAI_API_KEY")
|
| 37 |
+
GOOGLE_API_KEY =os.getenv("GOOGLE_API_KEY")
|
| 38 |
+
GOOGLE_CSE_ID =os.getenv("GOOGLE_CSE_ID")
|
| 39 |
+
#HUGGINGFACEHUB_API_TOKEN = os.getenv('HUGGINGFACEHUB_API_TOKEN')
|
| 40 |
+
#repo_id = os.getenv('repo_id')
|
| 41 |
+
HUGGINGFACEHUB_API_TOKEN = os.environ.get('HUGGINGFACEHUB_API_TOKEN')
|
| 42 |
+
SERPAPI_API_KEY=os.environ.get('SERPAPI_API_KEY')
|
| 43 |
+
repo_id = os.environ.get('repo_id')
|
| 44 |
+
|
| 45 |
+
template = """Question: {question}
|
| 46 |
+
|
| 47 |
+
Answer: Let's think step by step."""
|
| 48 |
+
|
| 49 |
+
prompt_lora = PromptTemplate(template=template, input_variables=["question"])
|
| 50 |
+
|
| 51 |
+
llm=HuggingFaceHub(repo_id=repo_id)
|
| 52 |
+
|
| 53 |
+
lora_chain = LLMChain(prompt=prompt_lora,llm = llm)
|
| 54 |
+
|
| 55 |
+
question = "How many fish can live in a ocean that is as big as North America?"
|
| 56 |
+
|
| 57 |
+
print(lora_chain.run(question))
|
| 58 |
+
|
| 59 |
+
# This is an LLMChain to write a synopsis given a title of a play.
|
| 60 |
+
|
| 61 |
+
template = """You are a engineer. Given the title, it is your job to write a synopsis for that title.
|
| 62 |
+
|
| 63 |
+
Title: {title}
|
| 64 |
+
Engineer: This is a synopsis for the above title:"""
|
| 65 |
+
|
| 66 |
+
prompt_template = PromptTemplate(input_variables=["title"],
|
| 67 |
+
template=template)
|
| 68 |
+
|
| 69 |
+
synopsis_chain = LLMChain(llm=llm,
|
| 70 |
+
prompt=prompt_template)
|
| 71 |
+
|
| 72 |
+
template = """You are a MBA from Harvard. Given the synopsis, it is your job to write a review for that synopsis.
|
| 73 |
+
|
| 74 |
+
Synopsis:
|
| 75 |
+
{synopsis}
|
| 76 |
+
Review from a Harvard MBA of above synopsis:"""
|
| 77 |
+
|
| 78 |
+
prompt_template = PromptTemplate(input_variables=["synopsis"], template=template)
|
| 79 |
+
|
| 80 |
+
review_chain = LLMChain(llm=llm, prompt=prompt_template)
|
| 81 |
+
|
| 82 |
+
from langchain.chains import SimpleSequentialChain
|
| 83 |
+
overall_chain = SimpleSequentialChain(chains=[synopsis_chain, review_chain], verbose=True)
|
| 84 |
+
|
| 85 |
+
overall_chain.run("How big is London Bridge?")
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
search = SerpAPIWrapper()
|
| 89 |
+
llm_math_chain = LLMMathChain(llm=llm, verbose=True)
|
| 90 |
+
|
| 91 |
+
tools = [
|
| 92 |
+
Tool(
|
| 93 |
+
name = "Search",
|
| 94 |
+
func=search.run,
|
| 95 |
+
description="useful for when you need to answer questions about current events"
|
| 96 |
+
),
|
| 97 |
+
Tool(
|
| 98 |
+
name="Calculator",
|
| 99 |
+
func=llm_math_chain.run,
|
| 100 |
+
description="useful for when you need to answer questions about Divide, Multiply, Add and Subtract"
|
| 101 |
+
)
|
| 102 |
+
]
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
prefix = """Answer the following questions as best you can. Think through step by step.
|
| 106 |
+
You have access to the following tools:"""
|
| 107 |
+
suffix = """Begin! Remember to think step by step
|
| 108 |
+
|
| 109 |
+
Question: {input}
|
| 110 |
+
{agent_scratchpad}"""
|
| 111 |
+
|
| 112 |
+
prompt = ZeroShotAgent.create_prompt(
|
| 113 |
+
tools,
|
| 114 |
+
prefix=prefix,
|
| 115 |
+
suffix=suffix,
|
| 116 |
+
input_variables=["input", "agent_scratchpad"]
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
agent_chain = LLMChain(llm=llm, prompt=prompt)
|
| 120 |
+
|
| 121 |
+
tool_names = [tool.name for tool in tools]
|
| 122 |
+
agent = ZeroShotAgent(llm_chain=agent_chain,
|
| 123 |
+
allowed_tools=tool_names)
|
| 124 |
+
|
| 125 |
+
agent_executor = AgentExecutor.from_agent_and_tools(agent=agent,
|
| 126 |
+
tools=tools,
|
| 127 |
+
verbose=True,
|
| 128 |
+
max_iterations=3)
|
| 129 |
+
|
| 130 |
+
agent.save("custom_agent.json")
|
| 131 |
+
|
| 132 |
+
result=agent_executor.run("How many people live in canada as of 2023?")
|
| 133 |
+
|
| 134 |
+
print("Result: "+str(result))
|