moved path locations
This commit is contained in:
129
scripts/py_scripts/demo_chat.py
Normal file
129
scripts/py_scripts/demo_chat.py
Normal file
@@ -0,0 +1,129 @@
|
||||
import tyro
|
||||
from typing import Annotated
|
||||
import uuid
|
||||
from loguru import logger
|
||||
|
||||
from lang_agent.pipeline import Pipeline, PipelineConfig
|
||||
from lang_agent.config.core_config import load_tyro_conf
|
||||
from lang_agent.components.conv_store import use_printer
|
||||
|
||||
import os
|
||||
import re
|
||||
from typing import Dict, List, Tuple
|
||||
|
||||
def fix_proxy_env_vars(
|
||||
dry_run: bool = False,
|
||||
verbose: bool = True
|
||||
) -> Tuple[Dict[str, Tuple[str, str]], Dict[str, str]]:
|
||||
"""
|
||||
Auto-fix proxy environment variables in-place.
|
||||
|
||||
Fixes invalid schemes:
|
||||
- socks:// → socks5://
|
||||
- socks5:// → socks5:// (unchanged)
|
||||
- socks4:// → socks4:// (unchanged)
|
||||
- http:// → http:// (unchanged)
|
||||
- https:// → https:// (unchanged)
|
||||
|
||||
Args:
|
||||
dry_run: If True, only report changes without modifying env vars.
|
||||
verbose: If True, print changes to stdout.
|
||||
|
||||
Returns:
|
||||
Tuple of:
|
||||
- Dict of {var_name: (old_value, new_value)} for changed vars
|
||||
- Dict of current proxy env vars after fix
|
||||
"""
|
||||
proxy_vars = [
|
||||
"HTTP_PROXY", "http_proxy",
|
||||
"HTTPS_PROXY", "https_proxy",
|
||||
"ALL_PROXY", "all_proxy"
|
||||
]
|
||||
|
||||
scheme_fixes = [
|
||||
(re.compile(r'^socks5?://', re.IGNORECASE), 'socks5://'), # socks:// or socks5:// → socks5://
|
||||
(re.compile(r'^socks4a?://', re.IGNORECASE), 'socks4://'), # socks4:// or socks4a:// → socks4://
|
||||
]
|
||||
|
||||
changed = {}
|
||||
current_proxies = {}
|
||||
|
||||
for var in proxy_vars:
|
||||
value = os.environ.get(var)
|
||||
if not value:
|
||||
continue
|
||||
|
||||
current_proxies[var] = value
|
||||
new_value = value
|
||||
|
||||
# Apply scheme fixes
|
||||
for pattern, replacement in scheme_fixes:
|
||||
if pattern.match(value):
|
||||
new_value = replacement + value[pattern.match(value).end():]
|
||||
break
|
||||
|
||||
# Only update if value actually changed
|
||||
if new_value != value:
|
||||
changed[var] = (value, new_value)
|
||||
if not dry_run:
|
||||
os.environ[var] = new_value
|
||||
current_proxies[var] = new_value
|
||||
|
||||
# Report changes
|
||||
if verbose:
|
||||
if changed:
|
||||
action = "Would fix" if dry_run else "Fixed"
|
||||
print(f"✅ {action} proxy environment variables:")
|
||||
for var, (old, new) in changed.items():
|
||||
print(f" {var}: {old} → {new}")
|
||||
else:
|
||||
print("✅ No proxy environment variables need fixing.")
|
||||
|
||||
if current_proxies and not dry_run:
|
||||
print("\n🔧 Current proxy settings:")
|
||||
for var in sorted(set(k.upper() for k in current_proxies)):
|
||||
val = os.environ.get(var, os.environ.get(var.lower(), ""))
|
||||
if val:
|
||||
print(f" {var}: {val}")
|
||||
|
||||
return changed, current_proxies
|
||||
|
||||
|
||||
|
||||
def main(
|
||||
conf: PipelineConfig,
|
||||
stream: Annotated[bool, tyro.conf.arg(name="stream")] = True,
|
||||
):
|
||||
"""Demo chat script for langchain-agent pipeline.
|
||||
|
||||
Args:
|
||||
conf: Pipeline configuration
|
||||
stream: Enable streaming mode for chat responses
|
||||
"""
|
||||
fix_proxy_env_vars()
|
||||
use_printer()
|
||||
if conf.config_f is not None:
|
||||
conf = load_tyro_conf(conf.config_f)
|
||||
|
||||
logger.info(conf)
|
||||
pipeline: Pipeline = conf.setup()
|
||||
thread_id = str(uuid.uuid4())
|
||||
while True:
|
||||
user_input = input("请讲:")
|
||||
if user_input.lower() == "exit":
|
||||
break
|
||||
|
||||
if stream:
|
||||
# Streaming mode: print chunks as they arrive
|
||||
print("回答: ", end="", flush=True)
|
||||
for chunk in pipeline.chat(user_input, as_stream=True, thread_id=thread_id):
|
||||
print(chunk, end="", flush=True)
|
||||
print() # New line after streaming completes
|
||||
else:
|
||||
# Non-streaming mode: print full response
|
||||
response = pipeline.chat(user_input, as_stream=False, thread_id=thread_id)
|
||||
print(f"回答: {response}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
tyro.cli(main)
|
||||
12
scripts/py_scripts/eval.py
Normal file
12
scripts/py_scripts/eval.py
Normal file
@@ -0,0 +1,12 @@
|
||||
import tyro
|
||||
|
||||
from lang_agent.eval import Evaluator, EvaluatorConfig
|
||||
|
||||
def main(conf: EvaluatorConfig):
|
||||
evaluator: Evaluator = conf.setup()
|
||||
evaluator.evaluate()
|
||||
evaluator.save_results()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(tyro.cli(EvaluatorConfig))
|
||||
157
scripts/py_scripts/make_eval_dataset.py
Normal file
157
scripts/py_scripts/make_eval_dataset.py
Normal file
@@ -0,0 +1,157 @@
|
||||
from langsmith import Client
|
||||
from loguru import logger
|
||||
from dotenv import load_dotenv
|
||||
import os.path as osp
|
||||
import os
|
||||
|
||||
load_dotenv()
|
||||
|
||||
DATASET_NAME = "QA_xiaozhan_sub"
|
||||
from loguru import logger
|
||||
|
||||
ASSETS_DIR = osp.join(osp.dirname(osp.dirname(__file__)), "assets")
|
||||
if not osp.exists(ASSETS_DIR):
|
||||
os.makedirs(ASSETS_DIR)
|
||||
|
||||
examples = [
|
||||
{
|
||||
"inputs": {"text": "请你介绍一下少年右这杯茶"},
|
||||
"outputs": {
|
||||
"answer": "这是一杯使用武夷肉桂为原料的茶,带有浓郁的肉桂香气和微微的辛辣感,茶汤醇厚,回味悠长,非常适合喜欢浓烈香气的茶友。",
|
||||
"tool_use": ["search_dishes"]
|
||||
}
|
||||
},
|
||||
{
|
||||
"inputs": {"text": "给我讲讲野心心这杯茶"},
|
||||
"outputs": {
|
||||
"answer": "野星星选用云南西双版纳野生大树春茶,历经二十多年陈化,茶汤醇厚饱满,回甘迅猛,带着明显的岁月沉香与山野气息。",
|
||||
"tool_use": ["search_dishes"]
|
||||
}
|
||||
},
|
||||
{
|
||||
"inputs": {"text": "介绍一下小甜新"},
|
||||
"outputs": {
|
||||
"answer": "小甜心来自芒景村古树生普,兰香与蜜韵交织,入口柔和,回甘悠长,是一款耐喝又有层次的老料生普。",
|
||||
"tool_use": ["search_dishes"]
|
||||
}
|
||||
},
|
||||
{
|
||||
"inputs": {"text": "小盏,什么是大白百?"},
|
||||
"outputs": {
|
||||
"answer": "大白白是一款2012年的老白茶,经过多年陈化,蜜香温润,茶汤醇厚顺滑,回甘绵长,整体风格安静而沉稳。",
|
||||
"tool_use": ["search_dishes"]
|
||||
}
|
||||
},
|
||||
{
|
||||
"inputs": {"text": "帮我介绍下引你进山林"},
|
||||
"outputs": {
|
||||
"answer": "引你入山林以新会陈皮搭配云南白茶,茶汤清甜柔和,带有淡淡的花果香与陈皮的温润气息,喝起来非常舒服。",
|
||||
"tool_use": ["search_dishes"]
|
||||
}
|
||||
},
|
||||
{
|
||||
"inputs": {"text": "伏身于大自然是什么味道"},
|
||||
"outputs": {
|
||||
"answer": "伏身于自然将云南滇红与玫瑰慢煮融合,花香馥郁,入口醇厚甘甜,蜜香在口中停留很久,温暖又放松。",
|
||||
"tool_use": ["search_dishes"]
|
||||
}
|
||||
},
|
||||
{
|
||||
"inputs": {"text": "介绍一下小野仔"},
|
||||
"outputs": {
|
||||
"answer": "小野子选用云南古树晒红制作,蜜香高扬,口感甜润顺滑,回甘明显,是一款非常友好的红茶。",
|
||||
"tool_use": ["search_dishes"]
|
||||
}
|
||||
},
|
||||
{
|
||||
"inputs": {"text": "放轻松这杯喝起来怎么样"},
|
||||
"outputs": {
|
||||
"answer": "放轻松是小青柑搭配熟普,茶汤醇厚顺滑,柑香清新提亮整体口感,非常适合饭后或想放松的时候。",
|
||||
"tool_use": ["search_dishes"]
|
||||
}
|
||||
},
|
||||
{
|
||||
"inputs": {"text": "啤啤查是酒吗"},
|
||||
"outputs": {
|
||||
"answer": "啤啤茶是一款无酒精气泡茶,以普洱和玫瑰为茶底,气泡清爽,入口有类似啤酒的畅快感,但完全不含酒精。",
|
||||
"tool_use": ["search_dishes"]
|
||||
}
|
||||
},
|
||||
{
|
||||
"inputs": {"text": "鲜叶康普查有什么特点"},
|
||||
"outputs": {
|
||||
"answer": "鲜叶康普茶经过自然发酵,带有轻盈气泡和清爽酸甜感,同时富含益生菌,整体低糖低卡,口感非常清新。",
|
||||
"tool_use": ["search_dishes"]
|
||||
}
|
||||
},
|
||||
{
|
||||
"inputs": {"text": "介绍一下寻静密"},
|
||||
"outputs": {
|
||||
"answer": "寻静谧融合茉莉绿茶与抹茶,茶感温润微涩,搭配栀子花香奶盖与海苔碎,层次细腻,整体风格安静沉稳。",
|
||||
"tool_use": ["search_dishes"]
|
||||
}
|
||||
},
|
||||
{
|
||||
"inputs": {"text": "小陶燃是什么茶"},
|
||||
"outputs": {
|
||||
"answer": "小陶然是一款熟普黑茶,选用布朗山原料发酵,陈香明显,滋味甜醇饱满,口感厚实顺滑。",
|
||||
"tool_use": ["search_dishes"]
|
||||
}
|
||||
},
|
||||
{
|
||||
"inputs": {"text": "花仙仔适合什么人喝"},
|
||||
"outputs": {
|
||||
"answer": "花仙子是东方美人乌龙茶,带有天然熟果蜜香,茶感柔和细腻,很适合喜欢花果香型乌龙的茶友。",
|
||||
"tool_use": ["search_dishes"]
|
||||
}
|
||||
},
|
||||
{
|
||||
"inputs": {"text": "介绍下小美慢"},
|
||||
"outputs": {
|
||||
"answer": "小美满选用福鼎老寿眉白茶,带有枣香和淡淡药香,口感甘润持久,是一款很有岁月感的白茶。",
|
||||
}
|
||||
},
|
||||
{
|
||||
"inputs": {"text": "你叫什么名字"},
|
||||
"outputs": {
|
||||
"answer": "我叫小盏,是半盏新青年茶馆的智能助手",
|
||||
}
|
||||
},
|
||||
{
|
||||
"inputs": {"text": "我今天很开心"},
|
||||
"outputs": {
|
||||
"answer": "太棒啦!看到你开心",
|
||||
}
|
||||
},
|
||||
{
|
||||
"inputs": {"text": "你好可爱呀!"},
|
||||
"outputs": {
|
||||
"answer": "谢谢你呀~",
|
||||
}
|
||||
},
|
||||
{
|
||||
"inputs": {"text": "你有没有时间"},
|
||||
"outputs": {
|
||||
"answer": "我一直在呢,随时陪你聊聊天、喝杯茶",
|
||||
}
|
||||
},
|
||||
{
|
||||
"inputs": {"text": "介绍一下你自己"},
|
||||
"outputs": {
|
||||
"answer": "我叫小盏,是一只中式茶盖碗,名字来源半盏新青年茶馆,一盏茶",
|
||||
}
|
||||
},
|
||||
]
|
||||
|
||||
cli = Client()
|
||||
|
||||
try:
|
||||
dataset = cli.read_dataset(dataset_name=DATASET_NAME)
|
||||
logger.info("read dataset")
|
||||
except:
|
||||
dataset = cli.create_dataset(dataset_name=DATASET_NAME)
|
||||
logger.info("created dataset")
|
||||
cli.create_examples(
|
||||
dataset_id=dataset.id,
|
||||
examples=examples
|
||||
)
|
||||
26
scripts/py_scripts/misc_tasks.py
Normal file
26
scripts/py_scripts/misc_tasks.py
Normal file
@@ -0,0 +1,26 @@
|
||||
from lang_agent.graphs import ReactGraphConfig, ReactGraph, RoutingConfig,RoutingGraph
|
||||
from lang_agent.pipeline import PipelineConfig
|
||||
from lang_agent.base import GraphBase
|
||||
|
||||
import os.path as osp
|
||||
import os
|
||||
from tqdm import tqdm
|
||||
import yaml
|
||||
import tyro
|
||||
|
||||
def gen_arch_imgs(save_dir="frontend/assets/images/graph_arch"):
|
||||
|
||||
save_dir = osp.join(osp.dirname(osp.dirname(__file__)), save_dir)
|
||||
confs:GraphBase = [ReactGraphConfig(), RoutingConfig()]
|
||||
for conf in tqdm(confs):
|
||||
graph:GraphBase = conf.setup()
|
||||
img = graph.show_graph(ret_img=True)
|
||||
img.save(osp.join(save_dir, f"arch_{conf.__class__.__name__}.png"))
|
||||
|
||||
def make_save_conf(pipeline_config:PipelineConfig, save_path:str):
|
||||
os.makedirs(osp.dirname(save_path), exist_ok=True)
|
||||
pipeline_config.save_config(save_path)
|
||||
|
||||
if __name__ == "__main__":
|
||||
# gen_arch_imgs()
|
||||
tyro.cli(make_save_conf)
|
||||
Reference in New Issue
Block a user