Tối ưu hóa hiệu suất xích LangChain

Tối ưu hóa hiệu suất xích LangChain

Tối ưu hóa hiệu suất là then chốt cho đảm bảo rằng các xích hoạt động hiệu quả, đặc biệt khi làm việc với dữ liệu
quy mô lớn hay các ứng dụng thời gian thực. Sau đây là một vài chiến lược cho tối ưu hóa xích:
1. Caching: Thực thi cơ chế bộ nhớ đệm cho được sử dụng thường xuyên hay các kết quả tổn phí tính toán. Cái này
có thể giảm đáng kể thời gian xử lí cho các hoạt động lặp lại.

from langchain.cache import InMemoryCache
from langchain.globals import set_llm_cache
# Set up in-memory cache
set_llm_cache(InMemoryCache())
# Now any calls to LLMs will be cached

Bộ nhớ đệm lưu giữ các kết quả của các hoạt động phí tổn, như các lời gọi LLM. Khi cùng hoạt động được yêu cầu
một lần nữa, kết quả ghi vào bộ nhớ đệm được trả về, tiết kiệm thời gian và tài nguyên. Code phụ thuộc vào cả lõi
langchain và các gói Python langchain đang được cài đặt trong môi trường của bạn. Nếu những cái này không được cài
đặt, code có thể thất bại vì thiếu các dependencies.
2. Đánh giá độ lười: Thiết kế các xích để tính toán các kết quả chỉ khi chúng được cần, hơn là xử lí háo hức tất
cả các kết quả có thể.

# Lazy evaluation
class LazyChain:

def __init__(self, llm, prompt_template):

self.llm = llm
self.prompt_template = prompt_template # Store as prompt_template
self._result = None

def get_result(self, input_text: str) -> str:

if self._result is None:

# Create PromptTemplate instance here
prompt = PromptTemplate.from_template(self.prompt_template)
chain = LLMChain(llm=self.llm, prompt=prompt)
self._result = chain.run(input_text)

return self._result

# Example usage
lazy_chain = LazyChain(llm, “Analyze: {text}”)
result = lazy_chain.get_result(“How is AI shaping the modern world”)
print(“result:\n”, result)
output:
Artificial intelligence (AI) is quickly shaping the modern world in a variety of ways. From revolutionizing industries to changing the way we live and work, AI has become an integral part of our daily lives. Here are some of the ways in which AI is shaping the modern world:
1. Automation and efficiency: One of the key ways in which AI is shaping the modern world is through automation and efficiency.
2. Personalization: AI is also shaping the modern world by personalizing experiences for individuals. This has led to improved customer satisfaction and loyalty.
3. Improving healthcare: AI is playing a crucial role in improving healthcare by accurately diagnosing diseases, assisting in surgeries, and developing personalized treatment plans.

Code này thực thi đánh giá độ lười cho một xích mô hình ngôn ngữ, hoãn tính toán thực cho đến khi get_result()
được gọi và ghi vào bộ nhớ đệm kết quả để tránh xử lí thừa.
3. Phân bổ tài nguyên: Quản lí cẩn thận các tài nguyên như bộ nhớ và sử dụng CPU, đặc biệt cho các bộ phận cái yêu
cầu năng lượng tính toán đáng kể.

from langchain.chat_models import ChatOpenAI
llm = ChatOpenAI(

max_tokens=1000, # Limit token usage
request_timeout=30 # Set timeout for requests

)

Chia sẻ