Skip to content

Commit 7a2029f

Browse files
authored
feat: ✨ Learning path ai chatbot demos (#99)
* conf: ⬆️ LangChain version * fix: 🐛 RAG with simple files * doc: 📝 Add some documentations * feat: 🧠 Create Python memory use case * feat: 🔢 Add steps for learning path demo * clean: 🗑️ Remove unused import * clean: 🗑️ Remove code * fix: 🐛 reorder demo * feat: ✨ update after learning path
1 parent 9a43f57 commit 7a2029f

File tree

10 files changed

+212
-194
lines changed

10 files changed

+212
-194
lines changed
Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
This project illustrate how to use[LangChain](https://python.langchain.com/v0.2/) Python Framework with [AI Endpoints](https://endpoints.ai.cloud.ovh.net/).
22

3+
### 🧰 Pre requisites 🧰
4+
5+
- Python 3.12.x installed
6+
- AI Endpoints API token
7+
- model to use: any of the LLM instruct models
8+
- have the following environment variables created:
9+
- OVH_AI_ENDPOINTS_ACCESS_TOKEN: the API token, see [documentation](https://help.ovhcloud.com/csm/en-gb-public-cloud-ai-endpoints-getting-started?id=kb_article_view&sysparm_article=KB0065401#generating-your-first-api-access-key) to know how to generate it
10+
- OVH_AI_ENDPOINTS_MODEL_URL: URL of the model, see [AI Endpoints website](https://endpoints.ai.cloud.ovh.net/) to know how to get it.
11+
- OVH_AI_ENDPOINTS_MODEL_NAME: model name, see [AI Endpoints website](https://endpoints.ai.cloud.ovh.net/) to know how to get it.
12+
- OVH_AI_ENDPOINTS_EMBEDDING_MODEL_NAME: embedding model name, see [AI Endpoints website](https://endpoints.ai.cloud.ovh.net/) to know how to get it.
13+
314
## How to use the project
415

5-
- set the `OVH_AI_ENDPOINTS_ACCESS_TOKEN` environment variable with your API token (see https://endpoints.ai.cloud.ovh.net/)
6-
- install lib magic: `brew install libmagic`
7-
- install the required dependencies: `pip3 install -r requirements.txt`
8-
- to use the blocking chatbot: `python3 chat-bot.py --question "What is OVHcloud?"`
9-
- to use the streaming chatbot: `python3 chat-bot-streaming.py --question "What is OVHcloud?"`
10-
- to use the RAG chatbot: `python3 chat-bot-streaming-rag.py --question "What is AI Endpoints?"`
16+
- install the required dependencies: `pip install -r requirements.txt`
17+
- to use the blocking chatbot: `python chatbot.py`
18+
- to use the memory chatbot: `python chatbot-memory.py`
19+
- to use the streaming chatbot: `python chatbot-streaming.py`
20+
- to use the RAG chatbot: `python chatbot-streaming-rag.py`

ai/ai-endpoints/python-langchain-chatbot/chat-bot-streaming-rag.py

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

ai/ai-endpoints/python-langchain-chatbot/chat-bot-streaming.py

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

ai/ai-endpoints/python-langchain-chatbot/chat-bot.py

Lines changed: 0 additions & 42 deletions
This file was deleted.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import os
2+
3+
from langchain_mistralai import ChatMistralAI
4+
from langchain.chains import ConversationChain
5+
from langchain.memory import ConversationBufferWindowMemory
6+
from langchain_core.prompts import ChatPromptTemplate
7+
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
8+
9+
# Load environment variables: see https://endpoints.ai.cloud.ovh.net/ for more information
10+
_OVH_AI_ENDPOINTS_ACCESS_TOKEN = os.environ.get('OVH_AI_ENDPOINTS_ACCESS_TOKEN')
11+
_OVH_AI_ENDPOINTS_MODEL_NAME = os.environ.get('OVH_AI_ENDPOINTS_MODEL_NAME')
12+
_OVH_AI_ENDPOINTS_MODEL_URL = os.environ.get('OVH_AI_ENDPOINTS_MODEL_URL')
13+
14+
# Callback handler for streaming output
15+
streaming_handler = StreamingStdOutCallbackHandler()
16+
17+
# Configure the used model: Mistral
18+
model = ChatMistralAI(model=_OVH_AI_ENDPOINTS_MODEL_NAME,
19+
api_key=_OVH_AI_ENDPOINTS_ACCESS_TOKEN,
20+
endpoint=_OVH_AI_ENDPOINTS_MODEL_URL,
21+
max_tokens=1500,
22+
streaming=True,
23+
callbacks=[streaming_handler])
24+
25+
# Define the prompt template for the chatbot
26+
prompt = ChatPromptTemplate.from_messages([
27+
("system", "You are Nestor, a virtual assistant. Answer to the question. The conversation history is {history}"),
28+
("human", "{input}"),
29+
])
30+
31+
# Set the size of the memory window to 10 exchanges.
32+
memory = ConversationBufferWindowMemory(k=10)
33+
34+
# Create a chain that "apply" the prompt to the model.
35+
conversation = ConversationChain(llm=model, memory=memory, prompt=prompt)
36+
37+
# Invoke the chatbot
38+
question = "Hello, my name is Stéphane"
39+
print(f"👤: {question}")
40+
print("🤖:")
41+
conversation.predict(input=question)
42+
43+
question = "What is my name?"
44+
print(f"\n👤: {question}")
45+
print("🤖:")
46+
conversation.predict(input=question)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import time
2+
import os
3+
4+
from langchain import hub
5+
6+
from langchain_mistralai import ChatMistralAI
7+
8+
from langchain_chroma import Chroma
9+
10+
from langchain_community.document_loaders import TextLoader
11+
from langchain_community.embeddings.ovhcloud import OVHCloudEmbeddings
12+
13+
from langchain_core.runnables import RunnablePassthrough
14+
from langchain_text_splitters import RecursiveCharacterTextSplitter
15+
16+
# Load environment variables: see https://endpoints.ai.cloud.ovh.net/ for more information
17+
_OVH_AI_ENDPOINTS_ACCESS_TOKEN = os.environ.get('OVH_AI_ENDPOINTS_ACCESS_TOKEN')
18+
_OVH_AI_ENDPOINTS_MODEL_NAME = os.environ.get('OVH_AI_ENDPOINTS_MODEL_NAME')
19+
_OVH_AI_ENDPOINTS_MODEL_URL = os.environ.get('OVH_AI_ENDPOINTS_MODEL_URL')
20+
_OVH_AI_ENDPOINTS_EMBEDDING_MODEL_NAME = os.environ.get('OVH_AI_ENDPOINTS_EMBEDDING_MODEL_NAME')
21+
22+
# Configure the used model: Mistral
23+
model = ChatMistralAI(model=_OVH_AI_ENDPOINTS_MODEL_NAME,
24+
api_key=_OVH_AI_ENDPOINTS_ACCESS_TOKEN,
25+
endpoint=_OVH_AI_ENDPOINTS_MODEL_URL,
26+
max_tokens=1500,
27+
streaming=True)
28+
29+
# Load and split the documents
30+
loader = TextLoader("./rag-files/content.txt")
31+
docs = loader.load()
32+
33+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
34+
splits = text_splitter.split_documents(docs)
35+
36+
# Create the vector thanks to OVHcloud embedding model
37+
vectorstore = Chroma.from_documents(documents=splits,
38+
embedding=OVHCloudEmbeddings(model_name=_OVH_AI_ENDPOINTS_EMBEDDING_MODEL_NAME,
39+
access_token=_OVH_AI_ENDPOINTS_ACCESS_TOKEN))
40+
41+
# Define the prompt template for the chatbot
42+
prompt = hub.pull("rlm/rag-prompt")
43+
44+
# Create a chain that "apply" the prompt to the model.
45+
rag_chain = (
46+
{"context": vectorstore.as_retriever(), "question": RunnablePassthrough()} |
47+
prompt
48+
| model
49+
)
50+
51+
# Invoke the chatbot
52+
print("👤: Which company created AI Endpoints?")
53+
print("🤖:")
54+
for r in rag_chain.stream({"question", "Which company created AI Endpoints?"}):
55+
print(r.content, end="", flush=True)
56+
time.sleep(0.150)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import time
2+
import os
3+
4+
from langchain_mistralai import ChatMistralAI
5+
from langchain_core.prompts import ChatPromptTemplate
6+
7+
# Load environment variables: see https://endpoints.ai.cloud.ovh.net/ for more information
8+
_OVH_AI_ENDPOINTS_ACCESS_TOKEN = os.environ.get('OVH_AI_ENDPOINTS_ACCESS_TOKEN')
9+
_OVH_AI_ENDPOINTS_MODEL_NAME = os.environ.get('OVH_AI_ENDPOINTS_MODEL_NAME')
10+
_OVH_AI_ENDPOINTS_MODEL_URL = os.environ.get('OVH_AI_ENDPOINTS_MODEL_URL')
11+
12+
# Configure the used model: Mistral
13+
model = ChatMistralAI(model=_OVH_AI_ENDPOINTS_MODEL_NAME,
14+
api_key=_OVH_AI_ENDPOINTS_ACCESS_TOKEN,
15+
endpoint=_OVH_AI_ENDPOINTS_MODEL_URL,
16+
max_tokens=1500,
17+
streaming=True)
18+
19+
# Define the prompt template for the chatbot
20+
prompt = ChatPromptTemplate.from_messages([
21+
("system", "You are a Nestor, a virtual assistant. Answer to the question."),
22+
("human", "{question}"),
23+
])
24+
25+
# Create a chain that "apply" the prompt to the model.
26+
chain = prompt | model
27+
28+
# Invoke the chatbot
29+
print("👤: What is OVHcloud?")
30+
print("🤖:")
31+
for r in chain.stream({"question", "What is OVHcloud?"}):
32+
print(r.content, end="", flush=True)
33+
time.sleep(0.150)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import os
2+
3+
from langchain_mistralai import ChatMistralAI
4+
from langchain_core.prompts import ChatPromptTemplate
5+
6+
# Load environment variables: see https://endpoints.ai.cloud.ovh.net/ for more information
7+
_OVH_AI_ENDPOINTS_ACCESS_TOKEN = os.environ.get('OVH_AI_ENDPOINTS_ACCESS_TOKEN')
8+
_OVH_AI_ENDPOINTS_MODEL_NAME = os.environ.get('OVH_AI_ENDPOINTS_MODEL_NAME')
9+
_OVH_AI_ENDPOINTS_MODEL_URL = os.environ.get('OVH_AI_ENDPOINTS_MODEL_URL')
10+
11+
# Configure the used model: Mistral
12+
model = ChatMistralAI(model=_OVH_AI_ENDPOINTS_MODEL_NAME,
13+
api_key=_OVH_AI_ENDPOINTS_ACCESS_TOKEN,
14+
endpoint=_OVH_AI_ENDPOINTS_MODEL_URL,
15+
max_tokens=1500)
16+
17+
# Define the prompt template for the chatbot
18+
prompt = ChatPromptTemplate.from_messages([
19+
("system", "You are Nestor, a virtual assistant. Answer to the question."),
20+
("human", "{question}"),
21+
])
22+
23+
# Create a chain that "apply" the prompt to the model.
24+
chain = prompt | model
25+
26+
# Invoke the chatbot
27+
response = chain.invoke("What is OVHcloud?")
28+
29+
print("👤: What is OVHcloud?")
30+
print(f"🤖:{response.content}")

0 commit comments

Comments
 (0)