-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlmstudio_node.py
More file actions
121 lines (110 loc) · 4.21 KB
/
Copy pathlmstudio_node.py
File metadata and controls
121 lines (110 loc) · 4.21 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import base64
import random as r
import time
from io import BytesIO
from typing import Optional
import requests
from invokeai.invocation_api import (
BaseInvocation,
ImageField,
InputField,
InvocationContext,
invocation,
)
from invokeai.app.invocations.primitives import StringOutput
@invocation(
"openai_assistant",
title="LM Studio API Assistant",
tags=["text", "prompt", "lmstudio", "api", "assistant"],
version="1.0.4",
)
class OpenAIAssistantInvocation(BaseInvocation):
"""LM Studio API Assistant Prompt Generator node"""
lmstudioContext: str = InputField(
default=(
"fill the details in the stable diffusion prompt the user enters. "
"Please be as detailed as possible. Don't ask the user for information, "
"but fill in the blanks for him. Be creative! User prompt :"
),
description="Context template",
)
prompt: str = InputField(default="", description="User prompt")
model: str = InputField(
default="MaziyarPanahi/WizardLM-2-7B-GGUF", description="Model name"
)
image: Optional[ImageField] = InputField(
default=None, description="Optional image input"
)
image_prompt: Optional[str] = InputField(
default=(
"Describe this image in a very detailed and intricate way, "
"as if you were describing it to a blind person for accessibility."
),
description="Instruction for image description",
)
max_tokens: int = InputField(default=2048, description="Max tokens")
temperature: float = InputField(default=0.8, description="Temperature")
seed: int = InputField(default=-1, description="Random seed")
trigger: int = InputField(default=0, description="Reinvoke trigger")
HOST: str = InputField(default="localhost:1234", description="Host:port")
def invoke(self, context: InvocationContext) -> StringOutput:
# Seed RNG
if self.seed >= 0:
r.seed(self.seed)
else:
r.seed()
# Optional delay
time.sleep(3 + r.randint(0, 5))
# Build messages
history = [
{
"role": "system",
"content": (
"You are an intelligent assistant. Provide well-reasoned, "
"correct, and helpful answers."
),
},
{"role": "user", "content": f"{self.lmstudioContext}\n{self.prompt}\n"},
]
# Embed image if provided
if self.image:
try:
pil_image = context.images.get_pil(self.image.image_name)
buf = BytesIO()
pil_image.save(buf, format="JPEG")
b64 = base64.b64encode(buf.getvalue()).decode("utf-8")
history.append(
{
"role": "user",
"content": [
{"type": "text", "text": self.image_prompt},
{
"type": "image_url",
"image_url": {"url": f"data:image/jpeg;base64,{b64}"},
},
],
}
)
except Exception as e:
print(f"Image error: {e}")
return StringOutput(value="Error processing the image")
# Prepare request
url = f"http://{self.HOST}/v1/chat/completions"
headers = {"Content-Type": "application/json"}
payload = {
"model": self.model,
"messages": history,
"temperature": self.temperature,
"max_tokens": self.max_tokens,
"seed": self.seed,
}
# Send HTTP request directly
try:
resp = requests.post(url, headers=headers, json=payload, timeout=60)
resp.raise_for_status()
data = resp.json()
prompt = data["choices"][0]["message"]["content"]
except Exception as e:
print(f"HTTP error: {e}")
prompt = "Error in API call"
return StringOutput(value=prompt)