File size: 1,800 Bytes
8f5c845
 
e0d7f28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8f5c845
e0d7f28
 
8f5c845
 
 
 
 
 
e0d7f28
 
 
8f5c845
 
 
 
e0d7f28
8f5c845
 
 
 
 
 
 
 
 
e0d7f28
 
8f5c845
e0d7f28
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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")
    )