Spaces:
Sleeping
Sleeping
Update appStore/rag.py
Browse files- appStore/rag.py +13 -11
appStore/rag.py
CHANGED
@@ -11,17 +11,19 @@ from huggingface_hub import InferenceClient
|
|
11 |
# Get openai API key
|
12 |
hf_token = os.environ["HF_API_KEY"]
|
13 |
|
14 |
-
|
15 |
# define a special function for putting the prompt together (as we can't use haystack)
|
16 |
def get_prompt(context, label):
|
17 |
base_prompt="Summarize the following context efficiently in bullet points, the less the better - but keep concrete goals. \
|
18 |
Summarize only elements of the context that address vulnerability of "+label+" to climate change. \
|
19 |
If there is no mention of "+label+" in the context, return nothing. \
|
|
|
20 |
Formatting example: \
|
21 |
- Bullet point 1 \
|
22 |
-
- Bullet point 2
|
|
|
|
|
23 |
prompt = base_prompt+"; Context: "+context+"; Answer:"
|
24 |
-
|
25 |
return prompt
|
26 |
|
27 |
|
@@ -39,16 +41,19 @@ def run_query(context, label):
|
|
39 |
messages = [{"role": "system", "content": chatbot_role},{"role": "user", "content": get_prompt(context, label)}]
|
40 |
|
41 |
# Initialize the client, pointing it to one of the available models
|
42 |
-
client = InferenceClient("meta-llama/Meta-Llama-3-
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
-
# instantiate ChatCompletion as a generator object (stream is set to True)
|
45 |
-
# response = completion_with_backoff(model=model_select, messages=[{"role": "user", "content": get_prompt(context, label)}], stream=True)
|
46 |
# iterate through the streamed output
|
47 |
report = []
|
48 |
res_box = st.empty()
|
49 |
-
for chunk in
|
50 |
# extract the object containing the text (totally different structure when streaming)
|
51 |
-
chunk_message = chunk
|
52 |
# test to make sure there is text in the object (some don't have)
|
53 |
if 'content' in chunk_message:
|
54 |
report.append(chunk_message['content']) # extract the message
|
@@ -63,6 +68,3 @@ def run_query(context, label):
|
|
63 |
|
64 |
|
65 |
|
66 |
-
|
67 |
-
|
68 |
-
|
|
|
11 |
# Get openai API key
|
12 |
hf_token = os.environ["HF_API_KEY"]
|
13 |
|
|
|
14 |
# define a special function for putting the prompt together (as we can't use haystack)
|
15 |
def get_prompt(context, label):
|
16 |
base_prompt="Summarize the following context efficiently in bullet points, the less the better - but keep concrete goals. \
|
17 |
Summarize only elements of the context that address vulnerability of "+label+" to climate change. \
|
18 |
If there is no mention of "+label+" in the context, return nothing. \
|
19 |
+
Do not include an introduction sentence, just the bullet points as per below. \
|
20 |
Formatting example: \
|
21 |
- Bullet point 1 \
|
22 |
+
- Bullet point 2 \
|
23 |
+
"
|
24 |
+
|
25 |
prompt = base_prompt+"; Context: "+context+"; Answer:"
|
26 |
+
|
27 |
return prompt
|
28 |
|
29 |
|
|
|
41 |
messages = [{"role": "system", "content": chatbot_role},{"role": "user", "content": get_prompt(context, label)}]
|
42 |
|
43 |
# Initialize the client, pointing it to one of the available models
|
44 |
+
client = InferenceClient("meta-llama/Meta-Llama-3.1-405B-Instruct", token = hf_token)
|
45 |
+
|
46 |
+
chat_completion = client.chat.completions.create(
|
47 |
+
messages=messages,
|
48 |
+
stream=True
|
49 |
+
)
|
50 |
|
|
|
|
|
51 |
# iterate through the streamed output
|
52 |
report = []
|
53 |
res_box = st.empty()
|
54 |
+
for chunk in chat_completion:
|
55 |
# extract the object containing the text (totally different structure when streaming)
|
56 |
+
chunk_message = chunk.choices[0].delta
|
57 |
# test to make sure there is text in the object (some don't have)
|
58 |
if 'content' in chunk_message:
|
59 |
report.append(chunk_message['content']) # extract the message
|
|
|
68 |
|
69 |
|
70 |
|
|
|
|
|
|