Skip to content

Commit a807a14

Browse files
committed
LlamaIndex: This and that. Naming things.
1 parent 30e6ec6 commit a807a14

File tree

6 files changed

+67
-62
lines changed

6 files changed

+67
-62
lines changed

topic/machine-learning/llama-index/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ Save your changes.
9292
Run the code like so:
9393

9494
```bash
95-
python main.py
95+
python demo_nlsql.py
9696
```
9797

9898
Here's the expected output:
Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
1-
"""
2-
Use an LLM to query a database in human language.
3-
Example code using LlamaIndex with vanilla Open AI and Azure Open AI.
4-
"""
5-
61
import os
7-
import openai
8-
import sqlalchemy as sa
92

10-
from dotenv import load_dotenv
3+
import openai
114
from langchain_openai import AzureOpenAIEmbeddings
125
from langchain_openai import OpenAIEmbeddings
6+
from llama_index.core import Settings
137
from llama_index.llms.azure_openai import AzureOpenAI
148
from llama_index.llms.openai import OpenAI
159
from llama_index.embeddings.langchain import LangchainEmbedding
16-
from llama_index.core.utilities.sql_wrapper import SQLDatabase
17-
from llama_index.core.query_engine import NLSQLTableQueryEngine
18-
from llama_index.core import Settings
1910

2011

2112
def configure_llm():
@@ -54,39 +45,3 @@ def configure_llm():
5445
model=os.getenv("EMBEDDING_MODEL_INSTANCE")
5546
)
5647
)
57-
58-
59-
def main():
60-
"""
61-
Use an LLM to query a database in human language.
62-
"""
63-
64-
# Configure application.
65-
load_dotenv()
66-
configure_llm()
67-
68-
# Configure database connection and query engine.
69-
print("Connecting to CrateDB")
70-
engine_crate = sa.create_engine(os.getenv("CRATEDB_SQLALCHEMY_URL"))
71-
engine_crate.connect()
72-
73-
print("Creating LlamaIndex QueryEngine")
74-
sql_database = SQLDatabase(engine_crate, include_tables=[os.getenv("CRATEDB_TABLE_NAME")])
75-
query_engine = NLSQLTableQueryEngine(
76-
sql_database=sql_database,
77-
tables=[os.getenv("CRATEDB_TABLE_NAME")],
78-
llm=Settings.llm
79-
)
80-
81-
# Invoke an inquiry.
82-
print("Running query")
83-
QUERY_STR = "What is the average value for sensor 1?"
84-
answer = query_engine.query(QUERY_STR)
85-
print(answer.get_formatted_sources())
86-
print("Query was:", QUERY_STR)
87-
print("Answer was:", answer)
88-
print(answer.metadata)
89-
90-
91-
if __name__ == "__main__":
92-
main()
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""
2+
Use an LLM to query a database in human language.
3+
Example code using LlamaIndex with vanilla Open AI and Azure Open AI.
4+
"""
5+
6+
import os
7+
import sqlalchemy as sa
8+
9+
from dotenv import load_dotenv
10+
from llama_index.core.utilities.sql_wrapper import SQLDatabase
11+
from llama_index.core.query_engine import NLSQLTableQueryEngine
12+
from llama_index.core import Settings
13+
14+
from boot import configure_llm
15+
16+
17+
def main():
18+
"""
19+
Use an LLM to query a database in human language.
20+
"""
21+
22+
# Configure application.
23+
load_dotenv()
24+
configure_llm()
25+
26+
# Configure database connection and query engine.
27+
print("Connecting to CrateDB")
28+
engine_crate = sa.create_engine(os.getenv("CRATEDB_SQLALCHEMY_URL"))
29+
engine_crate.connect()
30+
31+
print("Creating LlamaIndex QueryEngine")
32+
sql_database = SQLDatabase(engine_crate, include_tables=[os.getenv("CRATEDB_TABLE_NAME")])
33+
query_engine = NLSQLTableQueryEngine(
34+
sql_database=sql_database,
35+
tables=[os.getenv("CRATEDB_TABLE_NAME")],
36+
llm=Settings.llm
37+
)
38+
39+
# Invoke an inquiry.
40+
print("Running query")
41+
QUERY_STR = "What is the average value for sensor 1?"
42+
answer = query_engine.query(QUERY_STR)
43+
print(answer.get_formatted_sources())
44+
print("Query was:", QUERY_STR)
45+
print("Answer was:", answer)
46+
print(answer.metadata)
47+
48+
49+
if __name__ == "__main__":
50+
main()
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
OPENAI_API_KEY=TODO
2-
OPENAI_API_TYPE=azure
3-
OPENAI_AZURE_ENDPOINT=https://TODO.openai.azure.com
4-
OPENAI_AZURE_API_VERSION=2024-08-01-preview
5-
LLM_INSTANCE=TODO
6-
EMBEDDING_MODEL_INSTANCE=TODO
7-
CRATEDB_SQLALCHEMY_URL="crate://USER:PASSWORD@HOST:4200/?ssl=true"
8-
CRATEDB_TABLE_NAME=time_series_data
1+
export OPENAI_API_KEY=TODO
2+
export OPENAI_API_TYPE=azure
3+
export OPENAI_AZURE_ENDPOINT=https://TODO.openai.azure.com
4+
export OPENAI_AZURE_API_VERSION=2024-08-01-preview
5+
export LLM_INSTANCE=TODO
6+
export EMBEDDING_MODEL_INSTANCE=TODO
7+
export CRATEDB_SQLALCHEMY_URL="crate://USER:PASSWORD@HOST:4200/?ssl=true"
8+
export CRATEDB_TABLE_NAME=time_series_data
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# OPENAI_API_KEY=sk-XJZ7pfog5Gp8Kus8D--invalid--0CJ5lyAKSefZLaV1Y9S1
2-
OPENAI_API_TYPE=openai
3-
CRATEDB_SQLALCHEMY_URL="crate://crate@localhost:4200/"
4-
CRATEDB_TABLE_NAME=time_series_data
2+
export OPENAI_API_TYPE=openai
3+
export CRATEDB_SQLALCHEMY_URL="crate://crate@localhost:4200/"
4+
export CRATEDB_TABLE_NAME=time_series_data

topic/machine-learning/llama-index/test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ def init_database(cratedb):
2222
cratedb.run_sql((HERE / "init.sql").read_text())
2323

2424

25-
def test_main(cratedb, capsys):
25+
def test_nlsql(cratedb, capsys):
2626
"""
27-
Execute `main.py` and verify outcome.
27+
Execute `demo_nlsql.py` and verify outcome.
2828
"""
2929

3030
# Load the standalone configuration also for software testing.
3131
# On CI, `OPENAI_API_KEY` will need to be supplied externally.
3232
load_dotenv("env.standalone")
3333

3434
# Invoke the workload, in-process.
35-
from main import main
35+
from demo_nlsql import main
3636
main()
3737

3838
# Verify the outcome.

0 commit comments

Comments
 (0)