add gpt-5 with poe
Browse files586 unmodified lines587588589590591592593594595103 unmodified lines6997007017027037047057067077083 unmodified lines7127137147157167171782 unmodified lines25002501250225032504250525062507250825092510251125123 unmodified lines25162517251825192520252118 unmodified lines2540254125422543254425452546586 unmodified linesbase_url="https://dashscope.aliyuncs.com/compatible-mode/v1",)elif model_id == "gpt-5":# Use OpenAI client for GPT-5 modelreturn OpenAI(api_key=os.getenv("OPENAI_API_KEY"),)elif model_id == "step-3":# Use StepFun API client for Step-3 model103 unmodified lines# Remove a leading language marker line (e.g., 'python') if presentif extracted.split('\n', 1)[0].strip().lower() in ['python', 'html', 'css', 'javascript', 'json', 'c', 'cpp', 'markdown', 'latex', 'jinja2', 'typescript', 'yaml', 'dockerfile', 'shell', 'r', 'sql', 'sql-mssql', 'sql-mysql', 'sql-mariadb', 'sql-sqlite', 'sql-cassandra', 'sql-plSQL', 'sql-hive', 'sql-pgsql', 'sql-gql', 'sql-gpsql', 'sql-sparksql', 'sql-esper']:return extracted.split('\n', 1)[1] if '\n' in extracted else ''return extracted# If no code block is found, check if the entire text is HTMLif text.strip().startswith('<!DOCTYPE html>') or text.strip().startswith('<html') or text.strip().startswith('<'):return text.strip()# Special handling for python: remove python markerif text.strip().startswith('```python'):return text.strip()[9:-3].strip()3 unmodified linesreturn lines[1] if len(lines) > 1 else ''return text.strip()def parse_transformers_js_output(text):"""Parse transformers.js output and extract the three files (index.html, index.js, style.css)"""files = {1782 unmodified lines)else:# Use max_completion_tokens for GPT-5, max_tokens for othersif _current_model["id"] == "gpt-5":completion = client.chat.completions.create(model=_current_model["id"],messages=messages,stream=True,max_completion_tokens=16384)else:completion = client.chat.completions.create(3 unmodified linesmax_tokens=16384)content = ""for chunk in completion:# Handle different response formats for Mistral vs otherschunk_content = None18 unmodified lineschunk_content = chunk.choices[0].delta.contentif chunk_content:content += chunk_contentsearch_status = " (with web search)" if enable_search and tavily_client else ""# Handle transformers.js output differently586 unmodified lines587588589590591592593594595596103 unmodified lines7007017027037047057067077087097107117127137147157167177187197207217227233 unmodified lines7277287297307317327337347357367377387391782 unmodified lines25222523252425252526252725282529253025312532253325343 unmodified lines25382539254025412542254325442545254618 unmodified lines256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606586 unmodified linesbase_url="https://dashscope.aliyuncs.com/compatible-mode/v1",)elif model_id == "gpt-5":# Use Poe (OpenAI-compatible) client for GPT-5 modelreturn OpenAI(api_key=os.getenv("POE_API_KEY"),base_url="https://api.poe.com/v1")elif model_id == "step-3":# Use StepFun API client for Step-3 model103 unmodified lines# Remove a leading language marker line (e.g., 'python') if presentif extracted.split('\n', 1)[0].strip().lower() in ['python', 'html', 'css', 'javascript', 'json', 'c', 'cpp', 'markdown', 'latex', 'jinja2', 'typescript', 'yaml', 'dockerfile', 'shell', 'r', 'sql', 'sql-mssql', 'sql-mysql', 'sql-mariadb', 'sql-sqlite', 'sql-cassandra', 'sql-plSQL', 'sql-hive', 'sql-pgsql', 'sql-gql', 'sql-gpsql', 'sql-sparksql', 'sql-esper']:return extracted.split('\n', 1)[1] if '\n' in extracted else ''# If HTML markup starts later in the block (e.g., Poe injected preface), trim to first HTML roothtml_root_idx = Nonefor tag in ['<!DOCTYPE html', '<html']:idx = extracted.find(tag)if idx != -1:html_root_idx = idx if html_root_idx is None else min(html_root_idx, idx)if html_root_idx is not None and html_root_idx > 0:return extracted[html_root_idx:].strip()return extracted# If no code block is found, check if the entire text is HTMLstripped = text.strip()if stripped.startswith('<!DOCTYPE html>') or stripped.startswith('<html') or stripped.startswith('<'):# If HTML root appears later (e.g., Poe preface), trim to first HTML rootfor tag in ['<!DOCTYPE html', '<html']:idx = stripped.find(tag)if idx > 0:return stripped[idx:].strip()return stripped# Special handling for python: remove python markerif text.strip().startswith('```python'):return text.strip()[9:-3].strip()3 unmodified linesreturn lines[1] if len(lines) > 1 else ''return text.strip()def strip_placeholder_thinking(text: str) -> str:"""Remove placeholder 'Thinking...' status lines from streamed text."""if not text:return text# Matches lines like: "Thinking..." or "Thinking... (12s elapsed)"return re.sub(r"(?mi)^[\t ]*Thinking\.\.\.(?:\s*\(\d+s elapsed\))?[\t ]*$\n?", "", text)def parse_transformers_js_output(text):"""Parse transformers.js output and extract the three files (index.html, index.js, style.css)"""files = {1782 unmodified lines)else:# Poe expects model id "GPT-5" and uses max_tokensif _current_model["id"] == "gpt-5":completion = client.chat.completions.create(model="GPT-5",messages=messages,stream=True,max_tokens=16384)else:completion = client.chat.completions.create(3 unmodified linesmax_tokens=16384)content = ""# For Poe/GPT-5, maintain a simple code-fence state machine to only accumulate codepoe_inside_code_block = Falsepoe_partial_buffer = ""for chunk in completion:# Handle different response formats for Mistral vs otherschunk_content = None18 unmodified lineschunk_content = chunk.choices[0].delta.contentif chunk_content:if _current_model["id"] == "gpt-5":# Filter placeholdersincoming = strip_placeholder_thinking(chunk_content)# Process code fences incrementally, only keep content inside fencess = poe_partial_buffer + incomingappend_text = ""i = 0# Find all triple backticks positionsfor m in re.finditer(r"```", s):if not poe_inside_code_block:# Opening fence. Require a newline to confirm full opener so we can skip optional language linenl = s.find("\n", m.end())if nl == -1:# Incomplete opener; buffer from this fence and wait for morepoe_partial_buffer = s[m.start():]s = Nonebreak# Enter code, skip past newline after optional language tokenpoe_inside_code_block = Truei = nl + 1else:# Closing fence, append content inside and exit codeappend_text += s[i:m.start()]poe_inside_code_block = Falsei = m.end()if s is not None:if poe_inside_code_block:append_text += s[i:]poe_partial_buffer = ""else:poe_partial_buffer = s[i:]if append_text:content += append_textelse:# Append content, filtering out placeholder thinking linescontent += strip_placeholder_thinking(chunk_content)search_status = " (with web search)" if enable_search and tavily_client else ""# Handle transformers.js output differently
app.py+68-8