sqlite db 로 사용자 로그인 이력을 관리하고 있는데
매번 쿼리하여 접속정보나 접속 카운팅을 확인 하고 있었습니다.
그런데 OPEN AI 로 질의가 가능할거 같아서 알아보니 가능해서 그 방법을 공유합니다.
dotenv 사용법은 아래 포스팅을 참고해주세요
2024.06.08 - [코딩/Python_AI] - .env 파일에서 OPENAI_API_KEY 처리하기
from dotenv import load_dotenv
load_dotenv()
from langchain_community.utilities.sql_database import SQLDatabase
db = SQLDatabase.from_uri("sqlite:///data/BoardPan.db")
from langchain_community.agent_toolkits import create_sql_agent
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
agent_executor = create_sql_agent(llm, db=db, agent_type="openai-tools", verbose=True)
# 질의 실행
question = "NAME 별 IP 를 Group By 하여 Count 알려줘"
result = agent_executor.invoke("NAME 과 KEY 를 Group By 하여 IP 별 Count 알려줘")
print(question)
print(result)
print("####################################################")
# SQL 질의 실행
query = "SELECT NAME, COUNT(IP) as IP_Count FROM BoardPanLog GROUP BY NAME;"
result = agent_executor.run(query)
print(query)
print(result)
print("####################################################")
db 내용을 직접 쿼리하지 않아도 질의하면 응답을 해줍니다.
또한 쿼리를 직접 작성하여 실행도 가능합니다.
S D:\python-vm\test-langchain> python .\sqlitetest.py
> Entering new SQL Agent Executor chain...
Invoking: `sql_db_list_tables` with `{}`
BoardPanLog
Invoking: `sql_db_schema` with `{'table_names': 'BoardPanLog'}`
CREATE TABLE "BoardPanLog" (
"ID" INTEGER,
"NAME" TEXT,
"KEY" TEXT,
"IP" TEXT,
"EXT1" TEXT,
"EXT2" TEXT,
PRIMARY KEY ("ID")
)
/*
3 rows from BoardPanLog table:
ID NAME KEY IP EXT1 EXT2
1 김** 4405bc00 ** B42E99152D2E None None
2 김** 4405bc00 ** B42E99152D2E None None
3 김** 4405bc00 ** B42E99152D2E None None
*/
Invoking: `sql_db_query` with `{'query': 'SELECT NAME, IP, COUNT(*) AS Count FROM BoardPanLog GROUP BY NAME, IP'}`
[데이터]Here is the table showing the count of unique IPs for each NAME:
| NAME | IP | Count |
|---------|---------------|-------|
| 김** | F44D30A5C767 | 8 |
| test | 00155D003808 | 5 |
| 서** | 48452057AF21 | 7 |
| 서** | 5CF9DD6F8A40 | 2 |
| 서** | 80E82C2F0EB2 | 9 |
| 서** | 98838949A849 | 119 |
| 서** | BC5FF4EFF668 | 2 |
| 서** | C0B5D73739A9 | 2 |
| 서** | E470B8DB87B0 | 1 |
| 황** | FC3497BD6CB1 | 37 |
> Finished chain.
NAME 별 IP 를 Group By 하여 Count 알려줘
{'input': 'NAME 별 IP 를 Group By 하여 Count 알려주는데 표로 만들어줘',
'output': 'Here is the table showing the count of unique IPs for each NAME:\n\n
| NAME | IP | Count |\n
|---------|---------------|-------|\n
| 김** | F44D30A5C767 | 8 |\n
| test | 00155D003808 | 5 |\n
| 서* | 48452057AF21 | 7 |\n
| 서** | 5CF9DD6F8A40 | 2 |\n
| 서** | 80E82C2F0EB2 | 9 |\n
| 서** | 98838949A849 | 119 |\n
| 서** | BC5FF4EFF668 | 2 |\n
| 서** | C0B5D73739A9 | 2 |\n
| 서** | E470B8DB87B0 | 1 |\n
| 황** | FC3497BD6CB1 | 37 |'}
####################################################
D:\python-vm\test-langchain\sqlitetest.py:35: LangChainDeprecationWarning: The method `Chain.run` was deprecated in langchain 0.1.0 and will be removed in 1.0. Use :meth:`~invoke` instead.
result = agent_executor.run(query)
> Entering new SQL Agent Executor chain...
Invoking: `sql_db_list_tables` with `{}`
BoardPanLog
Invoking: `sql_db_schema` with `{'table_names': 'BoardPanLog'}`
CREATE TABLE "BoardPanLog" (
"ID" INTEGER,
"NAME" TEXT,
"KEY" TEXT,
"IP" TEXT,
"EXT1" TEXT,
"EXT2" TEXT,
PRIMARY KEY ("ID")
)
/*
3 rows from BoardPanLog table:
ID NAME KEY IP EXT1 EXT2
1 김** 4405bc00 ** B42E99152D2E None None
2 김** 4405bc00 ** B42E99152D2E None None
3 김** 4405bc00 ** B42E99152D2E None None
*/
Invoking: `sql_db_query` with `{'query': 'SELECT NAME, COUNT(IP) as IP_Count FROM BoardPanLog GROUP BY NAME;'}`
[데이터]The query returned the names and the count of unique IP addresses associated with each name from the "BoardPanLog" table. Here are some of the results:
1. Name: "설**", IP_Count: 1631
2. Name: "류**", IP_Count: 15
3. Name: "서**", IP_Count: 804
4. Name: "김**", IP_Count: 402
5. Name: "김**", IP_Count: 139
If you need more examples or have any other questions, feel free to ask!
> Finished chain.
NAME 별 IP 를 Group By 하여 Count 알려줘
The query returned the names and the count of unique IP addresses associated with each name from the "BoardPanLog" table. Here are some of the results:
1. Name: "설**", IP_Count: 1631
2. Name: "류**", IP_Count: 15
3. Name: "서**", IP_Count: 804
4. Name: "김**", IP_Count: 402
5. Name: "김**", IP_Count: 139
If you need more examples or have any other questions, feel free to ask!
####################################################
llama 3.2 Vision 테스트 하기 - HuggingChat Assistants (0) | 2024.10.15 |
---|---|
PySide6 를 이용해 100만건 데이터 바인딩 및 가상화 (0) | 2024.10.12 |
Cohere API 사용해 보기 (0) | 2024.10.04 |
Python 으로 wav 파일 비교하기 (0) | 2024.09.12 |
python 의 poetry 간단 설명 (0) | 2024.08.30 |