27 lines
908 B
Python
27 lines
908 B
Python
|
|
from langchain_community.callbacks import get_openai_callback
|
|
from langchain_core.prompts import FewShotChatMessagePromptTemplate, PromptTemplate
|
|
from langchain_openai import ChatOpenAI
|
|
import os
|
|
import dotenv
|
|
|
|
dotenv.load_dotenv()
|
|
|
|
## 设置环境变量
|
|
os.environ['OPENAI_API_KEY'] = os.getenv("SILICONFLOW_API_KEY")
|
|
os.environ['OPENAI_BASE_URL'] = os.getenv("SILICONFLOW_BASE_URL")
|
|
|
|
# 默认的 'model_name': 'deepseek-ai/DeepSeek-V3.1',
|
|
llm = ChatOpenAI(model="deepseek-ai/DeepSeek-R1-0528-Qwen3-8B")
|
|
template = PromptTemplate.from_template("给我讲一个关于{topic}的冷笑话")
|
|
chain = template | llm
|
|
|
|
with get_openai_callback() as cb:
|
|
response = chain.invoke({"topic":"特朗普"})
|
|
print(response)
|
|
|
|
print("------")
|
|
print(f"total_tokens:{cb.total_tokens}")
|
|
print(f"prompt_tokens :{cb.prompt_tokens}")
|
|
print(f"completion_tokens:{cb.completion_tokens}")
|
|
print(f"total_cost:{cb.total_cost}") |