Spaces:
Runtime error
Runtime error
| import os | |
| import requests | |
| from langchain.agents import Tool | |
| from langchain.tools import BaseTool | |
| from langchain.agents import load_tools | |
| from langchain.memory import ConversationBufferMemory | |
| from langchain.memory import ConversationBufferWindowMemory | |
| #from langchain.chat_models import ChatOpenAI | |
| from langchain.utilities import GoogleSearchAPIWrapper | |
| from langchain.utilities import GoogleSerperAPIWrapper | |
| from langchain.agents import initialize_agent | |
| import gradio as gr | |
| from langchain.chains.question_answering import load_qa_chain | |
| from langchain import PromptTemplate, LLMChain | |
| from langchain import HuggingFaceHub | |
| from pathlib import Path | |
| from time import sleep | |
| from langchain.agents import AgentType | |
| #from langchain.llms import OpenAI | |
| from langchain.agents import AgentOutputParser | |
| from langchain.agents.conversational_chat.prompt import FORMAT_INSTRUCTIONS | |
| from langchain.output_parsers.json import parse_json_markdown | |
| from langchain.schema import AgentAction, AgentFinish | |
| import os | |
| import random | |
| import string | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| from langchain.agents import ZeroShotAgent, Tool, AgentExecutor | |
| from langchain import OpenAI, SerpAPIWrapper, LLMChain, LLMMathChain | |
| OPENAI_API_KEY =os.getenv("OPENAI_API_KEY") | |
| GOOGLE_API_KEY =os.getenv("GOOGLE_API_KEY") | |
| GOOGLE_CSE_ID =os.getenv("GOOGLE_CSE_ID") | |
| #HUGGINGFACEHUB_API_TOKEN = os.getenv('HUGGINGFACEHUB_API_TOKEN') | |
| #repo_id = os.getenv('repo_id') | |
| HUGGINGFACEHUB_API_TOKEN = os.environ.get('HUGGINGFACEHUB_API_TOKEN') | |
| SERPAPI_API_KEY=os.environ.get('SERPAPI_API_KEY') | |
| repo_id = os.environ.get('repo_id') | |
| template = """Question: {question} | |
| Answer: Let's think step by step.""" | |
| prompt_lora = PromptTemplate(template=template, input_variables=["question"]) | |
| llm=HuggingFaceHub(repo_id=repo_id) | |
| lora_chain = LLMChain(prompt=prompt_lora,llm = llm) | |
| question = "How many fish can live in a ocean that is as big as North America?" | |
| print(lora_chain.run(question)) | |
| # This is an LLMChain to write a synopsis given a title of a play. | |
| template = """You are a engineer. Given the title, it is your job to write a synopsis for that title. | |
| Title: {title} | |
| Engineer: This is a synopsis for the above title:""" | |
| prompt_template = PromptTemplate(input_variables=["title"], | |
| template=template) | |
| synopsis_chain = LLMChain(llm=llm, | |
| prompt=prompt_template) | |
| template = """You are a MBA from Harvard. Given the synopsis, it is your job to write a review for that synopsis. | |
| Synopsis: | |
| {synopsis} | |
| Review from a Harvard MBA of above synopsis:""" | |
| prompt_template = PromptTemplate(input_variables=["synopsis"], template=template) | |
| review_chain = LLMChain(llm=llm, prompt=prompt_template) | |
| from langchain.chains import SimpleSequentialChain | |
| overall_chain = SimpleSequentialChain(chains=[synopsis_chain, review_chain], verbose=True) | |
| overall_chain.run("How big is London Bridge?") | |
| search = SerpAPIWrapper() | |
| llm_math_chain = LLMMathChain(llm=llm, verbose=True) | |
| tools = [ | |
| Tool( | |
| name = "Search", | |
| func=search.run, | |
| description="useful for when you need to answer questions about current events" | |
| ), | |
| Tool( | |
| name="Calculator", | |
| func=llm_math_chain.run, | |
| description="useful for when you need to answer questions about Divide, Multiply, Add and Subtract" | |
| ) | |
| ] | |
| prefix = """Answer the following questions as best you can. Think through step by step. | |
| You have access to the following tools:""" | |
| suffix = """Begin! Remember to think step by step | |
| Question: {input} | |
| {agent_scratchpad}""" | |
| prompt = ZeroShotAgent.create_prompt( | |
| tools, | |
| prefix=prefix, | |
| suffix=suffix, | |
| input_variables=["input", "agent_scratchpad"] | |
| ) | |
| agent_chain = LLMChain(llm=llm, prompt=prompt) | |
| tool_names = [tool.name for tool in tools] | |
| agent = ZeroShotAgent(llm_chain=agent_chain, | |
| allowed_tools=tool_names) | |
| agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, | |
| tools=tools, | |
| verbose=True, | |
| max_iterations=3) | |
| agent.save("custom_agent.json") | |
| result=agent_executor.run("How many people live in canada as of 2023?") | |
| print("Result: "+str(result)) |