Update main.py
Browse files
main.py
CHANGED
|
@@ -1,6 +1,10 @@
|
|
| 1 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from fastapi import FastAPI
|
| 3 |
-
import
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
|
| 6 |
app = FastAPI()
|
|
@@ -13,21 +17,42 @@ app.add_middleware( # add the middleware
|
|
| 13 |
allow_headers=["*"], # allow all headers
|
| 14 |
)
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
@app.get("/")
|
| 17 |
-
def
|
| 18 |
-
return "Hello
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
result = loop.run_until_complete(generate(prompt))
|
| 32 |
-
loop.close()
|
| 33 |
-
return result
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from langchain.memory import ConversationBufferMemory
|
| 3 |
+
from langchain.utilities import GoogleSearchAPIWrapper
|
| 4 |
+
from langchain.agents import initialize_agent, Tool
|
| 5 |
+
from lang import G4F
|
| 6 |
from fastapi import FastAPI
|
| 7 |
+
from pydantic import BaseModel
|
| 8 |
from fastapi.middleware.cors import CORSMiddleware
|
| 9 |
|
| 10 |
app = FastAPI()
|
|
|
|
| 17 |
allow_headers=["*"], # allow all headers
|
| 18 |
)
|
| 19 |
|
| 20 |
+
|
| 21 |
+
google_api_key = os.environ["GOOGLE_API_KEY"]
|
| 22 |
+
cse_id = os.environ["GOOGLE_CSE_ID"]
|
| 23 |
+
model = os.environ['default_model']
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
search = GoogleSearchAPIWrapper()
|
| 27 |
+
tools = [
|
| 28 |
+
Tool(
|
| 29 |
+
name ="Search" ,
|
| 30 |
+
func=search.run,
|
| 31 |
+
description="useful when you need to answer questions about current events"
|
| 32 |
+
),
|
| 33 |
+
]
|
| 34 |
+
|
| 35 |
+
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
| 36 |
+
|
| 37 |
+
llm = G4F(model=model)
|
| 38 |
+
agent_chain = initialize_agent(tools, llm, agent="chat-conversational-react-description",
|
| 39 |
+
verbose=True, memory=memory)
|
| 40 |
+
|
| 41 |
+
class messages(BaseModel):
|
| 42 |
+
messages: str
|
| 43 |
+
|
| 44 |
@app.get("/")
|
| 45 |
+
def gello():
|
| 46 |
+
return "Hello! My name is Linlada."
|
| 47 |
+
|
| 48 |
+
@app.post('/linlada')
|
| 49 |
+
def hello_post(message: messages):
|
| 50 |
+
llm = G4F(model=model)
|
| 51 |
+
chat = llm(message)
|
| 52 |
+
return chat
|
| 53 |
+
|
| 54 |
+
@app.post('/search')
|
| 55 |
+
def searches(message: messages):
|
| 56 |
+
response = agent_chain.run(input=message)
|
| 57 |
+
return response
|
| 58 |
+
|
|
|
|
|
|
|
|
|