Update app.py
Browse files
app.py
CHANGED
@@ -1,68 +1,74 @@
|
|
1 |
-
from
|
2 |
-
from openai import AsyncOpenAI
|
3 |
import panel as pn
|
|
|
|
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
|
|
|
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
- ALWAYS cite your sources. Context will be given to you after the text ### Context source_url ### with source_url being the url to the file. For example, ### Context https://example.com/docs/api.html#files ### will have a source_url of https://example.com/docs/api.html#files.
|
16 |
-
- When you cite your source, please cite it as [num] with `num` starting at 1 and incrementing with each source cited (1, 2, 3, ...). At the bottom, have a newline-separated `num: source_url` at the end of the response. ALWAYS add a new line between sources or else the user won't be able to read it. DO NOT convert links into markdown, EVER! If you do that, the user will not be able to click on the links.
|
17 |
-
For example:
|
18 |
-
**Context 1**: https://example.com/docs/api.html#pdfs
|
19 |
-
I'm a big fan of PDFs.
|
20 |
-
**Context 2**: https://example.com/docs/api.html#csvs
|
21 |
-
I'm a big fan of CSVs.
|
22 |
-
### Prompt ###
|
23 |
-
What is this person a big fan of?
|
24 |
-
### Response ###
|
25 |
-
This person is a big fan of PDFs[1] and CSVs[2].
|
26 |
-
1: https://example.com/docs/api.html#pdfs
|
27 |
-
2: https://example.com/docs/api.html#csvs
|
28 |
"""
|
29 |
|
30 |
-
pn.extension()
|
31 |
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
messages.extend(
|
49 |
-
instance.serialize(role_names={"user": ["user", "Fleet Context"]})[1:]
|
50 |
)
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
54 |
)
|
55 |
-
|
56 |
message = ""
|
57 |
-
|
58 |
-
|
59 |
-
if
|
60 |
-
message +=
|
61 |
yield message
|
62 |
|
63 |
|
64 |
-
client =
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from openai import OpenAI
|
|
|
2 |
import panel as pn
|
3 |
+
import requests
|
4 |
+
import os
|
5 |
|
6 |
+
pn.extension()
|
7 |
+
|
8 |
+
# format this URL with query and number of rows
|
9 |
+
API_URL = "https://api.crossref.org/works"
|
10 |
+
# API_KEY = os.environ["ELSEVIER_API_KEY"]
|
11 |
+
# URL_FMT = "https://api.elsevier.com/content/search/scopus"
|
12 |
+
DEFAULT_PROMPT_TEMPLATE = """
|
13 |
+
Here are the papers related to {query}
|
14 |
|
15 |
+
Help me summarize these into bullet points, readable within 2 minutes.
|
16 |
+
|
17 |
+
{items}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
"""
|
19 |
|
|
|
20 |
|
21 |
+
def get_relevant_papers(query, rows):
|
22 |
+
params = {
|
23 |
+
"query.bibliographic": query,
|
24 |
+
"rows": rows,
|
25 |
+
}
|
26 |
+
response = requests.get(API_URL, params=params)
|
27 |
+
output = response.json()
|
28 |
+
return output
|
29 |
|
30 |
|
31 |
+
def process_inputs(contents, user, instance):
|
32 |
+
output = get_relevant_papers(contents, rows_input.value)
|
33 |
+
instance.send(pn.pane.JSON(output), respond=False, user="Sources")
|
34 |
|
35 |
+
items = []
|
36 |
+
for item in output["message"]["items"]:
|
37 |
+
abstract = item.get("abstract", "")
|
38 |
+
title = item["title"]
|
39 |
+
url = item["URL"]
|
40 |
+
items.append(f"{title}({url}): {abstract}")
|
41 |
|
42 |
+
prompt = prompt_template_input.value.format(
|
43 |
+
query=contents, items=items
|
|
|
|
|
44 |
)
|
45 |
+
response = client.chat.completions.create(
|
46 |
+
model="gpt-3.5-turbo",
|
47 |
+
messages=[{"role": "user", "content": prompt}],
|
48 |
+
stream=True,
|
49 |
)
|
|
|
50 |
message = ""
|
51 |
+
for chunk in response:
|
52 |
+
part = chunk.choices[0].delta.content
|
53 |
+
if part is not None:
|
54 |
+
message += part
|
55 |
yield message
|
56 |
|
57 |
|
58 |
+
client = OpenAI()
|
59 |
+
|
60 |
+
# define widgets
|
61 |
+
prompt_template_input = pn.widgets.TextAreaInput(
|
62 |
+
value=DEFAULT_PROMPT_TEMPLATE.strip(), height=500
|
63 |
+
)
|
64 |
+
rows_input = pn.widgets.IntInput(name="Number of rows", value=2)
|
65 |
+
chat_interface = pn.chat.ChatInterface(callback=process_inputs, callback_exception="verbose")
|
66 |
+
|
67 |
+
# layout
|
68 |
+
sidebar = pn.Column(prompt_template_input, rows_input)
|
69 |
+
main = pn.Column(chat_interface)
|
70 |
+
pn.template.FastListTemplate(
|
71 |
+
sidebar=[sidebar],
|
72 |
+
main=[main],
|
73 |
+
title="Elsevier Summarizer",
|
74 |
+
).show()
|