Skip to content

Commit 240c718

Browse files
committed
LlamaIndex: This and that. Naming things.
1 parent ec8d119 commit 240c718

File tree

5 files changed

+60
-55
lines changed

5 files changed

+60
-55
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import os
2+
3+
import openai
4+
from langchain_openai import AzureOpenAIEmbeddings
5+
from langchain_openai import OpenAIEmbeddings
6+
from llama_index.core import Settings
7+
from llama_index.llms.azure_openai import AzureOpenAI
8+
from llama_index.llms.openai import OpenAI
9+
from llama_index.embeddings.langchain import LangchainEmbedding
10+
11+
12+
def configure_llm():
13+
"""
14+
Configure LLM. Use either vanilla Open AI, or Azure Open AI.
15+
"""
16+
17+
openai.api_type = os.getenv("OPENAI_API_TYPE")
18+
openai.azure_endpoint = os.getenv("OPENAI_AZURE_ENDPOINT")
19+
openai.api_version = os.getenv("OPENAI_AZURE_API_VERSION")
20+
openai.api_key = os.getenv("OPENAI_API_KEY")
21+
22+
if openai.api_type == "openai":
23+
llm = OpenAI(
24+
api_key=os.getenv("OPENAI_API_KEY"),
25+
temperature=0.0
26+
)
27+
elif openai.api_type == "azure":
28+
llm = AzureOpenAI(
29+
engine=os.getenv("LLM_INSTANCE"),
30+
azure_endpoint=os.getenv("OPENAI_AZURE_ENDPOINT"),
31+
api_key = os.getenv("OPENAI_API_KEY"),
32+
api_version = os.getenv("OPENAI_AZURE_API_VERSION"),
33+
temperature=0.0
34+
)
35+
else:
36+
raise ValueError(f"Open AI API type not defined or invalid: {openai.api_type}")
37+
38+
Settings.llm = llm
39+
if openai.api_type == "openai":
40+
Settings.embed_model = LangchainEmbedding(OpenAIEmbeddings())
41+
elif openai.api_type == "azure":
42+
Settings.embed_model = LangchainEmbedding(
43+
AzureOpenAIEmbeddings(
44+
azure_endpoint=os.getenv("OPENAI_AZURE_ENDPOINT"),
45+
model=os.getenv("EMBEDDING_MODEL_INSTANCE")
46+
)
47+
)

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

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,56 +4,14 @@
44
"""
55

66
import os
7-
import openai
87
import sqlalchemy as sa
98

109
from dotenv import load_dotenv
11-
from langchain_openai import AzureOpenAIEmbeddings
12-
from langchain_openai import OpenAIEmbeddings
13-
from llama_index.llms.azure_openai import AzureOpenAI
14-
from llama_index.llms.openai import OpenAI
15-
from llama_index.embeddings.langchain import LangchainEmbedding
1610
from llama_index.core.utilities.sql_wrapper import SQLDatabase
1711
from llama_index.core.query_engine import NLSQLTableQueryEngine
1812
from llama_index.core import Settings
1913

20-
21-
def configure_llm():
22-
"""
23-
Configure LLM. Use either vanilla Open AI, or Azure Open AI.
24-
"""
25-
26-
openai.api_type = os.getenv("OPENAI_API_TYPE")
27-
openai.azure_endpoint = os.getenv("OPENAI_AZURE_ENDPOINT")
28-
openai.api_version = os.getenv("OPENAI_AZURE_API_VERSION")
29-
openai.api_key = os.getenv("OPENAI_API_KEY")
30-
31-
if openai.api_type == "openai":
32-
llm = OpenAI(
33-
api_key=os.getenv("OPENAI_API_KEY"),
34-
temperature=0.0
35-
)
36-
elif openai.api_type == "azure":
37-
llm = AzureOpenAI(
38-
engine=os.getenv("LLM_INSTANCE"),
39-
azure_endpoint=os.getenv("OPENAI_AZURE_ENDPOINT"),
40-
api_key = os.getenv("OPENAI_API_KEY"),
41-
api_version = os.getenv("OPENAI_AZURE_API_VERSION"),
42-
temperature=0.0
43-
)
44-
else:
45-
raise ValueError(f"Open AI API type not defined or invalid: {openai.api_type}")
46-
47-
Settings.llm = llm
48-
if openai.api_type == "openai":
49-
Settings.embed_model = LangchainEmbedding(OpenAIEmbeddings())
50-
elif openai.api_type == "azure":
51-
Settings.embed_model = LangchainEmbedding(
52-
AzureOpenAIEmbeddings(
53-
azure_endpoint=os.getenv("OPENAI_AZURE_ENDPOINT"),
54-
model=os.getenv("EMBEDDING_MODEL_INSTANCE")
55-
)
56-
)
14+
from boot import configure_llm
5715

5816

5917
def 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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ 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
"""
2727
Execute `demo_nlsql.py` and verify outcome.
2828
"""

0 commit comments

Comments
 (0)