Commit
·
d3a8b99
1
Parent(s):
54d5191
test
Browse files- app.py +171 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,171 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
from math import exp
|
4 |
+
from tenacity import retry, wait_random_exponential, stop_after_attempt
|
5 |
+
from langchain_core.messages import HumanMessage, SystemMessage
|
6 |
+
import tiktoken
|
7 |
+
from langchain_openai import ChatOpenAI
|
8 |
+
|
9 |
+
class OpenAIChat(ChatOpenAI):
|
10 |
+
"""Openai chatbot LLM."""
|
11 |
+
def __init__(
|
12 |
+
self,
|
13 |
+
model_name: str = "gpt-4o",
|
14 |
+
temperature: float = 1,
|
15 |
+
top_p: float = 1,
|
16 |
+
presence_penalty: float = 0,
|
17 |
+
frequency_penalty: float = 0,
|
18 |
+
max_tokens: int = 2000,
|
19 |
+
cache: bool = False,
|
20 |
+
seed: int = 42
|
21 |
+
):
|
22 |
+
"""Initialize azure chat LLM."""
|
23 |
+
super(ChatOpenAI, self).__init__(
|
24 |
+
model_name=model_name,
|
25 |
+
openai_api_key=os.getenv("openai_api"),
|
26 |
+
temperature=temperature,
|
27 |
+
|
28 |
+
max_tokens=max_tokens,
|
29 |
+
model_kwargs={"presence_penalty": presence_penalty,
|
30 |
+
"frequency_penalty": frequency_penalty,
|
31 |
+
"logprobs": True,
|
32 |
+
"seed": seed,
|
33 |
+
"top_p": top_p,
|
34 |
+
},
|
35 |
+
cache=cache
|
36 |
+
)
|
37 |
+
|
38 |
+
|
39 |
+
|
40 |
+
model_name = "gpt-4o"
|
41 |
+
tokenizer = tiktoken.encoding_for_model("gpt-4o")
|
42 |
+
tokens = ["Yes", "No"]
|
43 |
+
ids = [tokenizer.encode(token)[0] for token in tokens]
|
44 |
+
model = OpenAIChat(model_name=model_name).bind(logit_bias=dict((id, 1) for id in ids))
|
45 |
+
|
46 |
+
system_prompt = """
|
47 |
+
You are an assistant to decide if a statement is as the similar format of the example statements.
|
48 |
+
In general, a statement has complete information, its positive,
|
49 |
+
If the statement is ambigious, often seems like a follow up, or need more backgroud or context to understand, its negative.
|
50 |
+
If a statement is positive, reply Yes, otherwise reply No. Do not reply the reasoning, just Yes or No.
|
51 |
+
|
52 |
+
Here are some positive and negative examples:
|
53 |
+
|
54 |
+
[S]: Top ten trucks with best fuel mileage.
|
55 |
+
[A]: Yes
|
56 |
+
|
57 |
+
[S]: Which battery electric vehicle travelled the most distance this year?
|
58 |
+
[A]: Yes
|
59 |
+
|
60 |
+
[S]: Show me a list of all customer visits from yesterday.
|
61 |
+
[A]: Yes
|
62 |
+
|
63 |
+
[S]: What is the average fuel usage per 100km?
|
64 |
+
[A]: Yes
|
65 |
+
|
66 |
+
[S]: Best month with the lowest idle.
|
67 |
+
[A]: Yes
|
68 |
+
|
69 |
+
[S]: Best month with the lowest idle.
|
70 |
+
[A]: Yes
|
71 |
+
|
72 |
+
[S]: What was the idle each month this year?
|
73 |
+
[A]: Yes
|
74 |
+
|
75 |
+
|
76 |
+
[S]: How about the TwinTaxi?
|
77 |
+
[A]: No
|
78 |
+
This one is No since it doesn't specify what thing about the TwinTaxi
|
79 |
+
|
80 |
+
[S]: Group services.
|
81 |
+
[A]: No
|
82 |
+
This one is No since it doesn't specify thing about Group services.
|
83 |
+
|
84 |
+
[S]: Last week.
|
85 |
+
[A]: No
|
86 |
+
This one is No since it doesn't specify last week for what?
|
87 |
+
|
88 |
+
[S]: Yes, it is.
|
89 |
+
[A]: No
|
90 |
+
This one is No since it doesn't specify what thing is confirmed.
|
91 |
+
|
92 |
+
[S]: And the mileage?
|
93 |
+
[A]: No
|
94 |
+
This one is No since it doesn't specify mileage for what.
|
95 |
+
|
96 |
+
[S]: Details, please.
|
97 |
+
[A]: No
|
98 |
+
This one is No since it doesn't specify details for what.
|
99 |
+
"""
|
100 |
+
|
101 |
+
prompt = """
|
102 |
+
Now Answer this statament:
|
103 |
+
[S]: {statement}
|
104 |
+
[A]:
|
105 |
+
"""
|
106 |
+
|
107 |
+
|
108 |
+
@retry(wait=wait_random_exponential(min=1, max=40), stop=stop_after_attempt(3))
|
109 |
+
def statement_valid(statement):
|
110 |
+
user_prompt = prompt.format(statement=statement)
|
111 |
+
messages = [
|
112 |
+
SystemMessage(
|
113 |
+
content=system_prompt
|
114 |
+
),
|
115 |
+
HumanMessage(
|
116 |
+
content=user_prompt
|
117 |
+
),
|
118 |
+
]
|
119 |
+
response = model.invoke(messages)
|
120 |
+
return (
|
121 |
+
statement,
|
122 |
+
response.content.strip(),
|
123 |
+
response.response_metadata['logprobs']['content'][0]['logprob']
|
124 |
+
)
|
125 |
+
|
126 |
+
|
127 |
+
def make_pred(statement: str) -> str:
|
128 |
+
_, prediction, logprob = statement_valid(statement)
|
129 |
+
probability = exp(logprob)
|
130 |
+
yes_probability = probability * -1 + 1 if prediction == "No" else probability
|
131 |
+
if yes_probability > 0.5:
|
132 |
+
return "No need to rephrase, send in the original question."
|
133 |
+
return "Need to be rephrased with chat hist"
|
134 |
+
|
135 |
+
|
136 |
+
def get_question_examples():
|
137 |
+
examples=[
|
138 |
+
"And the mileage?",
|
139 |
+
"I mean last week.",
|
140 |
+
"How about the B123?",
|
141 |
+
"No need rephrases examples: ",
|
142 |
+
"Top ten trucks with best fuel mileage.",
|
143 |
+
"What trucks in motion have no driver assigned?",
|
144 |
+
"What group is vehicle Blueberry in?"
|
145 |
+
]
|
146 |
+
return examples
|
147 |
+
|
148 |
+
|
149 |
+
with gr.Blocks() as demo:
|
150 |
+
with gr.Row(equal_height=False):
|
151 |
+
input_statement = gr.Textbox(placeholder="type your statement", label="Prompt")
|
152 |
+
reponse = gr.Textbox(placeholder=None, label="AI Response", lines=5)
|
153 |
+
gr.Examples(
|
154 |
+
examples=get_question_examples(),
|
155 |
+
inputs=[input_statement],
|
156 |
+
fn=None,
|
157 |
+
outputs=None,
|
158 |
+
cache_examples=False,
|
159 |
+
label="Question Examples"
|
160 |
+
)
|
161 |
+
|
162 |
+
btn = gr.Button("run", variant="primary")
|
163 |
+
|
164 |
+
btn.click(
|
165 |
+
fn=make_pred,
|
166 |
+
inputs=[
|
167 |
+
input_statement,
|
168 |
+
],
|
169 |
+
outputs=[reponse]
|
170 |
+
)
|
171 |
+
demo.launch(share=False)
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
langchain==0.1.11
|
2 |
+
langchain-openai
|
3 |
+
langchain-community
|
4 |
+
openai==1.13.3
|