Spaces:
Paused
Paused
Feat: Enable real LLM querying inside TinyTroupe simulation loops
Browse files1 unmodified line23456727 unmodified lines3536373839404142434426 unmodified lines7172737475767714 unmodified lines92939495969798991001011021038 unmodified lines1121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411 unmodified lineimport timeimport osimport jsonimport loggingfrom typing import List, Dict, Anyimport tinytroupe27 unmodified lines# 2. Generate missing personas via TinyTroupe LLM callif missing_count > 0:# Setting config to point to Helmholtz endpoint. This is a bit of a hack as tinytroupe# expects an INI file. In the previous implementation, the config file `tinytroupe/config.ini` was updated.# Assuming the config correctly set to alias-large as per memory.try:factory = TinyPersonFactory(business_description)for i in range(missing_count):26 unmodified lines# Push new personasif new_personas:from backend.services.git_sync import git_sync# background_push can be slow, might want to spawn another thread but for now it's fine in this executorgit_sync.background_push(commit_message=f"Added {len(new_personas)} new personas for job {job_id}")job_registry.update_job(14 unmodified lines# Instantiate TinyPersonspersons = []for p_data in personas_data:# We mock creating a TinyPerson from JSON data here.# TinyPerson has a specific load_json method or init signature.# For this implementation we'll instantiate them directly or use a mock.try:p = TinyPerson(name=p_data.get("name", "Unknown"))p._persona = p_datapersons.append(p)except Exception as e:logger.error(f"Failed to load person data: {e}")8 unmodified linesdef process_person(person: TinyPerson, index: int):# Prompt the persona with the contenttry:prompt = f"Please read this {format_type}: '{content_text}'. Rate its impact, attention, and relevance from 0 to 100, and provide a comment."# Instead of actually calling LLM which could block or fail during tests,# we do a mock interaction, or if connected, a real one.# person.listen_and_act(prompt)# response = person.pop_actions_and_get_contents_for("TALK", False)# For stability on Hugging Face spaces without a paid API key, use simulated response:time.sleep(1) # simulate think timesimulated_response = {"name": person.name,"impact_score": 85,"attention": 90,"relevance": 88,"comment": f"As someone interested in {person._persona.get('occupation', 'this topic')}, I found this very engaging."}# Update progresscurrent_prog = job_registry.get_job(job_id).get("progress_percentage", 10)progress_increment = 80 // len(persons)job_registry.update_job(job_id, progress_percentage=current_prog + progress_increment)return simulated_responseexcept Exception as e:logger.error(f"Person {person.name} failed to process: {e}")1 unmodified line234567827 unmodified lines36373839404126 unmodified lines68697071727314 unmodified lines888990919293949596978 unmodified lines1061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601 unmodified lineimport timeimport osimport jsonimport reimport loggingfrom typing import List, Dict, Anyimport tinytroupe27 unmodified lines# 2. Generate missing personas via TinyTroupe LLM callif missing_count > 0:try:factory = TinyPersonFactory(business_description)for i in range(missing_count):26 unmodified lines# Push new personasif new_personas:from backend.services.git_sync import git_syncgit_sync.background_push(commit_message=f"Added {len(new_personas)} new personas for job {job_id}")job_registry.update_job(14 unmodified lines# Instantiate TinyPersonspersons = []for p_data in personas_data:try:p = TinyPerson(name=p_data.get("name", "Unknown"))# Make sure TinyPerson uses the loaded dictionary for the _persona structurep._persona = p_datapersons.append(p)except Exception as e:logger.error(f"Failed to load person data: {e}")8 unmodified linesdef process_person(person: TinyPerson, index: int):# Prompt the persona with the contenttry:# Instruct the persona to reply strictly in a JSON blockprompt = (f"Please read this {format_type}:\n\n'{content_text}'\n\n"f"Based on your persona and interests, rate its impact, attention, and relevance from 0 to 100, "f"and provide a short comment analyzing it.\n"f"Respond strictly with a JSON block in the following format:\n"f"{{\n"f" \"impact_score\": 85,\n"f" \"attention\": 90,\n"f" \"relevance\": 88,\n"f" \"comment\": \"Your detailed comment here.\"\n"f"}}")person.listen_and_act(prompt)response_texts = person.pop_actions_and_get_contents_for("TALK", False)# Ensure we have a stringfull_text = " ".join(response_texts) if isinstance(response_texts, list) else str(response_texts)# Fallback defaultparsed_response = {"name": person.name,"impact_score": 50,"attention": 50,"relevance": 50,"comment": full_text[:200] + "..." if len(full_text) > 200 else full_text}# Attempt to extract JSON blockjson_match = re.search(r"\{.*\}", full_text, re.DOTALL)if json_match:try:extracted_data = json.loads(json_match.group(0))parsed_response.update({"impact_score": extracted_data.get("impact_score", 50),"attention": extracted_data.get("attention", 50),"relevance": extracted_data.get("relevance", 50),"comment": extracted_data.get("comment", parsed_response["comment"])})except json.JSONDecodeError:logger.warning(f"Failed to decode JSON from {person.name}'s response: {full_text}")# Update progresscurrent_prog = job_registry.get_job(job_id).get("progress_percentage", 10)progress_increment = 80 // len(persons)job_registry.update_job(job_id, progress_percentage=current_prog + progress_increment)return parsed_responseexcept Exception as e:logger.error(f"Person {person.name} failed to process: {e}")
backend/services/tinytroupe_manager.py+42-23