File size: 4,509 Bytes
11acc44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from langchain.agents import initialize_agent, Tool
from langchain_groq import ChatGroq
from langchain.utilities import SerpAPIWrapper, WikipediaAPIWrapper
from langchain_community.tools import WikipediaQueryRun
from langchain_experimental.tools import PythonREPLTool
from langchain_community.tools import DuckDuckGoSearchRun
from langchain_community.tools import ArxivQueryRun
from langchain_community.tools import PubmedQueryRun
from langchain_community.tools import ShellTool
from langchain_community.utilities.requests import RequestsWrapper
from langchain.chains.conversation.memory import ConversationBufferWindowMemory
from fastapi import FastAPI
from pydantic import BaseModel
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
import os
from dotenv import load_dotenv
load_dotenv()

app=FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

class Chat_input(BaseModel):
    input: str


os.environ["GROQ_API_KEY"] = os.getenv("GROQ_API_KEY")
# os.environ["SERPAPI_API_KEY"] = os.getenv("SERPAPI_API_KEY")


llm = ChatGroq(model="llama3-70b-8192",
            temperature=0.5,
            max_tokens=None,
            timeout=60,
            max_retries=2,
            api_key=os.getenv("GROQ_API_KEY"),)


search = SerpAPIWrapper(serpapi_api_key=os.getenv("SERPAPI_API_KEY"),search_engine="google")
youtube = SerpAPIWrapper(serpapi_api_key=os.getenv("SERPAPI_API_KEY"), search_engine="youtube")
wikipedia = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper())
duckduckgo_search = DuckDuckGoSearchRun()
arxiv = ArxivQueryRun()
pubmed = PubmedQueryRun()
requests_tool = RequestsWrapper()

def calculate_bmi(input_string):
    try:
        height_cm, weight_kg = map(float, input_string.split(','))
        height_m = height_cm / 100
        bmi = weight_kg / (height_m ** 2)
        return f"The BMI is {bmi:.2f}"
    except ValueError:
        return "Error: Please provide input in the format 'height,weight'."


 

tools = [
    Tool(
        name="BMI Calculator",
        func=calculate_bmi,
        description="Calculates BMI when given height in cm and weight in kg. Input should be two numbers separated by a comma: height,weight"
    ),
    Tool(
        name="Youtube",
        func=youtube.run,
        description="Search YouTube for videos. Input should be a query string."
    ),
    Tool(
        name="Search",
        func=search.run,
        description="Useful for when you need to answer questions youtube links, any usefull links."
    ),
    Tool(
        name="Wikipedia",
        func=wikipedia.run,
        description="Useful for when you need detailed information on a topic. Use this for historical facts, definitions, or in-depth knowledge on a subject."
    ),
    Tool(
        name="Python REPL",
        func=PythonREPLTool().run,
        description="Useful for when you need to execute Python code, especially for calculations or data processing."
    ),
    Tool(
        name="DuckDuckGo Search",
        func=duckduckgo_search.run,
        description="Useful for searching the internet for current information, used it for google search, past information and dark world or hacking related data. Ip address and other information."
    ),
    Tool(
        name="ArXiv",
        func=arxiv.run,
        description="Useful for searching and retrieving scientific papers from arXiv."
    ),
    Tool(
        name="PubMed",
        func=pubmed.run,
        description="Useful for searching and retrieving biomedical literature from PubMed."
    ),
    Tool(
        name="Requests",
        func=requests_tool.get,
        description="Useful for making HTTP requests to websites and APIs."
    ),
    Tool(
        name="Shell",
        func=ShellTool().run,
        description="Useful for running shell commands. Use with caution!"
    )
]

memory=ConversationBufferWindowMemory(k=20, memory_key="chat_history", return_messages=True)

agent = initialize_agent(tools=tools,
                         llm=llm,
                         memory=memory,
                         max_iterations=3,
                         early_stopping_method="generate",
                         agent="zero-shot-react-description",
                         verbose=True,
                         min_tokens=1000,
                         handle_parsing_errors=True)


@app.post("/search")
async def search(input: Chat_input):
    return agent.run(input.input)