Sneha Dixit commited on
Commit
5b90a2e
1 Parent(s): 283373b

[UPDATE] api

Browse files
Files changed (5) hide show
  1. auth.py +2 -1
  2. client.py +55 -15
  3. model.py +9 -0
  4. requirements.txt +2 -1
  5. server.py +9 -5
auth.py CHANGED
@@ -2,9 +2,10 @@ from fastapi.security.api_key import APIKeyHeader
2
  from fastapi import Security, HTTPException
3
  import os
4
 
5
- API_Keys = [os.environ.get('API_KEY')]
6
  api_key_header = APIKeyHeader(name="x-api-key", auto_error=False)
7
 
8
  async def api_key_auth(api_key: str = Security(api_key_header)):
 
9
  if api_key not in API_Keys:
10
  raise HTTPException(status_code=401, detail="Missing or invalid API key")
 
2
  from fastapi import Security, HTTPException
3
  import os
4
 
5
+
6
  api_key_header = APIKeyHeader(name="x-api-key", auto_error=False)
7
 
8
  async def api_key_auth(api_key: str = Security(api_key_header)):
9
+ API_Keys = [os.environ.get('API_KEY')]
10
  if api_key not in API_Keys:
11
  raise HTTPException(status_code=401, detail="Missing or invalid API key")
client.py CHANGED
@@ -1,23 +1,63 @@
1
  from openai import AzureOpenAI
2
  import os
 
3
 
4
- client = AzureOpenAI(
5
- api_key=os.environ.get('OPENAI_API_KEY'),
6
- api_version=os.environ.get('MODEL_VERSION'),
7
- azure_endpoint = os.environ.get('END_POINT')
 
 
8
  )
 
9
 
10
- prompt = 'Write a tagline for an ice cream shop.'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- response = client.completions.create(
13
- model=os.environ.get('MODEL_NAME'),
14
- prompt=prompt,
15
- max_tokens=50,
16
- n=1,
17
- stop=None,
18
- temperature=0.7
19
- )
20
 
21
- result = response.choices[0].text.strip()
 
 
 
 
 
22
 
23
- print(result)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from openai import AzureOpenAI
2
  import os
3
+ import model
4
 
5
+ def create_client():
6
+ print("In create client")
7
+ client = AzureOpenAI(
8
+ api_key=os.environ.get('OPENAI_API_KEY'),
9
+ api_version=os.environ.get('MODEL_VERSION'),
10
+ azure_endpoint = os.environ.get('END_POINT')
11
  )
12
+ return client
13
 
14
+ def get_completion(ticket:model.Ticket):
15
+ print("In get completion")
16
+ client = create_client()
17
+ prompt = create_prompt(ticket.subject, ticket.description)
18
+ response = client.completions.create(
19
+ model=os.environ.get('MODEL_NAME'),
20
+ prompt=prompt,
21
+ max_tokens=100,
22
+ n=1,
23
+ stop=None,
24
+ temperature=0.7
25
+ )
26
+ result = response.choices[0].text.strip()
27
+ print(result)
28
+ return result
29
 
30
+ def create_prompt(subject:str, desc:str):
31
+ print("In create Prompt")
32
+ # add booking data here
33
+ data = ""
34
+ prompt = '''
35
+ You are an AI Customer Service Agent for booking.com. Please use the below instructions to answer.
36
+ What is the intent of this request ```
37
+ Subject: {{subject}} Description: {{desc}}``` in 1 single word? Use these options for intent :
38
 
39
+ Complaint
40
+ Information Request
41
+ Service Request
42
+ Cancellation
43
+ Change
44
+ Refund Request
45
 
46
+ In the context of the previous request,
47
+ If the intent is Information Request, use the ``` Booking Details: {{data}}``` to provide information as part of resolution response.
48
+ otherwise Suggest a resolution response to address the customer query from the following actions list:
49
+
50
+ Issue Refund
51
+ Cancellation
52
+ Change in booking requested
53
+
54
+ Finally, Summarize the message: ```
55
+ Subject: {{subject}}Description:{{desc}}``` and generate a mail content for the customer about the whole conversation.
56
+
57
+ Give the final result like this:
58
+ intent= {your intent}
59
+ suggested_response= {your response}
60
+ summary= {your summary}
61
+ mail={generated_mail}
62
+ '''
63
+ return prompt
model.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ from pydantic import BaseModel
2
+
3
+ class Ticket(BaseModel):
4
+ subject: str
5
+ description: str
6
+
7
+ class TicketResponse(BaseModel):
8
+ subject: str
9
+ description: str
requirements.txt CHANGED
@@ -1,3 +1,4 @@
1
  fastapi==0.99.1
2
  uvicorn
3
- openai
 
 
1
  fastapi==0.99.1
2
  uvicorn
3
+ openai
4
+ pydantic
server.py CHANGED
@@ -1,6 +1,8 @@
1
  from fastapi import FastAPI, Depends
2
  import os
3
  import auth
 
 
4
 
5
  app = FastAPI()
6
 
@@ -8,9 +10,11 @@ app = FastAPI()
8
  async def welcome():
9
  return "Hello, Welcome to AI space!"
10
 
11
- @app.get("/generate")
12
  async def generate(api_key = Depends(auth.api_key_auth)):
13
- # add the logic to call gpt here
14
- os.environ.get('test')
15
- response = os.environ.get('test')
16
- return "All good. You only get this message if you're authenticated "+ response
 
 
 
1
  from fastapi import FastAPI, Depends
2
  import os
3
  import auth
4
+ import client
5
+ import model
6
 
7
  app = FastAPI()
8
 
 
10
  async def welcome():
11
  return "Hello, Welcome to AI space!"
12
 
13
+ @app.get("/test")
14
  async def generate(api_key = Depends(auth.api_key_auth)):
15
+ return "All good. You only get this message if you're authenticated"
16
+
17
+ @app.post("/generate")
18
+ async def generate(ticket: model.Ticket, api_key = Depends(auth.api_key_auth)):
19
+ response = client.get_completion(ticket)
20
+ return response