You are a software engineer with 10 years of experience in backend systems.
Review the following code and identify any issues that may affect performance. Focus on algorithmic complexity and resource utilization.
Context: This review is necessary due to a significant performance regression in our production environment. The system handles 1000 concurrent users and processes 1M requests per hour.
Code to review: [Insert the code here]
Format: Provide a structured review with: - Executive Summary (2-3 sentences) - Issues Found (bullet points with severity levels) - Recommended Actions (prioritized list) - Implementation Timeline (estimated effort)
Constraints: - Keep review objective and data-driven - Avoid speculation beyond provided code - Include code snippets for suggested improvements - Limit response to 500 words maximum
Input: What is the capital of Canada?
Output: The capital of Canada is Ottawa.Input/Output:
[
{'role': 'user', 'content': 'What is the capital of Canada?'},
{'role': 'assistant', 'content': 'The capital of Canada is Ottawa.'}
]
Role: You are a [expert] [role] Task: [clear instruction] Input: [data to be processed] Output: [format/structure]
Input: <Instruction> You are a helpful assistant. You will classify the text into negative or positive. <data> The weather is great today!
Output: PositiveInput/Output:
[
{'role': 'system', 'content': 'You are a helpful assistant. You will classify the text into negative or positive.'},
{'role': 'user', 'content': 'The weather is great today!'},
{'role': 'assistant', 'content': 'Positive'}
]
Input: <Instruction> You are a helpful assistant. You will extract entities from the text. <indicator> Definition: an entity is an organization, a person, or a location. <data> The capital of Canada is Ottawa.Output:
[
{'role': 'system', 'content': 'You are a helpful assistant. You will extract entities from the text.'},
{'role': 'system', 'content': 'Definition: an entity is an organization, a person, or a location.'},
{'role': 'user', 'content': 'The capital of Canada is Ottawa.'},
{'role': 'assistant', 'content': 'Ottawa - Location (capital of Canada)'}
]
Input: <question> What's the capital of Canada? <answer> Ottawa is the capital of Canada and is located in Ontario. <Instruction> You are a helpful assistant. Answer the following question? <data>: What's the capital of France?Input/Output:
[
{'role': 'system', 'content': 'You are a helpful assistant. Answer the following questions.'},
{'role': 'user', 'content': 'What is the capital of Canada?'},
{'role': 'assistant', 'content': 'Ottawa is the capital of Canada and is located in Ontario.'},
{'role': 'user', 'content': 'What is the capital of France?'},
{'role': 'assistant', 'content': 'Paris is the capital of France. It is not only the political center but also a major cultural and ...'}
]
Problem: I have five apples. If I ate one in the morning and three in the afternoon, how many apples do I have left? Let's solve this step by step: 1. Calculate the total number of apples eaten: 1 (morning) + 3 (afternoon) = 4 2. Subtract the total eaten from the original amount: 5 - 4 = 1 3. Answer: I have 1 apple left.
$ wget https://huggingface.co/microsoft/Phi-3-mini-4k-instruct-gguf/resolve/main/Phi-3-mini-4k-instruct-q4.ggufPython code:
$ vi prompt.py
from langchain_community.llms.llamacpp import LlamaCpp
from langchain_core.callbacks import CallbackManager, StreamingStdOutCallbackHandler
callback_manager = CallbackManager([StreamingStdOutCallbackHandler()])
llm = LlamaCpp(
model_path="./Phi-3-mini-4k-instruct-q4.gguf",
temperature=0,
max_tokens=50,
top_p=0,
callback_manager=callback_manager,
verbose=False
)
prompt = "What's 1+1?"
llm.invoke(prompt)
Run the Python script:$ python3 prompt.pyOutput:
<|assistant|> 1+1 equals 2.
$ vi instruction-prompt.py
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
model = AutoModelForCausalLM.from_pretrained("microsoft/Phi-4-mini-instruct")
tokenizer = AutoTokenizer.from_pretrained("microsoft/Phi-4-mini-instruct")
generator = pipeline(
"text-generation",
model=model,
tokenizer=tokenizer
)
generation_args = {
"max_new_tokens": 10,
"return_full_text": False,
"do_sample": False,
}
prompt = [
{"role": "system", "content": "You are a helpful assistant. You will answer questions in a concise and informative manner."},
{"role": "user", "content": "What's the capital of Canada?"}
]
output = generator(prompt, **generation_args)
print(output[0]["generated_text"]) # Ottawa
#prompt_template = generator.tokenizer.apply_chat_template(prompt, tokenize=False)
#print(prompt_template)
Run the Python script:$ python3 instruction-prompt.pyOutput:
OttawaIf we set the "return_full_text" parameter to "True", we can see the full chat text:
[
{'role': 'system', 'content': 'You are a helpful assistant. You will answer questions in a concise and informative manner.'},
{'role': 'user', 'content': "What's the capital of Canada?"},
{'role': 'assistant', 'content': 'Ottawa'}
]
If you uncomment these two lines in the code above, you will see the prompt template as created by the pipeline from the prompt:prompt_template = generator.tokenizer.apply_chat_template(prompt, tokenize=False) print(prompt_template)Output:
<|system|>You are a helpful assistant. You will answer questions in a concise and informative manner.<|end|> <|user|>What's the capital of Canada?<|end|> <|endoftext|>Note the special tokens: |system|, |user|, |assistant|
<|system|>: Start of guidelines You are a helpful assistant. You will answer questions in a concise and informative manner.: guidelines <|end|>: end of guidelines
<|user|>: Start of prompt What's the capital of Canada?: prompt <|end|>: end of prompt
<|assistant|>: start of output Ottawa: output <|end|>: end of output
<|endoftext|>: end of the model output
$ vi chain-prompt.py
from langchain_community.llms.llamacpp import LlamaCpp
from langchain_core.callbacks import CallbackManager, StreamingStdOutCallbackHandler
from langchain_core.prompts import PromptTemplate
template = """Question: {question}
Answer: Explain how you arrived at the correct answer."""
prompt = PromptTemplate.from_template(template)
callback_manager = CallbackManager([StreamingStdOutCallbackHandler()])
llm = LlamaCpp(
model_path="./Phi-3-mini-4k-instruct-q4.gguf",
temperature=0,
max_tokens=100,
top_p=0,
callback_manager=callback_manager,
verbose=False
)
llm_chain = prompt | llm
llm_chain.invoke({"question": "What's 1+1?"})
Run the Python script:$ python3 chain-prompt.pyOutput:
<|assistant|> To arrive at the correct answer for 1+1, we start with the number 1 and add another 1 to it. When you combine one unit with another unit, you get a total of two units. Therefore, 1 + 1 equals 2. This is based on basic arithmetic addition where combining quantities results in their sum. ```
$ vi multiple-chain-prompt.py
from langchain_community.llms.llamacpp import LlamaCpp
from langchain_core.callbacks import CallbackManager, StreamingStdOutCallbackHandler
from langchain_core.prompts import PromptTemplate
callback_manager = CallbackManager([StreamingStdOutCallbackHandler()])
llm = LlamaCpp(
model_path="./Phi-3-mini-4k-instruct-q4.gguf",
temperature=0,
max_tokens=100,
top_p=0,
callback_manager=callback_manager,
verbose=False
)
sentiment_template = """<|user|>
Analyze the following sentence whether it is positive or negative {sentence}.<|end|>
<|assistant|>"""
sentiment_prompt = PromptTemplate(
template=sentiment_template,
input_variables=["sentence"]
)
sentiment_llmchain = sentiment_prompt | llm
sentiment_refined_template = """<|user|>
If the sentiment of the sentence is negative, rewrite the sentence {sentence} to sound positive.<|end|>
<|assistant|>"""
sentiment_refined_prompt = PromptTemplate(
template=sentiment_refined_template,
input_variables=["sentence"]
)
sentiment_refined_llmchain = sentiment_refined_prompt | llm
llm_chain = sentiment_llmchain | sentiment_refined_llmchain
llm_chain.invoke("Not a good day today. I don't feel like going out!")
Run the Python script:$ python3 multiple-chain-prompt.pyOutput:
The sentence "Not a good day today. I don't feel like going out!" is negative. It expresses dissatisfaction with the current day and a lack of desire to engage in social activities, indicating an overall unfavorable mood or sentiment. Despite today not being as vibrant as I'd hoped, it presents a perfect opportunity for some cozy indoor activities that I truly enjoy!