Session-based Custom ChatGPT Model for Website Content Utilizing OpenAI GPT-4 LLM, Langchain LLMChain, and MongoDB Conversational Memory

Happy LLM

(λx.x)eranga
Effectz.AI
21 min readDec 31, 2023

--

Background

In my previous post I have discussed about creating Session-based Custom ChatGPT Model for Website Content Utilizing OpenAI GPT-4 LLM, Langchain ConversationalRetrievalChain, and MongoDB Conversational Memory. In this post, I am implementing the same Chatbot model but using LLMChain with a Custom Prompt instead of ConversationalRetrievalChain. The LLMChain is specifically designed to employ a language model, such as OpenAI GPT-4, for response generation. It fundamentally depends on the intrinsic knowledge and capabilities of the language model.

LLMChain processes a user’s input directly through a language model to produce a response, which is based on the model’s training data and its proficiency in generating language-based solutions. To introduce additional knowledge/input for the LLM, I have utilized a custom prompt. A prompt for a language model is essentially a set of instructions or inputs provided by the user to direct the model’s response. This custom prompt takes input, which constitutes additional knowledge gathered from external sources, such as document data, database data, website data etc. This prompt, enriched with additional information, is then conveyed to the language model, enabling it to integrate this information into its response.

Leveraging the functionalities of the LLMChain, the Chatbot incorporates a custom-made dataset, which is dynamically scraped from an online website using Langchain’s RecursiveUrlLoader. Users can interact with the website’s data through the Chatbot, which is powered by OpenAI’s GPT-4 LLM. For demonstration purposes, I’ve selected the Open5GS documentation website (Open5GS is a C-language implementation of the 5G Core). The data from the Open5GS documentation is scraped using Langchain’s RecursiveUrlLoader, split, and then stored in the Chroma vector database as vector embeddings. Consequently, users can seamlessly interact with the content of the Open5GS documentation via the Chatbot, which is built with the GPT-4 LLM. The GPT-4 LLM provides answers to user questions based on the content in the Open5GS documentation.

A key feature of this API is its capability to simultaneously handle requests from multiple users while effectively managing their individual sessions. This functionality is anchored in the integration of MongoDB for chat conversational memory management. This setup ensures that all user chat histories are persistently stored in MongoDB, facilitating the retrieval of past interactions. Such a memory system is crucial for maintaining a rich, context-aware dialogue history, enhancing the chatbot’s capacity for engaging in more meaningful and contextually relevant discussions, informed by knowledge of previous interactions.

Functionality

The primary functional distinction between this Chatbot and the one presented in the previous post’s ConversationalRetrievalChain example lies in the use of LLMChain with a custom prompt. While the ConversationalRetrievalChain utilizes an inbuilt prompt, the LLMChain in this instance allows for manually specifying the prompt, thereby feeding additional knowledge to the LLM. Consequently, the LLM processes the user’s question and generates an answer based on the provided input. The following figure illustrates the end-to-end functionality of the Chatbot. For more detailed information about each step, please refer to the previous post.

Implementation

The complete implementation of this ChatBot is detailed below. The full source code of the ChatBot agent is available for access and review on GitLab.

1. Configurations

In the config.py file, I have defined various configurations used in the ChatBot. These configurations are read through environment variables in adherence to the principles of 12-factor apps.

import os

# openai api key
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY', '')

# define init index
INIT_INDEX = os.getenv('INIT_INDEX', 'false').lower() == 'true'

# vector index persist directory
INDEX_PERSIST_DIRECTORY = os.getenv('INDEX_PERSIST_DIRECTORY', "./data/chromadb")

# target url to scrape
TARGET_URL = os.getenv('TARGET_URL', "https://open5gs.org/open5gs/docs/")

# http api port
HTTP_PORT = os.getenv('HTTP_PORT', 7654)

# mongodb config host, username, password
MONGO_HOST = os.getenv('MONGO_HOST', 'localhost')
MONGO_PORT = os.getenv('MONGO_PORT', 27017)
MONGO_USER = os.getenv('MONGO_USER', 'testuser')
MONGO_PASS = os.getenv('MONGO_PASS', 'testpass')

2. HTTP API

The HTTP API implementation is carried out in api.py. This API includes an HTTP POST endpoint api/question, which accepts a JSON object containing a question and user_id. The user_id is utilized for demonstration purposes. In a real-world application, this could be managed with an Authorization header (e.g., JWT Bearer token) in the HTTP request. When a question request is received from the user, it is forwarded to the chat function in the ChatBot model.

from flask import Flask
from flask import jsonify
from flask import request
from flask_cors import CORS
import logging
import sys
from model import init_index
from model import init_conversation
from model import chat
from config import *

app = Flask(__name__)
CORS(app)

logging.basicConfig(stream=sys.stdout, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

@app.route('/api/question', methods=['POST'])
def post_question():
json = request.get_json(silent=True)
question = json['question']
user_id = json['user_id']
logging.info("post question `%s` for user `%s`", question, user_id)

resp = chat(question, user_id)
data = {'answer':resp}

return jsonify(data), 200

if __name__ == '__main__':
init_index()
init_conversation()
app.run(host='0.0.0.0', port=HTTP_PORT, debug=True)

3. ChatBot Model

Below is the implementation of the ChatBot Model. It includes a function, init_index, which scrapes data from a given web URL and creates the vector store. An environment variable, INIT_INDEX, is used to determine whether to create the index. The init_conversation function initializes the LLMChain with custom prompt while the chat function is responsible for posting questions to the LLM. In the previous example, the document search function within vectordb was automatically handled by the ConversationalRetrievalChain. In this case, I have conducted a manual semantic search to find documents related to the user’s question in vectordb. These search results are then passed to the LLM along with a custom prompt.

import os
from langchain.chat_models import ChatOpenAI
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import Chroma
from langchain.chains import ConversationalRetrievalChain, LLMChain
from langchain.memory import ConversationBufferMemory
from langchain.llms import OpenAI
from langchain.memory import MongoDBChatMessageHistory
from langchain.document_loaders.recursive_url_loader import RecursiveUrlLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.prompts import ChatPromptTemplate, HumanMessagePromptTemplate, MessagesPlaceholder, SystemMessagePromptTemplate, PromptTemplate
from langchain.utils.html import (PREFIXES_TO_IGNORE_REGEX,
SUFFIXES_TO_IGNORE_REGEX)
from bs4 import BeautifulSoup as Soup
import logging
import sys
from config import *

logging.basicConfig(stream=sys.stdout, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

global conversation
global vectordb
conversation = None
vectordb = None

def init_index():
if not INIT_INDEX:
logging.info("continue without initializing index")
return

logging.info("initializing index from `%s`", TARGET_URL)

# scrape data from web
documents = RecursiveUrlLoader(
TARGET_URL,
max_depth=4,
extractor=lambda x: Soup(x, "html.parser").text,
prevent_outside=True,
use_async=True,
timeout=600,
check_response_status=True,
# drop trailing / to avoid duplicate pages.
link_regex=(
f"href=[\"']{PREFIXES_TO_IGNORE_REGEX}((?:{SUFFIXES_TO_IGNORE_REGEX}.)*?)"
r"(?:[\#'\"]|\/[\#'\"])"
),
).load()

logging.info("index created with `%d` documents", len(documents))

# split text
text_splitter = RecursiveCharacterTextSplitter(chunk_size=4000, chunk_overlap=200)
spllited_documents = text_splitter.split_documents(documents)

# create embedding and persist on vector db
embeddings = OpenAIEmbeddings()
vectordb = Chroma.from_documents(
documents=spllited_documents,
embedding=embeddings,
persist_directory=INDEX_PERSIST_DIRECTORY
)
vectordb.persist()

def init_conversation():
global vectordb
global conversation

# load index
embeddings = OpenAIEmbeddings()
vectordb = Chroma(persist_directory=INDEX_PERSIST_DIRECTORY,embedding_function=embeddings)

# custom proment
prompt = ChatPromptTemplate(
messages=[
SystemMessagePromptTemplate.from_template(
"""
You are an agent tasked with providing answers about the Open5GS 5G core, based on its official documentation.
"""
),
HumanMessagePromptTemplate.from_template(
"""
Using the provided Open5GS documentation, please respond to the question below:
Question: {question}
Relevant Documents: {input_documents}
Previous Chat History: {chat_history}
"""
)
],
)

# chat with conversational memory
conversation = LLMChain(
llm=ChatOpenAI(temperature=0.7, model_name="gpt-4", max_tokens=1024),
prompt=prompt,
verbose=True,
)

return conversation

def chat(question, user_id):
global conversation
global vectordb

# constrct chat history with mongodb
# session_id is the user_id which comes through http request
# mongo connection string (e.g mongodb://localhost:27017/)
connection_string = f"mongodb://{MONGO_HOST}:{MONGO_PORT}/"
history = MongoDBChatMessageHistory(
connection_string=connection_string, session_id=user_id
)

# find related documetns to the question
documents = vectordb.similarity_search(question)
input_documents = [doc.page_content for doc in documents]
logging.info("%d input documents", len(input_documents))

# gpt conversation
# provide inputs to the proment - question, input_documents, chat_history
answer = conversation.predict(question=question, input_documents=input_documents, chat_history=history.messages)

logging.info("got response from llm - %s", answer)

# save history
history.add_user_message(question)
history.add_ai_message(answer)

return answer

Run Application

Below are the main steps to operate the Chatbot application and interact with it. Questions can be submitted using the HTTP API, and responses will be received accordingly.

1. Install Dependencies

In this application, I have utilized a number of Python packages that need to be installed using Python’s pip package manager before running the application. The requirements.txt file lists all the necessary packages. These packages can be easily installed by executing the command pip install -r requirements.txt.

Flask==2.0.1
Werkzeug==2.2.2
flask-cors
pymongo
seaborn
openai
langchain==0.0.352
BeautifulSoup4
chromadb==0.3.29
Cython
tiktoken

2. Run MongoDB

As previously mentioned, MongoDB has been utilized as the conversational memory for the ChatBot application. To begin, it is necessary to first start running MongoDB.

# run mongodb with docker
❯❯ docker run -d -p 27017:27017 --name test-mongo mongo:latest

# running container
❯❯ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ba66af3866b1 docker.io/library/mongo:latest mongod 24 hours ago Up 24 hours 0.0.0.0:27017->27017/tcp test-mongo

3. Run Chatbot

The Chatbot can be initiated through api.py as outlined below. Prior to running it, it's necessary to set a few configurations via environment variables. Once app.py is executed, it will start the HTTP API, enabling users to post their questions.

# set openai API key
❯❯ export OPENAI_API_KEY=<add openai api key here>

# init index determine weather needs to create the index
❯❯ export INIT_INDEX=true

# mongo db host(local machine ip in this case)
❯❯ export MONGO_HOST=10.13.40.29

# run the chatbot application
❯❯ python api.py
2023-12-30 19:17:26,820 - INFO - initializing index from `https://open5gs.org/open5gs/docs/`
2023-12-30 19:17:27,579 - INFO - index created with `18` documents
2023-12-30 19:17:27,629 - INFO - Note: NumExpr detected 12 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
2023-12-30 19:17:27,629 - INFO - NumExpr defaulting to 8 threads.
2023-12-30 19:17:27,721 - INFO - Anonymized telemetry enabled. See https://docs.trychroma.com/telemetry for more information.
2023-12-30 19:17:27,814 - INFO - loaded in 602 embeddings
2023-12-30 19:17:27,814 - INFO - loaded in 1 collections
2023-12-30 19:17:27,815 - INFO - collection with name langchain already exists, returning existing collection
2023-12-30 19:17:30,683 - INFO - Persisting DB to disk, putting it in the save folder: ./data/chromadb
2023-12-30 19:17:30,739 - INFO - Anonymized telemetry enabled. See https://docs.trychroma.com/telemetry for more information.
2023-12-30 19:17:30,770 - INFO - loaded in 688 embeddings
2023-12-30 19:17:30,771 - INFO - loaded in 1 collections
2023-12-30 19:17:30,771 - INFO - collection with name langchain already exists, returning existing collection
* Serving Flask app 'api' (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
2023-12-30 19:17:30,785 - INFO - WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on all addresses (0.0.0.0)
* Running on http://127.0.0.1:7654
* Running on http://192.168.0.110:7654
2023-12-30 19:17:30,785 - INFO - Press CTRL+C to quit
2023-12-30 19:17:30,785 - INFO - * Restarting with stat
2023-12-30 19:17:31,482 - INFO - initializing index from `https://open5gs.org/open5gs/docs/`
2023-12-30 19:17:31,847 - INFO - index created with `18` documents
2023-12-30 19:17:31,897 - INFO - Note: NumExpr detected 12 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
2023-12-30 19:17:31,897 - INFO - NumExpr defaulting to 8 threads.
2023-12-30 19:17:31,983 - INFO - Anonymized telemetry enabled. See https://docs.trychroma.com/telemetry for more information.
2023-12-30 19:17:32,076 - INFO - loaded in 688 embeddings
2023-12-30 19:17:32,076 - INFO - loaded in 1 collections
2023-12-30 19:17:32,077 - INFO - collection with name langchain already exists, returning existing collection
2023-12-30 19:17:36,169 - INFO - Persisting DB to disk, putting it in the save folder: ./data/chromadb
2023-12-30 19:17:36,232 - INFO - Anonymized telemetry enabled. See https://docs.trychroma.com/telemetry for more information.
2023-12-30 19:17:36,269 - INFO - loaded in 774 embeddings
2023-12-30 19:17:36,269 - INFO - loaded in 1 collections
2023-12-30 19:17:36,270 - INFO - collection with name langchain already exists, returning existing collection
2023-12-30 19:17:36,274 - WARNING - * Debugger is active!
2023-12-30 19:17:36,281 - INFO - * Debugger PIN: 973-611-562

4. Post Question

Once the Chatbot application is running, I can submit questions related to the Open5GS documentation via the HTTP API.

# ask first question
❯❯ curl -i -XPOST "http://localhost:7654/api/question" \
--header "Content-Type: application/json" \
--data '
{
"question": "How open5gs work?",
"user_id": "zio"
}
'


# custom prompt with llmchain
> Entering new LLMChain chain...
Prompt after formatting:
System:
You are an agent tasked with providing answers about the Open5GS 5G core, based on its official documentation.

Human:
Using the provided Open5GS documentation, please respond to the question below:
Question: How open5gs work?
Relevant Documents: ['Documentation | Open5GS Open5GS Documentation Features Support CLA OSS Notice GitHub Documentation
User’s Guide Quickstart Building Open5GS from Sources Tutorials Your First LTE 5G SA COTS UE from SRS Metrics with
Prometheus VoLTE Setup with Kamailio IMS and Open5GS Dockerized VoLTE Setup Roaming Inside Source Code UPF Code
Explanation SMF Code Explanation Troubleshooting Simple Issues Now in Github Issues Platform Specific Notes
Debian/Ubuntu CentOS Fedora MacOSX(Apple Silicon) MacOSX(Intel) FreeBSD Alpine Hardware Specific Notes eNodeBs/gNodeBs
tested on Open5GS @infinitydon Open5GS on Amazon Elastic Kubernetes Service Kubernetes Open5GS Deployment 5G Core SBI
mTLS Using External Certificate PKI 5G Frame Routing 5G SCTP LoadBalancer Using LoxiLB(Video Link) 5G Roaming With
Mutual TLS @nickvsnetworking My first 5G Core : Open5GS and UERANSIM Sending SMS in Open5GS LTE Networks using the SGs
Interface and OsmoMSC OsmoMSC and Open5GS MME – SGs Interface for CSCF / InterRAT Handover Static IPs for UEs Open5GS
without NAT Basics of EPC/LTE Online Charging (OCS) Backing up and Restoring Open5GS Diameter Routing Agents - Part 1,
Part 2, Part 3 @s5uishida Open5GS EPC & OpenAirInterface UE/RAN Sample configuration Open5GS 5GC & UERANSIM UE/RAN
Sample Configuration Open5GS & UERANSIM - Select nearby UPF according to the connected gNodeB VoLTE and SMS
Configuration for docker_open5gs Select nearby UPF(PGW-U) according to the connected eNodeB Select UPF based on S-NSSAI
SCP Indirect communication Model C Monitoring Metrics with Prometheus Frame Routing VPP-UPF with DPDK UERANSIM with
eUPF(eBPF/XDP UPF) srsRAN with eUPF(eBPF/XDP UPF) Measurement of UPF Performance @gradiant helm charts Open5GS EPC and
SRS LTE in kubernetes Open5GS NGC and UERANSIM in kubernetes Open5GS NGC and OpenAirInterface GNB with ettus USRP in
kubernetes Open5GS EPC and SRS ENB with ettus USRP in kubernetes Open5GS with Service Communication Proxy in kubernetes
Open5GS Sukchan Lee acetcom@gmail.com GitHub open5gs Open5GS is a C-language implementation of 5G Core and EPC, i.e. the
core network of NR/LTE network (Release-17)', 'Documentation | Open5GS Open5GS Documentation Features Support CLA OSS
Notice GitHub Documentation User’s Guide Quickstart Building Open5GS from Sources Tutorials Your First LTE 5G SA COTS UE
from SRS Metrics with Prometheus VoLTE Setup with Kamailio IMS and Open5GS Dockerized VoLTE Setup Roaming Inside Source
Code UPF Code Explanation SMF Code Explanation Troubleshooting Simple Issues Now in Github Issues Platform Specific
Notes Debian/Ubuntu CentOS Fedora MacOSX(Apple Silicon) MacOSX(Intel) FreeBSD Alpine Hardware Specific Notes
eNodeBs/gNodeBs tested on Open5GS @infinitydon Open5GS on Amazon Elastic Kubernetes Service Kubernetes Open5GS
Deployment 5G Core SBI mTLS Using External Certificate PKI 5G Frame Routing 5G SCTP LoadBalancer Using LoxiLB(Video
Link) 5G Roaming With Mutual TLS @nickvsnetworking My first 5G Core : Open5GS and UERANSIM Sending SMS in Open5GS LTE
Networks using the SGs Interface and OsmoMSC OsmoMSC and Open5GS MME – SGs Interface for CSCF / InterRAT Handover Static
IPs for UEs Open5GS without NAT Basics of EPC/LTE Online Charging (OCS) Backing up and Restoring Open5GS Diameter
Routing Agents - Part 1, Part 2, Part 3 @s5uishida Open5GS EPC & OpenAirInterface UE/RAN Sample configuration Open5GS
5GC & UERANSIM UE/RAN Sample Configuration Open5GS & UERANSIM - Select nearby UPF according to the connected gNodeB
VoLTE and SMS Configuration for docker_open5gs Select nearby UPF(PGW-U) according to the connected eNodeB Select UPF
based on S-NSSAI SCP Indirect communication Model C Monitoring Metrics with Prometheus Frame Routing VPP-UPF with DPDK
UERANSIM with eUPF(eBPF/XDP UPF) srsRAN with eUPF(eBPF/XDP UPF) Measurement of UPF Performance @gradiant helm charts
Open5GS EPC and SRS LTE in kubernetes Open5GS NGC and UERANSIM in kubernetes Open5GS NGC and OpenAirInterface GNB with
ettus USRP in kubernetes Open5GS EPC and SRS ENB with ettus USRP in kubernetes Open5GS with Service Communication Proxy
in kubernetes Open5GS Sukchan Lee acetcom@gmail.com GitHub open5gs Open5GS is a C-language implementation of 5G Core and
EPC, i.e. the core network of NR/LTE network (Release-17)']
Previous Chat History: []


# response status
HTTP/1.1 200 OK
Server: Werkzeug/2.3.7 Python/3.11.2
Date: Sun, 31 Dec 2023 00:26:12 GMT
Content-Type: application/json
Content-Length: 1084
Access-Control-Allow-Origin: *
Connection: close


# response
{
"answer": "Open5GS is a C-language implementation of the 5G Core and EPC, which represent the core network of the NR/LTE network (Release-17). It's designed to support the development and testing of 5G and 4G networks.\n\nThe Open5GS project includes guides and tutorials for various applications like setting up your first LTE 5G SA COTS UE, metrics with Prometheus, VoLTE setup with Kamailio IMS and Open5GS, Roaming, and Dockerized VoLTE setup. \n\nThere are also instructions for building Open5GS from sources and deploying it on various platforms including Debian/Ubuntu, CentOS, Fedora, MacOSX (Apple Silicon and Intel), FreeBSD, and Alpine. \n\nFurthermore, there is specific documentation available about the inside source code like UPF code explanation and SMF code explanation.\n\nOpen5GS can also be deployed in a Kubernetes environment and it supports integration with other systems like UERANSIM, OsmoMSC, eNodeBs/gNodeBs, and more. \n\nTroubleshooting guides are also provided to resolve common issues, and the community can contribute to the project via GitHub."
}



---



# ask next question
❯❯ curl -i -XPOST "http://localhost:7654/api/question" \
--header "Content-Type: application/json" \
--data '
{
"question": "Give more information about it",
"user_id": "zio"
}
'


# custom prompt with llmchain
> Entering new LLMChain chain...
Prompt after formatting:
System:
You are an agent tasked with providing answers about the Open5GS 5G core, based on its official documentation.

Human:
Using the provided Open5GS documentation, please respond to the question below:
Question: Give more information about it
Relevant Documents: ['$ cp configurator.sh ../config/\n$ cd ../config\n$ ./configurator.sh \nDomain Name:
ims.mnc001.mcc001.3gppnetwork.org\nIP Adress:10.4.128.21\n\n$ cd ../src-web\n$ vim WEB-INF/web.xml\n And, change
open-ims.test to ims.mnc001.mcc001.3gppnetwork.org Prepare mysql database: $ mysql\n<mysql> drop database
hss_db;\n<mysql> create database hss_db;\n<mysql> quit\n Import database located at /opt/OpenIMSCore into hss_db $ cd
/opt/OpenIMSCore\n$ mysql -u root -p hss_db < FHoSS/scripts/hss_db.sql\n$ mysql -u root -p hss_db <
FHoSS/scripts/userdata.sql\n Check grants for mysql access rights at first time installation: $ mysql\n# See last line
in hss_db.sql:\n<mysql> grant delete,insert,select,update on hss_db.* to hss@localhost identified by \'hss\';\n<mysql>
grant delete,insert,select,update on hss_db.* to hss@\'%\' identified by \'hss\';\n Check database if domain names are
o.k. in various entries and privileges $ mysql -u hss -p\n<mysql> show databases;\n<mysql> use hss_db;\n<mysql> select *
from impu;\n Prepare script-file, start HSS Copy startup.sh to hss.sh in root directory $ cp
/opt/OpenIMSCore/FHoSS/deploy/startup.sh /root/hss.sh\n And, add the following to hss.sh before echo Building Classpath
cd /opt/OpenIMSCore/FHoSS/deploy\nJAVA_HOME="/usr/lib/jvm/jdk1.7.0_79"\nCLASSPATH="/usr/lib/jvm/jdk1.7.0_79/jre/lib/"\n
Start HSS using hss.sh $ ./hss.sh\n Access the web-interface of HSS: http://<IMS_VM_FLOATING_IP>:8080/hss.web.console/
For example, http://172.24.15.30:8080/hss.web.console/ user: hssAdmin\npassword: hss\n Then, edit the /etc/hosts
file as follows: In the below example. epc-ims is the hostname of the machine root@epc-ims:~# cat /etc/hosts\n127.0.0.1
localhost\n127.0.0.1 epc-ims\n 20. Add IMS subscription use in FoHSS as follows from the Web GUI Assuming IMSI of the
user as 001010123456791 and MSISDN is 0198765432100 Login to the HSS web console.\nNavigate to the User Identities
page\nCreate the IMSU \nClick IMS Subscription / Create\nEnter:\nName = 001010123456791\nCapabilities Set =
cap_set1\nPreferred S-CSCF = scsf1\nClick Save\n\nCreate the IMPI and Associate the IMPI to the IMSU\nClick Create &
Bind new IMPI\nEnter:\nIdentity = 001010123456791@ims.mnc001.mcc001.3gppnetwork.org\nSecret Key =
8baf473f2f8fd09487cccbd7097c6862 (Ki value as in Open5GS HSS database)\nAuthentication Schemes - All\nDefault =
Digest-AKAv1-MD5\nAMF = 8000 (As in Open5GS HSS database)\nOP = 11111111111111111111111111111111 (As in Open5GS HSS
database)\nSQN = 000000021090 (SQN value as in Open5GS HSS database)\nClick Save\n\nCreate and Associate IMPI to
IMPU\nClick Create & Bind new IMPU\nEnter:\nIdentity = sip:001010123456791@ims.mnc001.mcc001.3gppnetwork.org\nBarring =
Yes\nService Profile = default_sp\nCharging-Info Set = default_charging_set\nIMPU Type = Public_User_Identity\nClick
Save\n\nAdd Visited Network to IMPU\nEnter:\nVisited Network = ims.mnc001.mcc001.3gppnetwork.org\nClick Add\n\nNow, goto
Public User Identity and create further IMPUs as following\n\n1. tel:0198765432100\n\nPublic User Identity
-IMPU-\nIdentity = tel:0198765432100\nService Profile = default_sp\nCharging-Info Set = default_charging_set\nCan
Register = Yes\nIMPU Type = Public_User_Identity\nClick Save\n\nAdd Visited Network to IMPU\nEnter:\nVisited Network =
ims.mnc001.mcc001.3gppnetwork.org\nClick Add\n\nAssociate IMPI(s) to IMPU\nIMPI Identity =
001010123456791@ims.mnc001.mcc001.3gppnetwork.org\nClick Add\n\n2. sip:
0198765432100@ims.mnc001.mcc001.3gppnetwork.org\n\nPublic User Identity -IMPU-\nIdentity = sip:
0198765432100@ims.mnc001.mcc001.3gppnetwork.org\nService Profile = default_sp\nCharging-Info Set =
default_charging_set\nCan Register = Yes\nIMPU Type = Public_User_Identity\nClick Save\n\nAdd Visited Network to
IMPU\nEnter:\nVisited Network = ims.mnc001.mcc001.3gppnetwork.org\nClick Add\n\nAssociate IMPI(s) to IMPU\nIMPI
Identity = 001010123456791@ims.mnc001.mcc001.3gppnetwork.org\nClick Add\n\nAnd, finally add these IMPUs as implicit set
of IMSI derived IMPU in HSS i.e sip:001010123456791@ims.mnc001.mcc001.3gppnetwork.org as follows:']
Previous Chat History: [HumanMessage(content='How open5gs work?'), AIMessage(content="Open5GS is a C-language implementation of the 5G
Core and EPC, which represent the core network of the NR/LTE network (Release-17). It's designed to support the
development and testing of 5G and 4G networks.\n\nThe Open5GS project includes guides and tutorials for various
applications like setting up your first LTE 5G SA COTS UE, metrics with Prometheus, VoLTE setup with Kamailio IMS and
Open5GS, Roaming, and Dockerized VoLTE setup. \n\nThere are also instructions for building Open5GS from sources and
deploying it on various platforms including Debian/Ubuntu, CentOS, Fedora, MacOSX (Apple Silicon and Intel), FreeBSD,
and Alpine. \n\nFurthermore, there is specific documentation available about the inside source code like UPF code
explanation and SMF code explanation.\n\nOpen5GS can also be deployed in a Kubernetes environment and it supports
integration with other systems like UERANSIM, OsmoMSC, eNodeBs/gNodeBs, and more. \n\nTroubleshooting guides are also
provided to resolve common issues, and the community can contribute to the project via GitHub.")]


# response status
HTTP/1.1 200 OK
Server: Werkzeug/2.3.7 Python/3.11.2
Date: Sun, 31 Dec 2023 00:26:56 GMT
Content-Type: application/json
Content-Length: 1342
Access-Control-Allow-Origin: *
Connection: close


# response
{
"answer": "Open5GS is a software implementation of the 5G Core and EPC, specifically designed for the development and testing of 5G and 4G networks. The document provided details the process of setting up the Open5GS environment.\n\nIn the initial stage, a domain name and IP address are configured. Following this, a MySQL database is prepared and the database located at /opt/OpenIMSCore is imported into 'hss_db'. Access rights for MySQL are then checked and granted accordingly. \n\nThe document also describes the process of preparing the HSS script file and starting HSS using 'hss.sh'. Access to the web interface of HSS is then explained, with an example IP address provided for reference. \n\nThe document also provides guidance on editing the /etc/hosts file and adding an IMS subscription in FoHSS from the Web GUI. The process of creating an IMSU, IMPI, and associating the IMPI to the IMSU is thoroughly detailed, including the relevant values to be entered for each step.\n\nIt further explains how to create and associate IMPI to IMPU and add a Visited Network to IMPU. The process of creating further IMPUs under Public User Identity is also described, along with the necessary steps to associate IMPI(s) to IMPU.\n\nFinally, the document instructs on adding these IMPUs as an implicit set of IMSI derived IMPU in HSS."
}



---



# ask question which not exists in the context
❯❯ curl -i -XPOST "http://localhost:7654/api/question" \
--header "Content-Type: application/json" \
--data '
{
"question": "What is free5gc",
"user_id": "zio"
}
'


# custom prompt with llmchain
> Entering new LLMChain chain...
Prompt after formatting:
System:
You are an agent tasked with providing answers about the Open5GS 5G core, based on its official documentation.

Human:
Using the provided Open5GS documentation, please respond to the question below:
Question: What is free5gc
Relevant Documents: ['Documentation | Open5GS Open5GS Documentation Features Support CLA OSS Notice GitHub Documentation
User’s Guide Quickstart Building Open5GS from Sources Tutorials Your First LTE 5G SA COTS UE from SRS Metrics with
Prometheus VoLTE Setup with Kamailio IMS and Open5GS Dockerized VoLTE Setup Roaming Inside Source Code UPF Code
Explanation SMF Code Explanation Troubleshooting Simple Issues Now in Github Issues Platform Specific Notes
Debian/Ubuntu CentOS Fedora MacOSX(Apple Silicon) MacOSX(Intel) FreeBSD Alpine Hardware Specific Notes eNodeBs/gNodeBs
tested on Open5GS @infinitydon Open5GS on Amazon Elastic Kubernetes Service Kubernetes Open5GS Deployment 5G Core SBI
mTLS Using External Certificate PKI 5G Frame Routing 5G SCTP LoadBalancer Using LoxiLB(Video Link) 5G Roaming With
Mutual TLS @nickvsnetworking My first 5G Core : Open5GS and UERANSIM Sending SMS in Open5GS LTE Networks using the SGs
Interface and OsmoMSC OsmoMSC and Open5GS MME – SGs Interface for CSCF / InterRAT Handover Static IPs for UEs Open5GS
without NAT Basics of EPC/LTE Online Charging (OCS) Backing up and Restoring Open5GS Diameter Routing Agents - Part 1,
Part 2, Part 3 @s5uishida Open5GS EPC & OpenAirInterface UE/RAN Sample configuration Open5GS 5GC & UERANSIM UE/RAN
Sample Configuration Open5GS & UERANSIM - Select nearby UPF according to the connected gNodeB VoLTE and SMS
Configuration for docker_open5gs Select nearby UPF(PGW-U) according to the connected eNodeB Select UPF based on S-NSSAI
SCP Indirect communication Model C Monitoring Metrics with Prometheus Frame Routing VPP-UPF with DPDK UERANSIM with
eUPF(eBPF/XDP UPF) srsRAN with eUPF(eBPF/XDP UPF) Measurement of UPF Performance @gradiant helm charts Open5GS EPC and
SRS LTE in kubernetes Open5GS NGC and UERANSIM in kubernetes Open5GS NGC and OpenAirInterface GNB with ettus USRP in
kubernetes Open5GS EPC and SRS ENB with ettus USRP in kubernetes Open5GS with Service Communication Proxy in kubernetes
Open5GS Sukchan Lee acetcom@gmail.com GitHub open5gs Open5GS is a C-language implementation of 5G Core and EPC, i.e. the
core network of NR/LTE network (Release-17)']
Previous Chat History: [HumanMessage(content='How open5gs work?'), AIMessage(content="Open5GS is a C-language
implementation of the 5G Core and EPC, which represent the core network of the NR/LTE network (Release-17). It's
designed to support the development and testing of 5G and 4G networks.\n\nThe Open5GS project includes guides and
tutorials for various applications like setting up your first LTE 5G SA COTS UE, metrics with Prometheus, VoLTE setup
with Kamailio IMS and Open5GS, Roaming, and Dockerized VoLTE setup. \n\nThere are also instructions for building Open5GS
from sources and deploying it on various platforms including Debian/Ubuntu, CentOS, Fedora, MacOSX (Apple Silicon and
Intel), FreeBSD, and Alpine. \n\nFurthermore, there is specific documentation available about the inside source code
like UPF code explanation and SMF code explanation.\n\nOpen5GS can also be deployed in a Kubernetes environment and it
supports integration with other systems like UERANSIM, OsmoMSC, eNodeBs/gNodeBs, and more. \n\nTroubleshooting guides
are also provided to resolve common issues, and the community can contribute to the project via GitHub."), HumanMessage(
content='Give more information about it'), AIMessage(content="Open5GS is a software implementation of the 5G Core and
EPC, specifically designed for the development and testing of 5G and 4G networks. The document provided details the
process of setting up the Open5GS environment.\n\nIn the initial stage, a domain name and IP address are configured.
Following this, a MySQL database is prepared and the database located at /opt/OpenIMSCore is imported into 'hss_db'.
Access rights for MySQL are then checked and granted accordingly. \n\nThe document also describes the process of
preparing the HSS script file and starting HSS using 'hss.sh'. Access to the web interface of HSS is then explained,
with an example IP address provided for reference. \n\nThe document also provides guidance on editing the /etc/hosts
file and adding an IMS subscription in FoHSS from the Web GUI. The process of creating an IMSU, IMPI, and associating
the IMPI to the IMSU is thoroughly detailed, including the relevant values to be entered for each step.\n\nIt further
explains how to create and associate IMPI to IMPU and add a Visited Network to IMPU. The process of creating further
IMPUs under Public User Identity is also described, along with the necessary steps to associate IMPI(s) to
IMPU.\n\nFinally, the document instructs on adding these IMPUs as an implicit set of IMSI derived IMPU in HSS.")]


# response status
HTTP/1.1 200 OK
Server: Werkzeug/2.3.7 Python/3.11.2
Date: Sun, 31 Dec 2023 00:38:29 GMT
Content-Type: application/json
Content-Length: 528
Access-Control-Allow-Origin: *
Connection: close


# response
# lmm use it's own knowledge to generate the answer
{
"answer": "Free5GC is not directly mentioned in the provided Open5GS documentation. However, from an external perspective, Free5GC is an open-source 5G core network project. It aims to implement the 5G core network functions, as defined by 3GPP standards, in software. This project allows researchers and industries to experiment with a functional 5G network setup. It's worth noting that while Open5GS and free5GC both aim at providing a 5G core network, they are separate projects developed by different organizations."
}

5. Inspect Chat Storage

Now, all user questions and responses from the LLM are stored in MongoDB’s conversational memory. I can connect to MongoDB to inspect and review the chat history saved within the collection.

# mongod run with docker and no password(means no auth)
❯❯ docker run -d -p 27017:27017 --name test-mongo mongo:latest

# connect to mongo container
❯❯ docker exec -it test-mongo bash

# connect to mongodb shell
mongosh

# show dbs
# `chat_history` is the db created by langchain
test> show dbs
admin 40.00 KiB
chat_history 116.00 KiB
config 72.00 KiB
local 40.00 KiB

# connect to `chat_history` db
test> use chat_history
switched to db chat_history

# show collections
# `message_store` is the collection created by langchain to store chat history
chat_history> show collections
message_store

# find chat history belongs to the use `zio` in the `message_store`
chat_history> db.message_store.find({SessionId: 'zio'})
[
{
_id: ObjectId('6590b52456ad466862e2cc99'),
SessionId: 'zio',
History: '{"type": "human", "data": {"content": "How open5gs work?", "additional_kwargs": {}, "type": "human", "example": false}}'
},
{
_id: ObjectId('6590b52456ad466862e2cc9a'),
SessionId: 'zio',
History: `{"type": "ai", "data": {"content": "Open5GS is a C-language implementation of the 5G Core and EPC, which represent the core network of the NR/LTE network (Release-17). It's designed to support the development and testing of 5G and 4G networks.\\n\\nThe Open5GS project includes guides and tutorials for various applications like setting up your first LTE 5G SA COTS UE, metrics with Prometheus, VoLTE setup with Kamailio IMS and Open5GS, Roaming, and Dockerized VoLTE setup. \\n\\nThere are also instructions for building Open5GS from sources and deploying it on various platforms including Debian/Ubuntu, CentOS, Fedora, MacOSX (Apple Silicon and Intel), FreeBSD, and Alpine. \\n\\nFurthermore, there is specific documentation available about the inside source code like UPF code explanation and SMF code explanation.\\n\\nOpen5GS can also be deployed in a Kubernetes environment and it supports integration with other systems like UERANSIM, OsmoMSC, eNodeBs/gNodeBs, and more. \\n\\nTroubleshooting guides are also provided to resolve common issues, and the community can contribute to the project via GitHub.", "additional_kwargs": {}, "type": "ai", "example": false}}`
},
{
_id: ObjectId('6590b55056ad466862e2cc9c'),
SessionId: 'zio',
History: '{"type": "human", "data": {"content": "Give more information about it", "additional_kwargs": {}, "type": "human", "example": false}}'
},
{
_id: ObjectId('6590b55056ad466862e2cc9d'),
SessionId: 'zio',
History: `{"type": "ai", "data": {"content": "Open5GS is a software implementation of the 5G Core and EPC, specifically designed for the development and testing of 5G and 4G networks. The document provided details the process of setting up the Open5GS environment.\\n\\nIn the initial stage, a domain name and IP address are configured. Following this, a MySQL database is prepared and the database located at /opt/OpenIMSCore is imported into 'hss_db'. Access rights for MySQL are then checked and granted accordingly. \\n\\nThe document also describes the process of preparing the HSS script file and starting HSS using 'hss.sh'. Access to the web interface of HSS is then explained, with an example IP address provided for reference. \\n\\nThe document also provides guidance on editing the /etc/hosts file and adding an IMS subscription in FoHSS from the Web GUI. The process of creating an IMSU, IMPI, and associating the IMPI to the IMSU is thoroughly detailed, including the relevant values to be entered for each step.\\n\\nIt further explains how to create and associate IMPI to IMPU and add a Visited Network to IMPU. The process of creating further IMPUs under Public User Identity is also described, along with the necessary steps to associate IMPI(s) to IMPU.\\n\\nFinally, the document instructs on adding these IMPUs as an implicit set of IMSI derived IMPU in HSS.", "additional_kwargs": {}, "type": "ai", "example": false}}`
},
{
_id: ObjectId('6590b80556ad466862e2cc9f'),
SessionId: 'zio',
History: '{"type": "human", "data": {"content": "What is free5gc", "additional_kwargs": {}, "type": "human", "example": false}}'
},
{
_id: ObjectId('6590b80556ad466862e2cca0'),
SessionId: 'zio',
History: `{"type": "ai", "data": {"content": "Free5GC is not directly mentioned in the provided Open5GS documentation. However, from an external perspective, Free5GC is an open-source 5G core network project. It aims to implement the 5G core network functions, as defined by 3GPP standards, in software. This project allows researchers and industries to experiment with a functional 5G network setup. It's worth noting that while Open5GS and free5GC both aim at providing a 5G core network, they are separate projects developed by different organizations.", "additional_kwargs": {}, "type": "ai", "example": false}}`
}
]

# count entries in `message_store`
chat_history> db.message_store.count({SessionId: 'zio'})
6

Reference

  1. https://medium.com/rahasak/session-based-chatbot-for-website-content-utilizing-openai-gpt-4-llm-langchain-ef09e0706767
  2. https://medium.com/rahasak/creating-custom-chatgpt-with-your-own-dataset-using-openai-gpt-3-5-model-llamaindex-and-langchain-5d5837bf9d56
  3. https://cobusgreyling.medium.com/self-critique-llm-chain-using-langchain-smartllmchain-d67c42a4fa83
  4. https://towardsdatascience.com/a-gentle-intro-to-chaining-llms-agents-and-utils-via-langchain-16cd385fca81
  5. https://www.pinecone.io/learn/series/langchain/langchain-prompt-templates/
  6. https://www.unite.ai/zero-to-advanced-prompt-engineering-with-langchain-in-python/

--

--