Skip to content

Commit 431a9d2

Browse files
authored
Merge pull request #60 from morph-data/add/package-install-poetry-uv
Added sample install command of uv and poetry
2 parents aff1814 + 5519b17 commit 431a9d2

File tree

12 files changed

+312
-35
lines changed

12 files changed

+312
-35
lines changed

docs/en/advanced/llm.mdx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,19 @@ Morph offers a self-hosting feature for LLM provided by [Ollama](https://ollama.
5151

5252
<Note>
5353
You need to install the `langchain-ollama` package to use in Python.
54-
```bash Shell
55-
pip install langchain-ollama
56-
```
54+
55+
<CodeGroup>
56+
```bash pip
57+
pip install langchain-ollama
58+
```
59+
```bash poetry
60+
poetry add langchain-ollama
61+
```
62+
```bash uv
63+
uv add langchain-ollama
64+
```
65+
</CodeGroup>
66+
5767
</Note>
5868
<CodeGroup>
5969
```python Python

docs/en/develop/tutorials/chat_sidebyside.mdx

Lines changed: 94 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,21 @@ In this tutorial, you will create an application that automatically generates Py
88

99
## Prerequisites
1010

11+
Please install pageckages in advance by the following command.
12+
13+
<CodeGroup>
14+
```bash pip
15+
pip install openai plotly
16+
```
17+
```bash poetry
18+
poetry add openai plotly
19+
```
20+
```bash uv
21+
uv add openai plotly
22+
```
23+
</CodeGroup>
24+
25+
1126
Obtain the OpenAI API key in advance from the OpenAI dashboard and save it in an `.env` file.
1227

1328
```shell .env
@@ -30,16 +45,17 @@ The `text_to_plotly` function takes a user prompt and a DataFrame as arguments t
3045
Additionally, the `side-by-side` layout of the `<LLM />` component allows the HTML passed as the second argument to the `create_chunk` function to be displayed next to the chat UI, enabling real-time display.
3146

3247
```python
48+
import json
3349
import pandas as pd
3450
import os
3551
import morph
52+
from openai import OpenAI
3653
from morph import MorphGlobalContext
3754
from morph_lib.stream import create_chunk
38-
from morph_lib.ai.openai.code_generation import text_to_plotly
3955

4056
@morph.func
4157
def text_to_plotly_app(context: MorphGlobalContext):
42-
data = pd.DataFrame({
58+
df = pd.DataFrame({
4359
"city": [
4460
"Los Angeles", "San Francisco", "San Diego", "Sacramento",
4561
"Houston", "Dallas", "Austin", "San Antonio",
@@ -64,18 +80,86 @@ def text_to_plotly_app(context: MorphGlobalContext):
6480
})
6581
prompt = context.vars["prompt"]
6682

67-
result = text_to_plotly(
68-
prompt=prompt,
69-
api_key=os.getenv("OPENAI_API_KEY"),
70-
df=data,
83+
data = df.head(5).to_markdown()
84+
messages = [
85+
{
86+
"role": "system",
87+
"content": """You are a great data analyst using python.
88+
You can generate python code based on user prompt and you can use given python libraries.
89+
Please generate python code using plotly or matplotlib for visualization and pandas based on the user's question.
90+
"""},
91+
{
92+
"role": "system",
93+
"content": f"""Generate Python code for visualization using pandas, plotly and you should follow user's question.
94+
Make sure to write `import pandas as pd` at the beginning of the code otherwise the code must throw errors.
95+
96+
## Rules:
97+
1. You must generate Python code using plotly for visualization and the code should return plotly object.
98+
2. You must use the given data for visualization.
99+
3. You must use following code block and use additional packages if needed but do not change original import statements.
100+
4. Function name is `main` and it should accept a pandas DataFrame as an argument.
101+
102+
This is the template you should follow:
103+
104+
---
105+
import pandas as pd # DO NOT FORGET TO IMPORT PANDAS
106+
107+
def main(df: pd.DataFrame):
108+
# Your code here
109+
return plotly object
110+
---
111+
112+
## Importable libraries:
113+
- pandas
114+
- plotly
115+
116+
But you may have other packages installed in the environment. So you can use other packages if needed.
117+
118+
## Data:
119+
Given sample data is below. You need to use this data as data schema only.
120+
DO NOT write contents of this data to the code. The df is always passed as an argument to the main function.
121+
122+
{data}
123+
""",
124+
},
125+
{
126+
"role": "user",
127+
"content": prompt,
128+
},
129+
]
130+
code = ""
131+
132+
openai_model = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
133+
functions = {
134+
"name": "generate_python_code",
135+
"description": "Generate python code using plotly for visualization based on user prompt",
136+
"parameters": {
137+
"type": "object",
138+
"properties": {
139+
"code": {
140+
"type": "string",
141+
"description": "The python code using plotly generated based on the prompt",
142+
}
143+
},
144+
"required": ["code"],
145+
},
146+
}
147+
response = openai_model.chat.completions.create(
71148
model="gpt-4o",
149+
messages=messages,
150+
functions=[functions],
72151
)
73-
fig = result.content
152+
code = json.loads(response.choices[0].message.function_call.arguments)["code"]
153+
154+
code_ = compile(code, "<string>", "exec")
155+
namespace = {}
156+
exec(code_, namespace)
157+
func = namespace["main"]
74158

75-
yield create_chunk(f"Here is python code generated.\n")
76-
yield create_chunk(f"```python\n{result.code}\n```\n", fig.to_html())
77-
return result
159+
fig = func(df)
78160

161+
yield create_chunk(f"""Successfully, a chart was created!
162+
""", fig.to_html())
79163
```
80164
</Tab>
81165
<Tab title="2. MDX(pages)">

docs/en/develop/tutorials/chatbot.mdx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@ In this tutorial, you will create a chat app using Morph's LLM components and Op
99

1010
## Prerequisites
1111

12+
Please install pageckages in advance by the following command.
13+
14+
<CodeGroup>
15+
```bash pip
16+
pip install openai
17+
```
18+
```bash poetry
19+
poetry add langchain-ollama
20+
```
21+
```bash uv
22+
uv add langchain-ollama
23+
```
24+
</CodeGroup>
25+
1226
Obtain the OpenAI API key in advance from the OpenAI dashboard and save it in an `.env` file.
1327

1428
```shell .env

docs/en/develop/tutorials/plotly.mdx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,22 @@ icon: "chart-bar"
66
Morph allows you to customize and create dynamic dashboard applications with input forms.
77
In this tutorial, you will create an application that filters and displays Plotly charts based on values received from input forms.
88

9+
## Prerequisites
10+
11+
Please install pageckages in advance by the following command.
12+
13+
<CodeGroup>
14+
```bash pip
15+
pip install plotly
16+
```
17+
```bash poetry
18+
poetry add plotly
19+
```
20+
```bash uv
21+
uv add plotly
22+
```
23+
</CodeGroup>
24+
925
## Output
1026

1127
![plotly](/assets/images/docs/tutorial/filter_plotly.png)

docs/en/develop/tutorials/pygwalker.mdx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,17 @@ In this tutorial, we will introduce how to embed a rich dashboard created with P
1010

1111
Please install PyGWalker in advance by the following command.
1212

13-
```bash
14-
pip install pygwalker
15-
```
13+
<CodeGroup>
14+
```bash pip
15+
pip install pygwalker
16+
```
17+
```bash poetry
18+
poetry add pygwalker
19+
```
20+
```bash uv
21+
uv add pygwalker
22+
```
23+
</CodeGroup>
1624

1725
## Output
1826

docs/en/quickstart/building-app.mdx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,17 @@ As your first AI app using Morph, we will create a chat app using Langchain.
2525
</Step>
2626
<Step title="Install Packages">
2727
To use Langchain, run the following command to install the package.
28-
```bash
28+
<CodeGroup>
29+
```bash pip
2930
pip install langchain langchain-openai
3031
```
32+
```bash poetry
33+
poetry add langchain langchain-openai
34+
```
35+
```bash uv
36+
uv add langchain langchain-openai
37+
```
38+
</CodeGroup>
3139
</Step>
3240
<Step title="Create chat.py">
3341
Create a `chat.py` file in the `src/python` directory.

docs/ja/advanced/llm.mdx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,18 @@ Morphでは[Ollama](https://ollama.com "Ollama")によるLLMのセルフホス
5252

5353
<Note>
5454
Pythonで使用する場合は`langchain-ollama`パッケージをインストールする必要があります。
55-
```bash Shell
55+
<CodeGroup>
56+
```bash pip
5657
pip install langchain-ollama
5758
```
58-
</Note>
59+
```bash poetry
60+
poetry add langchain-ollama
61+
```
62+
```bash uv
63+
uv add langchain-ollama
64+
```
65+
</CodeGroup>
66+
</Note>
5967
<CodeGroup>
6068
```python Python
6169
from langchain_ollama import ChatOllama

0 commit comments

Comments
 (0)