File size: 2,914 Bytes
d1da8fd
145385b
 
d1da8fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145385b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d1da8fd
 
 
145385b
 
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import json
import os
import time

from openai import OpenAI

# Initialize the OpenAI client with the local server
client = OpenAI(
    base_url="http://localhost:8000/v1",
    api_key="not-needed",  # API key is not needed for local server
)


def test_chat_completion():
    try:
        print("Sending chat completion request...")
        response = client.chat.completions.create(
            model="unsloth/SmolLM2-135M-Instruct-bnb-4bit",
            messages=[{"role": "user", "content": "Hello"}],
            temperature=0.7,
            max_tokens=50,
        )

        # Print the response
        print("\nResponse:")
        print(response.choices[0].message.content)

        # Print full response object for debugging
        print("\nFull response object:")
        print(json.dumps(response.model_dump(), indent=2))

    except Exception as e:
        print(f"Error occurred: {str(e)}")
        import traceback

        print("\nFull traceback:")
        print(traceback.format_exc())


def test_fine_tuning():
    try:
        # Create a sample training file
        training_data = {
            "conversations": [
                {
                    "from": "human",
                    "value": "What is the capital of France?",
                },
                {
                    "from": "gpt",
                    "value": "The capital of France is Paris.",
                },
            ]
        }

        training_file = "training_data.json"
        with open(training_file, "w") as f:
            json.dump(training_data, f)

        print("\nCreating fine-tuning job...")
        job = client.fine_tuning.jobs.create(
            training_file=training_file,
            model="unsloth/SmolLM2-135M-Instruct-bnb-4bit",
        )
        print(f"Created job: {job.id}")

        # Wait for job to start
        print("\nWaiting for job to start...")
        time.sleep(2)

        # List jobs
        print("\nListing fine-tuning jobs...")
        jobs = client.fine_tuning.jobs.list()
        print(f"Found {len(jobs.data)} jobs")

        # Get job status
        print("\nGetting job status...")
        job = client.fine_tuning.jobs.retrieve(job.id)
        print(f"Job status: {job.status}")

        # Wait for job to complete or fail
        print("\nWaiting for job to complete...")
        while job.status in ["created", "running"]:
            time.sleep(5)
            job = client.fine_tuning.jobs.retrieve(job.id)
            print(f"Job status: {job.status}")

        # Clean up
        os.remove(training_file)

    except Exception as e:
        print(f"Error occurred: {str(e)}")
        import traceback

        print("\nFull traceback:")
        print(traceback.format_exc())


if __name__ == "__main__":
    print("Testing chat completions endpoint...")
    test_chat_completion()

    print("\nTesting fine-tuning endpoints...")
    test_fine_tuning()