Spaces:
Runtime error
Runtime error
Download src/models/llm_interface.py from QuantumGenAI/ErrorMitigation: direct link, hf CLI and curl.
- Browser
- Download file 1.8 kB
-
https://huggingface.co/spaces/QuantumGenAI/ErrorMitigation/resolve/main/src/models/llm_interface.py
- Command line
-
hf download hf://spaces/QuantumGenAI/ErrorMitigation/src/models/llm_interface.py
-
curl -L -o llm_interface.py https://huggingface.co/spaces/QuantumGenAI/ErrorMitigation/resolve/main/src/models/llm_interface.py
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") | |
| ) | |