ahuang11 commited on
Commit
920a9a0
1 Parent(s): 12e8d67

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +116 -59
app.py CHANGED
@@ -1,68 +1,125 @@
1
- from context import query
2
- from openai import AsyncOpenAI
3
  import panel as pn
 
 
 
4
 
5
- # taken from fleet context
6
- SYSTEM_PROMPT = """
7
- You are an expert in Python libraries. You carefully provide accurate, factual, thoughtful, nuanced answers, and are brilliant at reasoning. If you think there might not be a correct answer, you say so.
8
- Each token you produce is another opportunity to use computation, therefore you always spend a few sentences explaining background context, assumptions, and step-by-step thinking BEFORE you try to answer a question.
9
- Your users are experts in AI and ethics, so they already know you're a language model and your capabilities and limitations, so don't remind them of that. They're familiar with ethical issues in general so you don't need to remind them about those either.
10
- Your users are also in a CLI environment. You are capable of writing and running code. DO NOT write hypothetical code. ALWAYS write real code that will execute and run end-to-end.
11
-
12
- Instructions:
13
- - Be objective, direct. Include literal information from the context, don't add any conclusion or subjective information.
14
- - When writing code, ALWAYS have some sort of output (like a print statement). If you're writing a function, call it at the end. Do not generate the output, because the user can run it themselves.
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
- MODEL = "gpt-3.5-turbo"
33
-
34
-
35
- async def answer(contents, user, instance):
36
- # start with system prompt
37
- messages = [{"role": "system", "content": SYSTEM_PROMPT}]
38
-
39
- # add context to the user input
40
- context = ""
41
- fleet_responses = query(contents, k=3)
42
- for i, response in enumerate(fleet_responses):
43
- context += f"\n\n**Context {i}**: {response['metadata']['url']}\n{response['metadata']['text']}"
44
- instance.send(context, avatar="🛩️", user="Fleet Context", respond=False)
45
-
46
- # get history of messages (skipping the intro message)
47
- # and serialize fleet context messages as "user" role
48
- messages.extend(
49
- instance.serialize(role_names={"user": ["user", "Fleet Context"]})[1:]
50
- )
51
 
52
- openai_response = await client.chat.completions.create(
53
- model=MODEL, messages=messages, temperature=0.2, stream=True
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  )
 
55
 
 
56
  message = ""
57
- async for chunk in openai_response:
58
- token = chunk.choices[0].delta.content
59
- if token:
60
- message += token
61
  yield message
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
- client = AsyncOpenAI()
65
- intro_message = pn.chat.ChatMessage("Ask me anything about Python libraries!", user="System")
66
- chat_interface = pn.chat.ChatInterface(intro_message, callback=answer, callback_user="OpenAI")
67
- template = pn.template.FastListTemplate(main=[chat_interface], title="Panel UI of Fleet Context 🛩️")
68
- template.servable()
 
 
 
 
 
 
 
1
+ import re
2
+ import os
3
  import panel as pn
4
+ from mistralai.async_client import MistralAsyncClient
5
+ from mistralai.models.chat_completion import ChatMessage
6
+ from panel.io.mime_render import exec_with_return
7
 
8
+ pn.extension("codeeditor", sizing_mode="stretch_width")
9
+
10
+ LLM_MODEL = "mistral-medium"
11
+ SYSTEM_MESSAGE = ChatMessage(
12
+ role="system",
13
+ content=(
14
+ "You are a renowned data visualization expert "
15
+ "with a strong background in matplotlib. "
16
+ "Your primary goal is to assist the user "
17
+ "in edit the code based on user request "
18
+ "using best practices. Simply provide code "
19
+ "in code fences (```). You must have `fig` "
20
+ "as the last line of code"
21
+ ),
22
+ )
23
+
24
+ USER_CONTENT_FORMAT = """
25
+ Request:
26
+ {content}
27
+
28
+ Code:
29
+ ```python
30
+ {code}
31
+ ```
32
+ """.strip()
33
+
34
+ DEFAULT_MATPLOTLIB = """
35
+ import numpy as np
36
+ import matplotlib.pyplot as plt
37
+
38
+ fig = plt.figure()
39
+ ax = plt.axes(title="Plot Title", xlabel="X Label", ylabel="Y Label")
40
+
41
+ x = np.linspace(1, 10)
42
+ y = np.sin(x)
43
+ z = np.cos(x)
44
+ c = np.log(x)
45
+
46
+ ax.plot(x, y, c="blue", label="sin")
47
+ ax.plot(x, z, c="orange", label="cos")
 
 
 
 
 
 
48
 
49
+ img = ax.scatter(x, c, c=c, label="log")
50
+ plt.colorbar(img, label="Colorbar")
51
+ plt.legend()
52
+
53
+ # must have fig at the end!
54
+ fig
55
+ """.strip()
56
+
57
+
58
+ async def callback(content: str, user: str, instance: pn.chat.ChatInterface):
59
+ # system
60
+ messages = [SYSTEM_MESSAGE]
61
+
62
+ # history
63
+ messages.extend([ChatMessage(**message) for message in instance.serialize()[1:-1]])
64
+
65
+ # new user contents
66
+ user_content = USER_CONTENT_FORMAT.format(
67
+ content=content, code=code_editor.value
68
  )
69
+ messages.append(ChatMessage(role="user", content=user_content))
70
 
71
+ # stream LLM tokens
72
  message = ""
73
+ async for chunk in client.chat_stream(model=LLM_MODEL, messages=messages):
74
+ if chunk.choices[0].delta.content is not None:
75
+ message += chunk.choices[0].delta.content
 
76
  yield message
77
 
78
+ # extract code
79
+ llm_code = re.findall(r"```python\n(.*)\n```", message, re.DOTALL)[0]
80
+ if llm_code.splitlines()[-1].strip() != "fig":
81
+ llm_code += "\nfig"
82
+ code_editor.value = llm_code
83
+ matplotlib_pane.object = exec_with_return(llm_code)
84
+
85
+
86
+ # instantiate widgets and panes
87
+ client = MistralAsyncClient(api_key=os.environ["MISTRAL_API_KEY"])
88
+ chat_interface = pn.chat.ChatInterface(
89
+ callback=callback,
90
+ show_clear=False,
91
+ show_undo=False,
92
+ show_button_name=False,
93
+ message_params=dict(
94
+ show_reaction_icons=False,
95
+ show_copy_icon=False,
96
+ ),
97
+ height=700,
98
+ callback_exception="verbose",
99
+ )
100
+ matplotlib_pane = pn.pane.Matplotlib(
101
+ exec_with_return(DEFAULT_MATPLOTLIB),
102
+ sizing_mode="stretch_both",
103
+ )
104
+ code_editor = pn.widgets.CodeEditor(
105
+ value=DEFAULT_MATPLOTLIB,
106
+ sizing_mode="stretch_both",
107
+ )
108
+
109
+ # lay them out
110
+ tabs = pn.Tabs(
111
+ ("Plot", matplotlib_pane),
112
+ ("Code", code_editor),
113
+ )
114
 
115
+ sidebar = [chat_interface]
116
+ main = [tabs]
117
+ template = pn.template.FastListTemplate(
118
+ sidebar=sidebar,
119
+ main=main,
120
+ sidebar_width=600,
121
+ main_layout=None,
122
+ accent_base_color="#fd7000",
123
+ header_background="#fd7000",
124
+ )
125
+ template.servable()