Skip to content

Commit 4079e85

Browse files
authored
Merge pull request #5 from pamelafox/llamaindex
Add llamaindex example
2 parents 1410291 + 7b97b1e commit 4079e85

File tree

6 files changed

+48
-8
lines changed

6 files changed

+48
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ If not, follow these steps:
2525
2. Install the required packages:
2626

2727
```bash
28-
pip install -r requirements.txt
28+
python -m pip install -r requirements.txt
2929
```
3030

3131
## Configuring the OpenAI environment variables

chat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
MODEL_NAME = os.getenv("AZURE_OPENAI_DEPLOYMENT")
2121
elif API_HOST == "ollama":
2222
client = openai.OpenAI(
23-
base_url="http://localhost:11434/v1",
23+
base_url=os.getenv("OLLAMA_ENDPOINT"),
2424
api_key="nokeyneeded",
2525
)
2626
MODEL_NAME = os.getenv("OLLAMA_MODEL")

chat_llamaindex.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import os
2+
3+
import azure.identity
4+
from dotenv import load_dotenv
5+
from llama_index.core.llms import ChatMessage, MessageRole
6+
from llama_index.llms.azure_openai import AzureOpenAI
7+
from llama_index.llms.openai import OpenAI
8+
from llama_index.llms.openai_like import OpenAILike
9+
10+
# Setup the OpenAI client to use either Azure, OpenAI.com, or Ollama API
11+
load_dotenv(override=True)
12+
API_HOST = os.getenv("API_HOST")
13+
14+
if API_HOST == "azure":
15+
token_provider = azure.identity.get_bearer_token_provider(
16+
azure.identity.DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
17+
)
18+
llm = AzureOpenAI(
19+
model=os.getenv("OPENAI_MODEL"),
20+
deployment_name=os.getenv("AZURE_OPENAI_DEPLOYMENT"),
21+
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
22+
api_version=os.getenv("AZURE_OPENAI_VERSION"),
23+
use_azure_ad=True,
24+
azure_ad_token_provider=token_provider,
25+
)
26+
elif API_HOST == "ollama":
27+
llm = OpenAILike(
28+
model=os.getenv("OLLAMA_MODEL"), api_base=os.getenv("OLLAMA_ENDPOINT"), api_key="fake", is_chat_model=True
29+
)
30+
else:
31+
llm = OpenAI(model=os.getenv("OPENAI_MODEL"), api_key=os.getenv("OPENAI_KEY"))
32+
33+
chat_msgs = [
34+
ChatMessage(
35+
role=MessageRole.SYSTEM,
36+
content=("You are a helpful assistant that makes lots of cat references and uses emojis."),
37+
),
38+
ChatMessage(role=MessageRole.USER, content="Write a haiku about a hungry cat who wants tuna"),
39+
]
40+
response = llm.chat(chat_msgs)
41+
print(f"Response from {API_HOST}: \n")
42+
print(str(response))

chat_stream.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
MODEL_NAME = os.getenv("AZURE_OPENAI_DEPLOYMENT")
2121
elif API_HOST == "ollama":
2222
client = openai.OpenAI(
23-
base_url="http://localhost:11434/v1",
23+
base_url=os.getenv("OLLAMA_ENDPOINT"),
2424
api_key="nokeyneeded",
2525
)
2626
MODEL_NAME = os.getenv("OLLAMA_MODEL")

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ openai
33
python-dotenv
44
langchain
55
langchain-openai
6+
llama-index-llms-azure-openai
7+
llama-index-llms-openai
8+
llama-index-llms-openai-like

tests/main_test.py

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)