主干Ai实验室后端项目
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
|
||||
from langchain_community.chat_models.tongyi import ChatTongyi
|
||||
from utils.Tools import all_tools
|
||||
|
||||
llm = ChatTongyi(streaming=False, api_key="sk-fb46eefb6b404382a0a5325202e923a6")
|
||||
llm_with_tools = llm.bind_tools(all_tools)
|
||||
@@ -0,0 +1,22 @@
|
||||
|
||||
import psycopg
|
||||
from langchain_postgres import PostgresChatMessageHistory
|
||||
from psycopg_pool import ConnectionPool
|
||||
from contextlib import contextmanager
|
||||
|
||||
# conn = psycopg.connect("postgresql://postgres:123456@10.10.10.9/ktor2")
|
||||
database_name = "ai_chat_history"
|
||||
pool = ConnectionPool("postgresql://postgres:123456@10.10.10.9/ktor2")
|
||||
|
||||
@contextmanager
|
||||
def getConn():
|
||||
with pool.connection() as temp:
|
||||
temp.autocommit = True # 如果你想所有连接默认 autocommit
|
||||
yield temp # 把 conn 暴露给外部使用
|
||||
|
||||
|
||||
def init():
|
||||
with getConn() as connection:
|
||||
PostgresChatMessageHistory.create_tables(connection, database_name)
|
||||
|
||||
init()
|
||||
@@ -0,0 +1,39 @@
|
||||
import jwt
|
||||
from jwt import PyJWTError
|
||||
from uuid import UUID
|
||||
from fastapi import Header, HTTPException, Depends
|
||||
|
||||
JWT_SECRET = "secret_jwt"
|
||||
JWT_ALGORITHM = "HS256"
|
||||
JWT_AUDIENCE = "snowflake-ink"
|
||||
JWT_ISSUER = "https://snowflake.ink/"
|
||||
|
||||
def get_user_id_from_token(token: str = Header(..., alias="Authorization")) -> UUID:
|
||||
"""
|
||||
从 Authorization 头解析 token,并返回 user_id
|
||||
假设前端传 Authorization: Bearer <token>
|
||||
"""
|
||||
if token.startswith("Bearer "):
|
||||
token = token[7:]
|
||||
else:
|
||||
raise HTTPException(status_code=401, detail="Invalid token format")
|
||||
|
||||
try:
|
||||
payload = jwt.decode(
|
||||
token,
|
||||
JWT_SECRET,
|
||||
algorithms=[JWT_ALGORITHM],
|
||||
audience=JWT_AUDIENCE,
|
||||
issuer=JWT_ISSUER
|
||||
)
|
||||
except PyJWTError:
|
||||
raise HTTPException(status_code=401, detail="Token is missing or invalid")
|
||||
|
||||
if payload.get("token_type") != "access_token":
|
||||
raise HTTPException(status_code=401, detail="Invalid token type")
|
||||
|
||||
user_id = payload.get("user_id")
|
||||
if not user_id:
|
||||
raise HTTPException(status_code=401, detail="User ID not found in token")
|
||||
|
||||
return UUID(user_id)
|
||||
@@ -0,0 +1,12 @@
|
||||
from langchain_community.utilities import SQLDatabase
|
||||
from langchain_community.agent_toolkits import create_sql_agent
|
||||
from langchain_community.chat_models.tongyi import ChatTongyi
|
||||
# SQLAlchemy URI
|
||||
uri = "mssql+pyodbc://f8_db_test:APN^QPr!K9@122.114.58.23/f8_db_test?driver=ODBC+Driver+18+for+SQL+Server&TrustServerCertificate=yes"
|
||||
|
||||
# 建立数据库对象
|
||||
ssDB = SQLDatabase.from_uri(
|
||||
uri,
|
||||
include_tables=["NONGHU_INFO"], # 不加 schema 前缀试试
|
||||
schema="dbo" # 显式指定 schema
|
||||
)
|
||||
Reference in New Issue
Block a user