ErrorMitigation / src /models /llm_interface.py
Dvirmor100's picture
style: using black and ruff to format the project
e0d7f28
Raw History Blame Contribute Delete
1.8 kB
from openai import OpenAI
def chat_completion(
user_prompt: str,
model_name: str = "phi4",
temperature: float = 0.01,
top_p: float = 0.99,
):
"""
Generates a response from a language model specialized in quantum computing.
Args:
user_prompt (str): The input prompt provided by the user.
model_name (str, optional):
The name of the language model to use. Defaults to "phi4".
temperature (float, optional):
The sampling temperature for response generation,
controlling randomness. Defaults to 0.01.
top_p (float, optional):
The nucleus sampling parameter, determining the
probability mass for token selection. Defaults to 0.99.
Returns:
str: The generated response from the language model.
"""
client = OpenAI(
base_url="http://localhost:11434/v1",
api_key="ollama", # Required but ignored
)
response = client.chat.completions.create(
messages=[
{"role": "system", "content": "You are a quantum computing expert."},
{
"role": "user",
"content": user_prompt,
},
],
temperature=temperature,
top_p=top_p,
model=model_name,
stream=True, # Stream for prevent connection timeout for long response requests
)
final_answer = ""
for chunk in response:
if chunk.choices and chunk.choices[0].delta:
final_answer += chunk.choices[0].delta.content
return final_answer
if __name__ == "__main__":
print(chat_completion(user_prompt="Who are you in a sentence", model_name="phi4"))
print(
chat_completion(user_prompt="Who are you in sentence", model_name="qwen2.5:14b")
)