File size: 2,650 Bytes
4c05b4a
 
43e6e5d
4c05b4a
1cd99ec
 
4c05b4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1cd99ec
4c05b4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1cd99ec
 
 
 
 
4c05b4a
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
import google.generativeai as genai
import json
import os

genai.configure(api_key="AIzaSyAT-eqTQUIpoVpmM-PL5hR5el1n6YqZRV4")
# genai.configure(api_key=os.getenv("API_KEY"))
generation_config = {
  "temperature": 0.9,
  "top_p": 1,
  "top_k": 0,
  "max_output_tokens": 2048,
  "response_mime_type": "text/plain",
}
safety_settings = [
  {
    "category": "HARM_CATEGORY_HARASSMENT",
    "threshold": "BLOCK_MEDIUM_AND_ABOVE",
  },
  {
    "category": "HARM_CATEGORY_HATE_SPEECH",
    "threshold": "BLOCK_MEDIUM_AND_ABOVE",
  },
  {
    "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
    "threshold": "BLOCK_MEDIUM_AND_ABOVE",
  },
  {
    "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
    "threshold": "BLOCK_MEDIUM_AND_ABOVE",
  },
]

model = genai.GenerativeModel(
  model_name="gemini-1.0-pro",
  safety_settings=safety_settings,
  generation_config=generation_config,
)
NO_ORDER = "Order Number not provided."

def model_api(input, prompt_type):
    return prompt_type(input)

def sentiment(input):
    chat_session = model.start_chat(
    history=[
        {
        "role": "user",
        "parts": [
            "Valid sentiments are POSITIVE, NEGATIVE, NEUTRAL, MIXED\nQuery: The shoes I ordered are small. Size is not what was in the catalog. Order number is 11223\nOutput:{\"sentiment\": \"NEGATIVE\"}",
        ],
        },
    ]
    )

    response = chat_session.send_message(f"Query: {input}\nOutput:")

    print(response.text)
    obj = json.loads(response.text)
    return obj['sentiment']

def category(input):
    chat_session = model.start_chat(
    history=[
        {
        "role": "user",
        "parts": [
            "Order categories are Electronics, Clothing, Food, Books\nQuery: The shoes I ordered are small. Size is not what was in the catalog. Order number is 11223\nOutput:{\"category\": \"Clothing\"}",
        ],
        },
    ]
    )

    response = chat_session.send_message(f"Query: {input}\nOutput:")

    print(response.text)
    obj = json.loads(response.text)
    return obj['category']

def ord_num(input):
    chat_session = model.start_chat(
    history=[
        {
        "role": "user",
        "parts": [
            "Get order number from query, if present.\nQuery: The shoes I ordered are small. Size is not what was in the catalog. Order number is 11223\nOutput:{\"order number\": \"11223\"}",
        ],
        },
    ]
    )

    response = chat_session.send_message(f"Query: {input}\nOutput:")

    print(response.text)
    obj = json.loads(response.text)
    ret_num = obj.get('order number', NO_ORDER)
    if ret_num is None:
      return NO_ORDER
    else:
      return ret_num