实验追踪(Tracing)
实验追踪(Tracing)
Langchain
环境准备
langchain-openai==0.1.8
自动记录
import os
os.environ["OPENAI_API_BASE"] = "<你的 OPENAI API BASE URL>"
os.environ["OPENAI_API_KEY"] = "<你的 OPENAI API KEY>"
model_name = "<模型名称>"
from langchain.prompts import PromptTemplate
from langchain_openai import ChatOpenAI
import mlflow
assert (
"OPENAI_API_KEY" in os.environ
), "Please set your OPENAI_API_KEY environment variable."
# 创建实验
mlflow.set_experiment("LangChain Tracing")
# 开启 LangChain 自动记录
mlflow.langchain.autolog(log_models=True, log_input_examples=True)
llm = ChatOpenAI(model=model_name, temperature=0.7, max_tokens=1000)
prompt_template = (
"Imagine that you are {person}, and you are embodying their manner of answering questions posed to them. "
"While answering, attempt to mirror their conversational style, their wit, and the habits of their speech "
"and prose. You will emulate them as best that you can, attempting to distill their quirks, personality, "
"and habits of engagement to the best of your ability. Feel free to fully embrace their personality, whether "
"aspects of it are not guaranteed to be productive or entirely constructive or inoffensive."
"The question you are asked, to which you will reply as that person, is: {question}"
)
prompt = PromptTemplate.from_template(prompt_template)
chain = prompt | llm
# 测试
chain.invoke(
{
"person": "Richard Feynman",
"question": "Why should we colonize Mars instead of Venus?",
}
)
chain.invoke(
{
"person": "Linus Torvalds",
"question": "Can I just set everyone's access to sudo to make things easier?",
}
)
查看记录
在 模型/实验
中找到 LangChain Tracing
,点击 追踪
查看追踪结果。
- 请求 ID:
MLflow
生成的唯一请求 ID; - 状态:请求状态,
OK
orError
; - 请求:API 调用的请求参数;
- 响应:API 调用的响应数据;
- 执行时间:API 调用时间;
- 标签:可以为请求添加标签便于后续分析。

点击 请求ID
可以进一步查看追踪(Tracing)过程中的调用情况。

OpenAI
自动记录
import os
import openai
import mlflow
os.environ["OPENAI_API_BASE"] = "<你的 OPENAI API BASE URL>"
os.environ["OPENAI_API_KEY"] = "<你的 OPENAI API KEY>"
model_name = "<模型名称>"
# 创建实验
mlflow.set_experiment("OpenAI")
# 开启 openai 自动记录
mlflow.openai.autolog(log_models=True, log_input_examples=True)
client = openai.OpenAI(api_key=os.environ.get("OPENAI_API_KEY"), base_url=os.environ.get("OPENAI_API_BASE"))
response = client.chat.completions.create(
model=model_name,
messages=[
{"role": "system", "content": "You are a helpful assistant"},
{"role": "user", "content": "How can I improve my resting metabolic rate most effectively?"},
],
stream=False
)
print(response.choices[0].message.content)
输出
### 1. **Build Muscle Mass**
- **Strength Training:** Incorporate regular strength training exercises (e.g., weight lifting, resistance bands) to build muscle mass. Muscle tissue burns more calories at rest than fat tissue.
- **High-Intensity Interval Training (HIIT):** HIIT can help increase muscle mass and boost metabolism post-exercise (EPOC).
### 2. **Increase Physical Activity**
- **Aerobic Exercise:** Regular aerobic exercise (e.g., running, cycling, swimming) can help increase your overall calorie burn.
- **Daily Movement:** Incorporate more movement into your daily routine, such as walking, taking the stairs, or doing household chores.
### 3. **Optimize Nutrition**
- **Protein Intake:** Consume adequate protein (e.g., lean meats, fish, eggs, dairy, legumes) to support muscle growth and maintenance.
- **Balanced Diet:** Ensure a balanced diet with adequate vitamins, minerals, and macronutrients to support overall health and metabolism.
- **Hydration:** Stay well-hydrated, as dehydration can slow down metabolism.
### 4. **Get Quality Sleep**
- **Sleep Duration:** Aim for 7-9 hours of quality sleep per night. Poor sleep can negatively impact metabolism.
- **Sleep Quality:** Create a sleep-friendly environment (e.g., cool, dark, quiet) and establish a consistent sleep schedule.
### 5. **Manage Stress**
- **Stress Reduction:** Chronic stress can lead to increased cortisol levels, which can negatively affect metabolism. Practice stress-reducing techniques such as meditation, yoga, or deep breathing exercises.
### 6. **Avoid Crash Diets**
- **Sustainable Eating:** Avoid extreme calorie restriction, as it can lead to muscle loss and a slower metabolism. Focus on sustainable, balanced eating habits.
### 7. **Consider Hormonal Factors**
- **Thyroid Health:** Ensure your thyroid function is optimal, as thyroid hormones play a significant role in metabolism. Consult a healthcare provider if you suspect thyroid issues.
### 8. **Stay Consistent**
- **Long-Term Commitment:** Consistency is key. Regular exercise, balanced nutrition, and healthy lifestyle habits will help sustain an improved RMR over time.
By incorporating these strategies, you can effectively improve your resting metabolic rate and support a healthier, more energetic lifestyle.
查看记录
在 模型/实验
中找到 OpenAI
,该实验记录了一条运行,包含记录的模型以及工件信息。

点击 追踪
查看追踪结果,追踪
中详细记录了模型的输入输出信息。
