ka1kuk commited on
Commit
b89f196
1 Parent(s): 7102544

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +44 -19
main.py CHANGED
@@ -1,6 +1,10 @@
1
- import Linlada
 
 
 
 
2
  from fastapi import FastAPI
3
- import asyncio
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 hello():
18
- return "Hello, I'm Artist"
19
-
20
- def generate(prompt):
21
- response = Linlada._create_completion(model='gpt-4', messages=[
22
- {"role": "user", "content": prompt}], stream=True)
23
- chat = list(response)
24
- sentence = ''.join(chat)
25
- return sentence
26
-
27
- @app.get('/linlada/{prompt}')
28
- async def generate_image_route(prompt: str):
29
- loop = asyncio.new_event_loop()
30
- asyncio.set_event_loop(loop)
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
+