Update main.py
Browse files
main.py
CHANGED
|
@@ -1,7 +1,5 @@
|
|
| 1 |
import os
|
| 2 |
-
from
|
| 3 |
-
from langchain.utilities import GoogleSearchAPIWrapper
|
| 4 |
-
from langchain.agents import AgentType, initialize_agent, Tool
|
| 5 |
from lang import G4F
|
| 6 |
from fastapi import FastAPI, Request, Path
|
| 7 |
from pydantic import BaseModel
|
|
@@ -23,38 +21,64 @@ google_api_key = os.environ["GOOGLE_API_KEY"]
|
|
| 23 |
cse_id = os.environ["GOOGLE_CSE_ID"]
|
| 24 |
model = os.environ['default_model']
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
),
|
| 34 |
-
]
|
| 35 |
-
|
| 36 |
-
llm = G4F(model=model)
|
| 37 |
-
agent_chain = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
|
| 38 |
|
| 39 |
|
| 40 |
@app.get("/")
|
| 41 |
def hello():
|
| 42 |
return "Hello! My name is Linlada."
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
@app.post('/linlada')
|
| 45 |
-
|
| 46 |
llm = G4F(model=model)
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
| 50 |
return chat
|
| 51 |
|
| 52 |
-
@app.post('/search')
|
| 53 |
-
async def searches(request: Request):
|
| 54 |
-
data = await request.json()
|
| 55 |
-
prompt = data['prompt']
|
| 56 |
-
response = agent_chain.run(input=prompt)
|
| 57 |
-
return response
|
| 58 |
|
| 59 |
class User(BaseModel):
|
| 60 |
prompt: str
|
|
|
|
| 1 |
import os
|
| 2 |
+
from googleapiclient.discovery import build
|
|
|
|
|
|
|
| 3 |
from lang import G4F
|
| 4 |
from fastapi import FastAPI, Request, Path
|
| 5 |
from pydantic import BaseModel
|
|
|
|
| 21 |
cse_id = os.environ["GOOGLE_CSE_ID"]
|
| 22 |
model = os.environ['default_model']
|
| 23 |
|
| 24 |
+
def search_google(query):
|
| 25 |
+
service = build("customsearch", "v1", developerKey=google_api_key)
|
| 26 |
+
result = service.cse().list(
|
| 27 |
+
q=query,
|
| 28 |
+
cx=cse_id
|
| 29 |
+
).execute()
|
| 30 |
+
return result['items']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
|
| 33 |
@app.get("/")
|
| 34 |
def hello():
|
| 35 |
return "Hello! My name is Linlada."
|
| 36 |
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def gpt_with_google_search(prompt):
|
| 40 |
+
search_results = search_google(prompt)
|
| 41 |
+
text = ""
|
| 42 |
+
ref = ""
|
| 43 |
+
for item in search_results:
|
| 44 |
+
text += item['title'] + "\n" + item['snippet'] + "\n\n"
|
| 45 |
+
ref += "- {} ({})\n".format(item['title'], item['link'])
|
| 46 |
+
results = generate_text(f'Summarize: {text}')
|
| 47 |
+
res = "{} \n\n {}".format(results, ref)
|
| 48 |
+
return res
|
| 49 |
+
|
| 50 |
+
def gpt_with_google_search(prompt):
|
| 51 |
+
search_results = search_google(prompt)
|
| 52 |
+
text = ""
|
| 53 |
+
ref = ""
|
| 54 |
+
ref_count = 1
|
| 55 |
+
for item in search_results:
|
| 56 |
+
text += item['title'] + "\n" + item['snippet'] + "\n\n"
|
| 57 |
+
ref += " [{}] {} ({})\n".format(ref_count, item['title'], item['link'])
|
| 58 |
+
ref_count += 1
|
| 59 |
+
results = generate_text(f'Summarize: {text}')
|
| 60 |
+
res = "{} \n\n{}".format(results, ref)
|
| 61 |
+
return res
|
| 62 |
+
|
| 63 |
+
class Linlada(BaseModel):
|
| 64 |
+
prompt: str
|
| 65 |
+
web_access: str = False
|
| 66 |
+
sampler: str = None
|
| 67 |
+
seed: int = None
|
| 68 |
+
neg: str = None
|
| 69 |
+
|
| 70 |
+
|
| 71 |
@app.post('/linlada')
|
| 72 |
+
def linlada(request: Linlada):
|
| 73 |
llm = G4F(model=model)
|
| 74 |
+
prompt = request.prompt
|
| 75 |
+
web_access = request.web_access
|
| 76 |
+
if web_access == True:
|
| 77 |
+
chat = gpt_with_google_search(prompt)
|
| 78 |
+
else:
|
| 79 |
+
chat = llm(prompt)
|
| 80 |
return chat
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
class User(BaseModel):
|
| 84 |
prompt: str
|