File size: 3,846 Bytes
cf7f643
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from src.clients.llm_client import LLMClient
import json 
import pandas as pd
from pydantic import BaseModel, ValidationError

from functools import cache
from typing import List
from datetime import datetime
import pytz

def _ask_raw_hf(messages, model, response_format=None):
    """Compatibility wrapper: routes OpenAI-style messages through HF LLMClient."""
    from src.clients.llm_client import LLMClient
    import json as _json

    client = LLMClient()
    system_prompt = None
    user_text = ""
    images = []
    for msg in messages:
        role = msg.get("role", "")
        c = msg.get("content", "")
        if role == "system":
            if isinstance(c, str):
                system_prompt = c
        elif role == "user":
            if isinstance(c, str):
                user_text = c
            elif isinstance(c, list):
                for part in c:
                    if isinstance(part, dict):
                        if part.get("type") == "text":
                            user_text += part.get("text", "")
                        elif part.get("type") == "image_url":
                            url = part.get("image_url", {}).get("url", "")
                            if url.startswith("data:"):
                                images.append(url.split(",", 1)[1] if "," in url else url)
                            else:
                                images.append(url)

    if response_format is not None and hasattr(response_format, "model_json_schema"):
        result = client.call(
            prompt=user_text,
            schema=response_format,
            model=model,
            system_prompt=system_prompt,
            images=images if images else None,
            temperature=0,
        )
        return _json.dumps(result.model_dump(), ensure_ascii=False)
    else:
        return client.call_raw(
            prompt=user_text,
            model=model,
            system_prompt=system_prompt,
            images=images if images else None,
        )

   
class newHTMLs(BaseModel):
    HTMLs: list[str]

@cache
def modifyHTML(p, openai_key=os.environ.get('OPENAI_KEY')):
    """
    input0 (text): ボタンの斁E��を通信業界に変えて。色は若老E��け�EぁE��グラチE�Eションで、サブコピ�Eもバイブスあがる感じに。影をつけて、�Eタンは丸くして. <button class="c-button" style="appearance:none;border:0;border-radius:5px;background:#4676D7;color:#fff;padding:8px 16px;font-size:16px;">申し込む</button> <button class="c-button" style="appearance:none;border:0;border-radius:5px;background:#4676D7;color:#fff;padding:8px 16px;font-size:16px;">申し込まなぁE/button>
    input1 (text): default
    output1 (json): 修正したボタン
    """
    start_time = datetime.now(pytz.timezone('Asia/Tokyo')).strftime("%Y-%m-%d %H:%M:%S")
    print(start_time, f"modifyButton[{len(p)}]")

    if openai_key == "default":
        os.environ['OPENAI_API_KEY'] = os.environ.get('OPENAI_KEY')
    else:
        os.environ['OPENAI_API_KEY'] = openai_key

    client = LLMClient()
    
    response = _ask_raw_hf([{"role":"user","content":p}], model,
        model='meta-llama/Llama-3.3-70B-Instruct',
        messages=[
            {
                "role": "system",
                "content": """あなた�EHTMLとCSSの達人です、E
持E��された個数�E�なければ10個)�EHTMLコンポ�Eネントを提案してください、E
位置はこちらで決めるのでposition: absoluteやtransformは使わなぁE��ください、E
"""
            },
            {
                "role": "user",
                "content": p
            },
        ],
        response_format=newHTMLs,
        top_p=1,
        frequency_penalty=0,
        presence_penalty=0
    )
    
    return response