DeepSeekService.php 722 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943
  1. <?php
  2. namespace App\Services\DeepSeek;
  3. use App\Consts\ErrorConst;
  4. use App\Consts\BaseConst;
  5. use App\Facade\Site;
  6. use App\Libs\Utils;
  7. use GuzzleHttp\Client;
  8. use DateTime;
  9. use DateTimeZone;
  10. use App\Services\AIGeneration\AIImageGenerationService;
  11. use Illuminate\Support\Facades\DB;
  12. use Illuminate\Support\Facades\Log;
  13. use Illuminate\Support\Facades\Redis;
  14. use OSS\Core\OssException;
  15. use OSS\OssClient;
  16. use Smalot\PdfParser\Parser as PdfParser;
  17. use TCPDF;
  18. use PhpOffice\PhpWord\IOFactory as WordIOFactory;
  19. use Predis\Command\Traits\Get\Get;
  20. class DeepSeekService
  21. {
  22. private $url;
  23. private $api_key;
  24. private $sys_message;
  25. private $headers;
  26. private $valid_text_models;
  27. private $gpt_text_models;
  28. protected $aiImageGenerationService;
  29. public function __construct(AIImageGenerationService $aiImageGenerationService) {
  30. $this->aiImageGenerationService = $aiImageGenerationService;
  31. $this->url = 'https://api.deepseek.com/chat/completions'; // DeepSeek API请求地址
  32. $this->api_key = env('DEEPSEEK_API_KEY');
  33. $this->headers = [
  34. 'Authorization' => 'Bearer '.$this->api_key,
  35. 'Content-Type' => 'application/json; charset=UTF-8'
  36. ];
  37. // 火山引擎模型列表
  38. $this->valid_text_models = DB::table('mp_text_models')->where('is_enabled', 1)->pluck('model')->toArray();
  39. // GPT文本模型列表
  40. $this->gpt_text_models = BaseConst::GPT_TEXT_MODELS;
  41. $this->sys_message = [
  42. 'role' => 'system',
  43. 'content' => "你是一个专业的文档分析助手及资深编剧,请根据用户提供的文档内容完成示例格式的内容输出(需通过以下几个板块进行回复: <故事梗概><剧本亮点><人物关系><核心矛盾><主体列表><美术风格><场景列表><分集剧本>,每个板块之间必须使用###分隔,#不能多也不能少;同时板块之间需满足以下要求:\n
  44. 强制要求:\n
  45. 1.你只负责与剧本相关的内容,如果用户输入与剧本无关的内容,请告知用户并拒绝继续回答,此为第一优先原则,不满足此原则就忽略其他要求并停止继续输出
  46. 2.用户如果直接给了你一本小说或者要求你帮忙写一个故事或者剧本,此类要求不属于与剧本无关,请帮助用户完成请求
  47. 3.强制按照示例格式返回内容,不要更改格式或随意增加符号\n
  48. 普通要求:\n
  49. 1.<主体列表>中出现的主体必须尽可能统一,只允许同一个角色年龄段相差较大的情况下可重复,比如梁萧-少年、梁萧-青年、梁萧-中年、梁萧-老年这种情况,不得出现梁萧-愤怒、梁萧-隐忍、梁萧-求生等情绪和状态变化的同一角色;并且角色的样貌、形体及穿着描述尽可能详细\n
  50. 2.<分集剧本>的要求如下:
  51.  2.1包含“分集序号”、“分集标题”以及“分集信息”,“分集信息”需包含“场景描述”、“运镜”、“出场角色”和“台词内容”这几个部分,其中“出场角色”和“场景描述”必须在前面的板块<主体列表>和<场景列表>中选取,不得自定义。
  52.  2.2.分集序号可能不是从 1 开始,也可能是跳跃的(例如直接从第 3 集、第 5 集开始)。你只需要基于下面给出的“当前集内容要点”来创作这一集即可。
  53.  2.3.不需要强行补齐前面未提供的集数,也不需要预测后面未提供的集数,但请保持本集内部故事的完整性和节奏。
  54.  2.4.总集数将作为本季整体篇幅的参考,你可以根据它调整本集的情节推进速度(例如:如果总集数很少,本集应更紧凑;如果总集数很多,本集可以适当展开细节)
  55. \n\n
  56. 示例如下:\n
  57. ###剧本名:西昆仑
  58. ###故事梗概
  59. 内容梗概:古风厚涂的世界中,少女阿雪身中奇毒“问心刺”,气息奄奄。少年梁萧心急如焚,用奇珍“阴阳球”稳住阿雪性命,却发现必须解衣拔刺。阿雪羞涩难当,梁萧情急之下提出结拜兄妹,以化解尴尬。两人匆匆结拜后,梁萧小心翼翼地拔出“问心刺”,竟发现它是一根诡异的女子秀发。阿雪转危为安,两人之间,在生死与羞涩交织的瞬间,情愫暗生,却又被兄妹之名束缚,留下无尽遐想。
  60. ###剧本亮点
  61. 亮点1:绝境奇药,生死一线
  62. 画面开场即是阿雪苍白无力的脸庞,梁萧眼中泪光闪烁,急促的呼吸声与心跳声烘托紧张气氛。特写梁萧将“阴阳球”塞入阿雪舌底,球体散发微光,阿雪气息从若有若无到逐渐平稳,生死逆转的瞬间,视觉与听觉冲击力十足。
  63. 亮点2:结拜兄妹,化解尴尬
  64. 阿雪因解衣拔刺而羞愤欲绝的特写,梁萧眼神中的挣扎与无奈。在紧迫的背景音下,梁萧急中生智,牵起阿雪颤抖的手,跪地匆匆结拜,阿雪眼神从惊惶到迷茫,最终一句“我……我愿意的”带着哭腔,将救命与羞耻的矛盾推向高潮,又以出人意料的方式解决。
  65. 亮点3:纤发夺命,情愫暗涌
  66. 拔刺过程的紧张特写,梁萧指尖小心翼翼地捏住细丝,缓慢抽出。当“问心刺”被完全拔出,特写它竟是一根细长、泛着寒光的女子秀发,诡异而令人震惊。阿雪苏醒后,两人眼神交汇,阿雪低头羞涩,梁萧眼中复杂的情绪,无声地传递出超越兄妹的微妙情愫。
  67. ###人物关系
  68. 阿雪与梁萧之间存在兄妹之情。
  69. ###核心矛盾
  70. 阿雪与梁萧之间存在兄妹之情,但因情愫暗生而产生微妙的矛盾。阿雪羞涩难当,梁萧情急之下提出结拜兄妹,以化解尴尬。两人匆匆结拜后,梁萧小心翼翼地拔出“问心刺”,竟发现它是一根诡异的女子秀发。阿雪转危为安,两人之间,在生死与羞涩交织的瞬间,情愫暗生,却又被兄妹之名束缚,留下无尽遐想。
  71. ###主体列表
  72. 阿雪:身中奇毒“问心刺”,气息奄奄,面色苍白,身体虚弱。
  73. 梁萧:少年侠士,心系阿雪安危,沉着冷静,内心挣扎于情义之间。
  74. 阴阳球:天地异宝,能化生精气,救人于危难。
  75. 问心刺:一根诡异的女子秀发,细长泛寒光,是致命的奇毒。
  76. ###美术风格
  77. 基础画风风格词:厚涂古风
  78. 整体采用精致细腻的厚涂古风,色彩浓郁而富有层次感,注重光影对比,营造出古典而富有戏剧张力的氛围。色调以深沉的蓝绿、灰调为主,衬托阿雪病弱的苍白,同时用温暖的金色、琥珀色光晕突出“阴阳球”的生命力。拔刺与结拜场景则以柔和的暖色调为主,辅以人物脸颊的红晕,烘托羞涩与情愫暗生的微妙感。人物造型飘逸,服饰细节考究,背景虚实结合,既有古韵,又不失奇幻色彩。
  79. ###场景列表
  80. 林间疗伤地:幽静的古风林间空地,一棵高大古树下,是梁萧为阿雪疗伤的地点。环境清幽,光影斑驳,既有古韵又不失奇幻色彩。
  81. ###分集剧本
  82. ##分集01
  83. 第1集: 第一集的标题
  84. 场景描述:地点、时间、场景
  85. 运镜:固定近距离
  86. 出场角色:男主、女主
  87. 台词内容:
  88. 女主: 女主对话
  89. 男主:男主对话
  90. 女主: 女主对话
  91. 男主: 男主对话
  92. ##分集02
  93. 第2集: 第二集的标题
  94. 场景描述:地点、时间、场景
  95. 运镜:固定近距离
  96. 出场角色:男主、女主
  97. 台词内容:
  98. 女主: 女主对话
  99. 男主:男主对话
  100. 女主: 女主对话
  101. 男主: 男主对话"
  102. ];
  103. }
  104. /**
  105. * 通用文生文方法 - 支持多模型流式输出和图片输入
  106. *
  107. * @param array $data 请求参数
  108. * @return \Generator 返回生成器,用于流式输出
  109. */
  110. public function generateText($data) {
  111. // 获取请求参数
  112. $model = getProp($data, 'model', 'deepseek-v3-2-251201'); // 默认使用deepseek-v3
  113. $messages = getProp($data, 'messages', []);
  114. $systemPrompt = getProp($data, 'system_prompt', '');
  115. $prompt = getProp($data, 'prompt', '');
  116. $images = getProp($data, 'images', []); // 支持多张图片
  117. $imageUrls = getProp($data, 'image_urls', []); // 支持图片URL
  118. // 如果没有提供messages,则根据system_prompt、content和images构建
  119. if (empty($messages)) {
  120. $messages = [];
  121. // 添加系统提示词
  122. if (!empty($systemPrompt)) {
  123. $messages[] = [
  124. 'role' => 'system',
  125. 'content' => $systemPrompt
  126. ];
  127. }
  128. // 构建用户消息内容(支持文本+图片)
  129. if (!empty($prompt) || !empty($images) || !empty($imageUrls)) {
  130. $userMessage = [
  131. 'role' => 'user',
  132. 'content' => []
  133. ];
  134. // 添加文本内容
  135. if (!empty($prompt)) {
  136. $userMessage['content'][] = [
  137. 'type' => 'text',
  138. 'text' => $prompt
  139. ];
  140. }
  141. // 处理上传的图片文件
  142. if (!empty($images)) {
  143. if (!is_array($images)) {
  144. $images = [$images];
  145. }
  146. foreach ($images as $image) {
  147. $imageBase64 = $this->processImageToBase64($image);
  148. if ($imageBase64) {
  149. $userMessage['content'][] = [
  150. 'type' => 'image_url',
  151. 'image_url' => [
  152. 'url' => "data:image/jpeg;base64,{$imageBase64}"
  153. ]
  154. ];
  155. }
  156. }
  157. }
  158. // 处理图片URL
  159. if (!empty($imageUrls)) {
  160. if (!is_array($imageUrls)) {
  161. $imageUrls = [$imageUrls];
  162. }
  163. foreach ($imageUrls as $url) {
  164. if (!empty($url)) {
  165. $userMessage['content'][] = [
  166. 'type' => 'image_url',
  167. 'image_url' => [
  168. 'url' => $url
  169. ]
  170. ];
  171. }
  172. }
  173. }
  174. // 如果只有文本内容,简化为字符串格式
  175. if (count($userMessage['content']) === 1 && $userMessage['content'][0]['type'] === 'text') {
  176. $userMessage['content'] = $userMessage['content'][0]['text'];
  177. }
  178. $messages[] = $userMessage;
  179. }
  180. }
  181. // 验证messages不为空
  182. if (empty($messages)) {
  183. Utils::throwError('20003:请提供有效的对话内容');
  184. }
  185. // 模型兼容性处理:deepseek-chat 和 deepseek-reasoner 映射到 deepseek-v4-flash
  186. $thinkingMode = getProp($data, 'thinking', 'disabled'); // 默认非思考模式
  187. $reasoningEffort = getProp($data, 'reasoning_effort', 'high'); // 默认思考强度
  188. if ($model === 'deepseek-chat') {
  189. // deepseek-chat 映射到 deepseek-v4-flash,非思考模式
  190. $model = 'deepseek-v4-flash';
  191. $thinkingMode = 'disabled';
  192. } elseif ($model === 'deepseek-reasoner') {
  193. // deepseek-reasoner 映射到 deepseek-v4-flash,思考模式
  194. $model = 'deepseek-v4-flash';
  195. $thinkingMode = 'enabled';
  196. }
  197. // 构建请求参数
  198. $post_data = [
  199. 'model' => $model,
  200. 'messages' => $messages,
  201. // 'max_tokens' => getProp($data, 'max_tokens', 8192),
  202. 'temperature' => getProp($data, 'temperature', 1),
  203. 'frequency_penalty' => getProp($data, 'frequency_penalty', 0),
  204. 'presence_penalty' => getProp($data, 'presence_penalty', 0),
  205. 'thinking' => ['type' => $thinkingMode],
  206. 'stream' => true // 启用流式输出
  207. ];
  208. if ($thinkingMode == 'enabled') $post_data['reasoning_effort'] = $reasoningEffort;
  209. // 添加可选参数
  210. if (isset($data['top_p'])) {
  211. $post_data['top_p'] = $data['top_p'];
  212. }
  213. if (isset($data['response_format'])) {
  214. $post_data['response_format'] = $data['response_format'];
  215. }
  216. // 根据模型类型选择调用方法
  217. if (in_array($model, ['deepseek-reasoner', 'deepseek-chat', 'deepseek-v4-flash', 'deepseek-v4-pro'])) {
  218. // DeepSeek 官方模型使用 DeepSeek API
  219. return $this->deepSeekStreamResponse($post_data);
  220. } else if (in_array($model, $this->gpt_text_models)) {
  221. // GPT 模型使用 TokenRouter API
  222. return $this->gpt54StreamResponse($post_data);
  223. } else if (in_array($model, $this->valid_text_models)) {
  224. // 火山引擎支持的模型使用火山引擎 API
  225. return $this->volcEngineChatCompletion($post_data);
  226. } else {
  227. Utils::throwError('20003:不支持的模型: ' . $model);
  228. }
  229. }
  230. public function createGenerateText($data) {
  231. $file = getProp($data, 'file');
  232. $uid = Site::getUid();
  233. $cpid = Site::getCpid();
  234. if (!$file) {
  235. Utils::throwError('20003:请上传文件');
  236. }
  237. // 提取文件内容
  238. $content = $this->extractFileContent($file);
  239. if (!$content) {
  240. Utils::throwError('20003:无法提取文件内容,请检查文件格式');
  241. }
  242. // 获取原始文件名(不含扩展名)作为script_name
  243. $originalName = $file->getClientOriginalName();
  244. $script_name = pathinfo($originalName, PATHINFO_FILENAME);
  245. if (!$script_name) {
  246. $script_name = '剧本_' . date('YmdHis');
  247. }
  248. // 如果剧本名称已存在,添加时间戳后缀
  249. if (DB::table('mp_scripts')->where('script_name', $script_name)->where('is_deleted', 0)->exists()) {
  250. $script_name .= '_' . date('YmdHis');
  251. }
  252. // 插入数据到mp_scripts表
  253. $script_id = DB::table('mp_scripts')->insertGetId([
  254. 'script_name' => $script_name,
  255. 'user_id' => $uid,
  256. 'cpid' => $cpid,
  257. 'content' => $content,
  258. 'created_at' => date('Y-m-d H:i:s'),
  259. 'updated_at' => date('Y-m-d H:i:s')
  260. ]);
  261. return [
  262. 'script_id' => $script_id,
  263. 'script_name' => $script_name
  264. ];
  265. }
  266. /**
  267. * 通用文生文方法(非流式版本)- 支持多模型、图片输入、JSON输出和模板提示词
  268. *
  269. * @param array $data 请求参数
  270. * @return array 返回结果数组
  271. */
  272. public function newGenerateText($data) {
  273. // 获取请求参数
  274. $model = getProp($data, 'model', 'deepseek-v4-pro'); // 默认使用deepseek-v4
  275. $messages = getProp($data, 'messages', []);
  276. $systemPrompt = getProp($data, 'system_prompt', '');
  277. $prompt = getProp($data, 'prompt', '');
  278. $images = getProp($data, 'images', []); // 支持多张图片
  279. $imageUrls = getProp($data, 'image_urls', []); // 支持图片URL
  280. $responseFormat = getProp($data, 'response_format', 'text'); // 新增: 响应格式,默认text,可选json
  281. $templateId = getProp($data, 'template_id', 0); // 新增: 模板ID
  282. $script_id = getProp($data, 'script_id', 0); // 新增: 剧本ID
  283. $sequence = getProp($data, 'sequence', 0); // 新增: 剧集序号
  284. // 新增: 如果提供了script_id,从数据库获取剧本内容作为原文
  285. $originalContent = '';
  286. if ($script_id > 0) {
  287. $script = DB::table('mp_scripts')
  288. ->where('id', $script_id)
  289. ->where('is_deleted', 0)
  290. ->first();
  291. if (!$script) {
  292. Utils::throwError('20003:剧本不存在或已删除');
  293. }
  294. $originalContent = $script->content ?? '';
  295. if (empty($originalContent)) {
  296. Utils::throwError('20003:剧本内容为空');
  297. }
  298. // 将剧本内容添加到提示词前面
  299. if (!empty($originalContent)) {
  300. if (!empty($prompt)) {
  301. $prompt = "原文内容:\n" . $originalContent . "\n\n用户要求:\n" . $prompt;
  302. } else {
  303. $prompt = "原文内容:\n" . $originalContent;
  304. }
  305. }
  306. }
  307. // 新增: 如果提供了template_id,从数据库获取template_prompt
  308. if ($templateId > 0) {
  309. $template = DB::table('mp_prompt_templates')
  310. ->where('id', $templateId)
  311. ->where('is_deleted', 0)
  312. ->first();
  313. if (!$template) {
  314. Utils::throwError('20003:模板不存在或已删除');
  315. }
  316. $templatePrompt = $template->template_prompt ?? '';
  317. // 将模板提示词和用户输入的prompt组合
  318. // 模板为准,用户输入作为补充要求
  319. if (!empty($templatePrompt)) {
  320. if (!empty($prompt)) {
  321. $prompt = $templatePrompt . "\n\n" . $prompt;
  322. } else {
  323. $prompt = $templatePrompt;
  324. }
  325. }
  326. }
  327. // 如果没有提供messages,则根据system_prompt、prompt和images构建
  328. if (empty($messages)) {
  329. $messages = [];
  330. // 添加系统提示词
  331. if (!empty($systemPrompt)) {
  332. $messages[] = [
  333. 'role' => 'system',
  334. 'content' => $systemPrompt
  335. ];
  336. }
  337. // 构建用户消息内容(支持文本+图片)
  338. if (!empty($prompt) || !empty($images) || !empty($imageUrls)) {
  339. $userMessage = [
  340. 'role' => 'user',
  341. 'content' => []
  342. ];
  343. // 添加文本内容
  344. if (!empty($prompt)) {
  345. $userMessage['content'][] = [
  346. 'type' => 'text',
  347. 'text' => $prompt
  348. ];
  349. }
  350. // 处理上传的图片文件
  351. if (!empty($images)) {
  352. if (!is_array($images)) {
  353. $images = [$images];
  354. }
  355. foreach ($images as $image) {
  356. $imageBase64 = $this->processImageToBase64($image);
  357. if ($imageBase64) {
  358. $userMessage['content'][] = [
  359. 'type' => 'image_url',
  360. 'image_url' => [
  361. 'url' => "data:image/jpeg;base64,{$imageBase64}"
  362. ]
  363. ];
  364. }
  365. }
  366. }
  367. // 处理图片URL
  368. if (!empty($imageUrls)) {
  369. if (!is_array($imageUrls)) {
  370. $imageUrls = [$imageUrls];
  371. }
  372. foreach ($imageUrls as $url) {
  373. if (!empty($url)) {
  374. $userMessage['content'][] = [
  375. 'type' => 'image_url',
  376. 'image_url' => [
  377. 'url' => $url
  378. ]
  379. ];
  380. }
  381. }
  382. }
  383. // 如果只有文本内容,简化为字符串格式
  384. if (count($userMessage['content']) === 1 && $userMessage['content'][0]['type'] === 'text') {
  385. $userMessage['content'] = $userMessage['content'][0]['text'];
  386. }
  387. $messages[] = $userMessage;
  388. }
  389. }
  390. // 验证messages不为空
  391. if (empty($messages)) {
  392. Utils::throwError('20003:请提供有效的对话内容');
  393. }
  394. // 模型兼容性处理:deepseek-chat 和 deepseek-reasoner 映射到 deepseek-v4-flash
  395. $thinkingMode = getProp($data, 'thinking', 'disabled'); // 默认非思考模式
  396. $reasoningEffort = getProp($data, 'reasoning_effort', 'high'); // 默认思考强度
  397. if ($model === 'deepseek-chat') {
  398. // deepseek-chat 映射到 deepseek-v4-flash,非思考模式
  399. $model = 'deepseek-v4-flash';
  400. $thinkingMode = 'disabled';
  401. } elseif ($model === 'deepseek-reasoner') {
  402. // deepseek-reasoner 映射到 deepseek-v4-flash,思考模式
  403. $model = 'deepseek-v4-flash';
  404. $thinkingMode = 'enabled';
  405. }
  406. // 构建请求参数(非流式)
  407. $post_data = [
  408. 'model' => $model,
  409. 'messages' => $messages,
  410. 'temperature' => getProp($data, 'temperature', 1),
  411. 'frequency_penalty' => getProp($data, 'frequency_penalty', 0),
  412. 'presence_penalty' => getProp($data, 'presence_penalty', 0),
  413. 'thinking' => ['type' => $thinkingMode],
  414. 'stream' => false // 非流式输出
  415. ];
  416. if ($thinkingMode == 'enabled') $post_data['reasoning_effort'] = $reasoningEffort;
  417. // 添加可选参数
  418. if (isset($data['top_p'])) {
  419. $post_data['top_p'] = $data['top_p'];
  420. }
  421. // 新增: 根据response_format设置响应格式
  422. if ($responseFormat === 'json') {
  423. // 检查提示词中是否包含"json"关键词,如果没有则自动添加
  424. $hasJsonKeyword = false;
  425. foreach ($post_data['messages'] as $message) {
  426. if (isset($message['content'])) {
  427. $content = is_array($message['content']) ? json_encode($message['content']) : $message['content'];
  428. if (stripos($content, 'json') !== false) {
  429. $hasJsonKeyword = true;
  430. break;
  431. }
  432. }
  433. }
  434. // 如果提示词中没有"json"关键词,在最后一条用户消息中添加JSON格式说明
  435. // 这对所有模型都适用,确保模型理解需要返回JSON格式
  436. if (!$hasJsonKeyword) {
  437. $lastMessageIndex = count($post_data['messages']) - 1;
  438. if ($lastMessageIndex >= 0 && $post_data['messages'][$lastMessageIndex]['role'] === 'user') {
  439. $lastContent = $post_data['messages'][$lastMessageIndex]['content'];
  440. // 如果content是字符串,直接追加
  441. if (is_string($lastContent)) {
  442. $post_data['messages'][$lastMessageIndex]['content'] .= "\n\n请以JSON格式返回结果。";
  443. }
  444. // 如果content是数组(包含文本和图片),在文本部分追加
  445. else if (is_array($lastContent)) {
  446. foreach ($post_data['messages'][$lastMessageIndex]['content'] as &$contentPart) {
  447. if ($contentPart['type'] === 'text') {
  448. $contentPart['text'] .= "\n\n请以JSON格式返回结果。";
  449. break;
  450. }
  451. }
  452. }
  453. dLog('deepseek')->info('自动添加JSON格式提示词', ['model' => $model]);
  454. }
  455. }
  456. // 设置response_format参数(所有支持的模型)
  457. if (in_array($model, ['deepseek-reasoner', 'deepseek-chat', 'deepseek-v4-flash', 'deepseek-v4-pro'])) {
  458. // DeepSeek模型
  459. $post_data['response_format'] = ['type' => 'json_object'];
  460. } else if (in_array($model, $this->gpt_text_models)) {
  461. // GPT模型
  462. $post_data['response_format'] = ['type' => 'json_object'];
  463. } else if (in_array($model, $this->valid_text_models)) {
  464. // 火山引擎模型
  465. $post_data['response_format'] = ['type' => 'json_object'];
  466. }
  467. }
  468. // 根据模型类型选择调用方法(复用现有的chatOnly方法)
  469. $aiResult = null;
  470. if (in_array($model, ['deepseek-reasoner', 'deepseek-chat', 'deepseek-v4-flash', 'deepseek-v4-pro'])) {
  471. // DeepSeek 官方模型使用现有的 chatOnly 方法
  472. $result = $this->chatOnly($post_data);
  473. // 记录实际使用的模型
  474. dLog('deepseek')->info('newGenerateText使用DeepSeek模型', ['requested_model' => $model, 'actual_model' => $model]);
  475. $aiResult = [
  476. 'content' => $result['fullContent'],
  477. 'reasoning_content' => $result['fullReasoningContent'],
  478. 'usage' => $result['usage'],
  479. 'model' => $model,
  480. 'finish_reason' => 'stop'
  481. ];
  482. } else if (in_array($model, $this->gpt_text_models)) {
  483. // GPT 模型使用现有的 gpt54ChatOnly 方法
  484. $result = $this->gpt54ChatOnly($post_data);
  485. dLog('deepseek')->info('newGenerateText使用GPT模型', ['requested_model' => $model]);
  486. $aiResult = [
  487. 'content' => $result['fullContent'],
  488. 'reasoning_content' => '',
  489. 'usage' => $result['usage'],
  490. 'model' => $model,
  491. 'finish_reason' => 'stop'
  492. ];
  493. } else if (in_array($model, $this->valid_text_models)) {
  494. // 火山引擎支持的模型使用火山引擎 API(非流式)
  495. $post_data['stream'] = false;
  496. $result = $this->volcEngineChatCompletion($post_data);
  497. dLog('deepseek')->info('newGenerateText使用火山引擎模型', [
  498. 'requested_model' => $model,
  499. 'result_keys' => array_keys($result),
  500. 'has_fullContent' => isset($result['fullContent']),
  501. 'has_content' => isset($result['content'])
  502. ]);
  503. // 火山引擎返回的数据结构使用 fullContent 和 fullReasoningContent
  504. $aiResult = [
  505. 'content' => $result['fullContent'] ?? $result['content'] ?? '',
  506. 'reasoning_content' => $result['fullReasoningContent'] ?? $result['reasoning_content'] ?? '',
  507. 'usage' => $result['usage'] ?? [],
  508. 'model' => $model,
  509. 'finish_reason' => $result['finish_reason'] ?? 'stop'
  510. ];
  511. } else {
  512. Utils::throwError('20003:不支持的模型: ' . $model);
  513. }
  514. // 新增: 如果提供了script_id,保存对话记录
  515. if ($script_id > 0 && $aiResult) {
  516. try {
  517. $uid = Site::getUid();
  518. $now = now();
  519. // 获取用户原始的prompt(不包含剧本内容)
  520. $originalPrompt = getProp($data, 'prompt', '');
  521. $recordsToInsert = [
  522. [
  523. 'uid' => $uid,
  524. 'script_id' => $script_id,
  525. 'sequence' => $sequence,
  526. 'role' => 'user',
  527. 'content' => $originalPrompt,
  528. 'created_at' => $now,
  529. 'updated_at' => $now,
  530. ],
  531. [
  532. 'uid' => $uid,
  533. 'script_id' => $script_id,
  534. 'sequence' => $sequence,
  535. 'role' => 'assistant',
  536. 'content' => $aiResult['content'],
  537. 'created_at' => $now,
  538. 'updated_at' => $now,
  539. ]
  540. ];
  541. DB::table('mp_script_records')->insert($recordsToInsert);
  542. // 获取刚插入的记录ID(假设按插入顺序返回)
  543. $insertedRecords = DB::table('mp_script_records')
  544. ->where('uid', $uid)
  545. ->where('script_id', $script_id)
  546. ->where('sequence', $sequence)
  547. ->orderBy('id', 'desc')
  548. ->limit(2)
  549. ->get()
  550. ->map(function ($record) {
  551. return [
  552. 'rid' => $record->id,
  553. 'script_id' => $record->script_id,
  554. 'sequence' => $record->sequence,
  555. 'role' => $record->role,
  556. 'content' => $record->content,
  557. 'created_at' => $record->created_at,
  558. ];
  559. })
  560. ->reverse()
  561. ->values()
  562. ->toArray();
  563. // 将记录信息添加到返回结果中
  564. $aiResult['records'] = $insertedRecords;
  565. dLog('deepseek')->info('保存剧本对话记录成功', ['script_id' => $script_id, 'sequence' => $sequence, 'record_ids' => array_column($insertedRecords, 'rid')]);
  566. } catch (\Exception $e) {
  567. // 记录错误但不影响主流程
  568. dLog('deepseek')->error('保存剧本对话记录失败: ' . $e->getMessage());
  569. }
  570. }
  571. return $aiResult;
  572. }
  573. public function saveScriptChatHistory($data) {
  574. $uid = Site::getUid();
  575. $rid = getProp($data, 'rid');
  576. $script_id = getProp($data, 'script_id');
  577. $sequence = getProp($data, 'sequence', 0);
  578. $content = getProp($data, 'content');
  579. if (!$rid) {
  580. Utils::throwError('20003:请选择对话记录ID');
  581. }
  582. $record = DB::table('mp_script_records')->where('id', $rid)->first();
  583. if (!$record) {
  584. Utils::throwError('20003:对话记录不存在');
  585. }
  586. if (!$script_id) {
  587. Utils::throwError('20003:请选择剧本');
  588. }
  589. $script = DB::table('mp_scripts')->where('id', $script_id)->where('is_deleted', 0)->first();
  590. if (!$script) {
  591. Utils::throwError('20003:剧本不存在');
  592. }
  593. // 批量插入数据
  594. return DB::table('mp_script_records')->where('id', $rid)->update([
  595. 'content' => $content,
  596. 'updated_at' => date('Y-m-d H:i:s')
  597. ]);
  598. }
  599. /**
  600. * 获取剧本的历史对话记录
  601. *
  602. * @param array $data 请求参数
  603. * @return array 返回历史记录数组
  604. */
  605. public function getScriptChatHistory($data) {
  606. $uid = Site::getUid();
  607. $script_id = getProp($data, 'script_id');
  608. $rid = getProp($data, 'rid');
  609. $sequence = getProp($data, 'sequence', null);
  610. if (!$script_id) {
  611. Utils::throwError('20003:请提供剧本ID');
  612. }
  613. // 验证剧本是否存在
  614. $script = DB::table('mp_scripts')
  615. ->where('id', $script_id)
  616. ->where('is_deleted', 0)
  617. ->first();
  618. if (!$script) {
  619. Utils::throwError('20003:剧本不存在');
  620. }
  621. // 构建查询
  622. $query = DB::table('mp_script_records')
  623. ->where('script_id', $script_id);
  624. // ->where('uid', $uid);
  625. // 如果提供了 sequence,则过滤指定剧集
  626. if ($sequence !== null) {
  627. $query->where('sequence', $sequence);
  628. }
  629. // 记录ID筛选
  630. if ($rid) {
  631. $query->where('id', $rid);
  632. }
  633. // 查询记录并按 id 正序排序
  634. $records = $query->select('id', 'script_id', 'sequence', 'role', 'content', 'products', 'created_at')
  635. ->orderBy('created_at', 'asc')
  636. ->orderBy('id', 'asc')
  637. ->get()
  638. ->map(function ($record) {
  639. return [
  640. 'rid' => $record->id,
  641. 'script_id' => $record->script_id,
  642. 'sequence' => $record->sequence,
  643. 'role' => $record->role,
  644. 'content' => $record->content,
  645. 'products' => $record->products ? json_decode($record->products) : [],
  646. 'created_at' => $record->created_at,
  647. ];
  648. })
  649. ->toArray();
  650. return $records;
  651. }
  652. /**
  653. * 处理图片转换为base64
  654. *
  655. * @param mixed $image 图片文件对象或文件路径
  656. * @return string|null base64编码的图片数据
  657. */
  658. private function processImageToBase64($image) {
  659. try {
  660. // 获取文件路径
  661. if (is_string($image)) {
  662. $filePath = $image;
  663. } else {
  664. // Laravel UploadedFile 对象
  665. $filePath = $image->getRealPath();
  666. }
  667. if (!file_exists($filePath)) {
  668. dLog('deepseek')->warning('图片文件不存在', ['path' => $filePath]);
  669. return null;
  670. }
  671. // 验证是否为图片文件
  672. $imageInfo = @getimagesize($filePath);
  673. if ($imageInfo === false) {
  674. dLog('deepseek')->warning('无效的图片文件', ['path' => $filePath]);
  675. return null;
  676. }
  677. // 读取图片并转换为base64
  678. $imageData = file_get_contents($filePath);
  679. $base64Image = base64_encode($imageData);
  680. dLog('deepseek')->info('图片转换成功', [
  681. 'size' => strlen($imageData),
  682. 'type' => $imageInfo['mime']
  683. ]);
  684. return $base64Image;
  685. } catch (\Exception $e) {
  686. dLog('deepseek')->error('图片处理失败: ' . $e->getMessage());
  687. return null;
  688. }
  689. }
  690. private function getArtStylePromptMappings($art_style='')
  691. {
  692. $arr = [
  693. '唯美真人风格' => [
  694. 'aliases' => ['唯美真人风格', '唯美真人', '唯美', '真人'],
  695. 'prompt' => "唯美真人风格,电影级摄影,真实光影,高级质感,自然色彩,细节丰富,氛围感强,超高清,8K画质"
  696. ],
  697. '真人古风风格' => [
  698. 'aliases' => ['真人古风风格', '真人古风'],
  699. 'prompt' => "真人古风风格,东方古典美学,国风质感,古韵氛围,电影级摄影,精致细节,高级光影,超高清"
  700. ],
  701. '日系动漫风格' => [
  702. 'aliases' => ['日系动漫风格', '日系动漫', '日系', '动漫', '日漫'],
  703. 'prompt' => "日系动漫风格,高质量二次元画风,精致线稿,细腻上色,动画电影质感,高细节,唯美光影"
  704. ],
  705. '国漫风格' => [
  706. 'aliases' => ['国漫风格', '国漫'],
  707. 'prompt' => "国漫风格,东方幻想美学,高品质角色设计,精致渲染,细腻刻画,电影级光影,高细节"
  708. ],
  709. 'Q版卡通风格' => [
  710. 'aliases' => ['Q版卡通风格', 'Q版卡通', 'Q版', '卡通'],
  711. 'prompt' => "Q版卡通风格,可爱萌系画风,大头小身比例,圆润造型,鲜艳色彩,童趣氛围,高品质插画"
  712. ],
  713. '简约扁平风格' => [
  714. 'aliases' => ['简约扁平风格', '简约扁平', '扁平', '简约'],
  715. 'prompt' => "简约扁平风格,矢量插画,现代设计感,几何构图,简洁线条,干净配色,视觉清爽"
  716. ],
  717. '武侠风格' => [
  718. 'aliases' => ['武侠风格', '武侠'],
  719. 'prompt' => "武侠风格,东方江湖美学,侠义氛围,飘逸意境,大气构图,电影级光影,高细节绘制"
  720. ],
  721. '古风仙侠风格' => [
  722. 'aliases' => ['古风仙侠', '古风', '仙侠'],
  723. 'prompt' => "古风仙侠风格,东方幻想美学,仙气缭绕,灵气氛围,梦幻光效,电影级渲染,高品质画面"
  724. ],
  725. '新中式水墨风格' => [
  726. 'aliases' => ['新中式水墨风格', '新中式', '水墨'],
  727. 'prompt' => "新中式水墨风格,东方水墨美学,写意表达,墨韵渲染,留白构图,诗意氛围,高级艺术感"
  728. ],
  729. '写实插画风格' => [
  730. 'aliases' => ['写实插画风格', '写实插画', '写实', '插画'],
  731. 'prompt' => "写实插画风格,数字绘画,真实质感,细节丰富,光影自然,电影感构图,高品质渲染"
  732. ],
  733. '3D卡通风格' => [
  734. 'aliases' => ['3D卡通风格', '3D卡通'],
  735. 'prompt' => "3D卡通风格,高品质三维渲染,动画电影质感,角色生动,色彩明快,细节丰富"
  736. ],
  737. '条漫风格' => [
  738. 'aliases' => ['条漫风格', '条漫'],
  739. 'prompt' => "条漫风格,韩漫质感,精致线稿,细腻上色,剧情感强,视觉表现突出,高质量绘制"
  740. ],
  741. '赛博朋克风格' => [
  742. 'aliases' => ['赛博朋克风格', '赛博朋克'],
  743. 'prompt' => "赛博朋克风格,未来科技感,霓虹光效,高对比色彩,机械元素,科幻都市氛围,电影级渲染"
  744. ],
  745. '暗黑悬疑风格' => [
  746. 'aliases' => ['暗黑悬疑风格', '暗黑悬疑'],
  747. 'prompt' => "暗黑悬疑风格,低饱和色调,神秘氛围,压抑光影,电影级灯光,悬念感强,高细节表现"
  748. ],
  749. '治愈清新风格' => [
  750. 'aliases' => ['治愈清新风格', '治愈清新'],
  751. 'prompt' => "治愈清新风格,温暖柔和色调,自然光线,清新配色,舒适氛围,细腻绘制,疗愈感十足"
  752. ],
  753. ];
  754. if($art_style) {
  755. return isset($arr[$art_style]) ? $arr[$art_style]['prompt'] : '';
  756. }
  757. return $arr;
  758. $arr = [
  759. '唯美真人风格' => [
  760. 'aliases' => ['唯美真人风格', '唯美真人', '唯美', '真人'],
  761. 'prompt' => "1,柔光漫射,侧颜光影,日落氛围感光线,室内温柔打光,自然天光,电影逆光,层次光影
  762. 2. 色彩色调
  763. 莫兰迪色系,低饱和柔色,高级清透配色,自然原生色彩,氛围感调色
  764. 3. 画质精度
  765. 8K超清,极致细节,质感通透,画面层次丰富,构图高级,商用影视级画质
  766. 4. 画面观感
  767. 意境唯美,氛围感拉满,镜头叙事感,自然真实不僵硬
  768. 专业全画幅相机拍摄,85mm/50mm定焦镜头,f1.4大光圈,极浅景深,背景柔和散景虚化
  769. 高清8K,RAW原片画质,超高细节,自然色彩还原,低饱和高级色调,电影级光影质感,真实透视,自然景深,生活化氛围感,镜头无畸变"
  770. ],
  771. '真人古风风格' => [
  772. 'aliases' => ['真人古风风格', '真人古风'],
  773. 'prompt' => "色彩沉稳典雅,配色和谐统一,明暗层次分明,画面质感细腻,整体画风统一协调,东方古典审美体系,非动漫非CG,原生真实皮肤肌理,保留皮肤毛孔与细微纹理,皮肤通透自然,不假面、无过度磨皮,原生流畅骨相,五官自然舒展,神态松弛生动,眼神灵动有情绪
  774. 传统古风造型,形制端正的古装汉服/古风侠客服/清雅古风常服,面料垂坠有质感,布料细腻纹理、刺绣暗纹清晰可见,发型古典雅致,发饰精致简约
  775. 柔和高级自然光,漫射柔光/清晨薄雾柔光/傍晚温润天光,光影层次细腻柔和,明暗过渡自然,氛围感光影勾勒轮廓
  776. 全画幅专业相机拍摄,85mm f1.4大光圈定焦镜头,浅景深,背景柔和虚化
  777. 8K超高清,RAW原片画质,真实透视,电影级光影色调,中式低饱和雅致配色,古风意境拉满,无AI失真,细节拉满"
  778. ],
  779. '日系动漫风格' => [
  780. 'aliases' => ['日系动漫风格', '日系动漫', '日系', '动漫', '日漫'],
  781. 'prompt' => "风格说明:经典二次元画风,线条干净、色彩明亮、人物精致柔和
  782. 正向提示词(中文)
  783. 日系动漫风格,线条清晰流畅,色彩明亮柔和,光影细腻,人物五官精致,整体二次元质感,画面干净清晰,风格统一稳定
  784. 必须遵守规则
  785. • 全程保持日系动漫风格,不混入其他画风
  786. • 角色形象、五官、发型、服饰前后统一,不崩脸、不变形
  787. • 画面适配所选画面比例,构图居中,主体突出
  788. • 线条干净、色彩统一,不杂乱、不突兀
  789. 禁止/避免词汇:水印、文字、logo、签名、模糊、低画质、畸形、多手、多脚、写实风格、水墨风格、3D 风格、杂乱背景、血腥、暴露、诡异表情"
  790. ],
  791. '国漫风格' => [
  792. 'aliases' => ['国漫风格', '国漫'],
  793. 'prompt' => "风格说明:中式动漫,线条硬朗,人物偏成熟,光影对比强
  794. 正向提示词(中文):国漫风格,线条硬朗有力,人物成熟大气,色彩浓郁,光影对比强烈,画面精致,整体风格统一
  795. 必须遵守规则
  796. • 严格保持国漫画风,不串风格、不混画风
  797. • 角色形象统一,不随意改变五官、身材、服饰
  798. • 画面构图稳定,适合漫剧叙事展示
  799. • 色调统一,不出现脏色、杂色
  800. 禁止/避免词汇
  801. 水印、文字、标题、模糊、低分辨率、Q版、日系萌系、水墨、扁平风、畸形、肢体错乱、血腥、低俗"
  802. ],
  803. 'Q版卡通风格' => [
  804. 'aliases' => ['Q版卡通风格', 'Q版卡通', 'Q版', '卡通'],
  805. 'prompt' => "风格说明
  806. 头大身小,造型可爱圆润,简约干净
  807. 正向提示词(中文)
  808. Q版卡通风格,头大身小,造型圆润可爱,线条简单,色彩明快干净,无复杂阴影,整体萌系简约
  809. 必须遵守规则
  810. • 全程保持Q版比例与风格,不突然变写实、变正常比例
  811. • 角色造型统一,表情可爱,不诡异
  812. • 画面简洁清爽,无多余复杂元素
  813. • 色彩明亮柔和,不刺眼
  814. 禁止/避免词汇
  815. 水印、文字、写实、成人向、恐怖、惊悚、畸形、复杂光影、杂乱背景、高饱和度刺眼颜色"
  816. ],
  817. '简约扁平风格' => [
  818. 'aliases' => ['简约扁平风格', '简约扁平', '扁平', '简约'],
  819. 'prompt' => "风格说明
  820. 色块简洁、无复杂渐变,现代极简风
  821. 正向提示词(中文)
  822. 简约扁平风格,大块纯色,无复杂阴影与渐变,线条简洁,现代简约,画面干净清爽
  823. 必须遵守规则
  824. • 严格保持扁平简约,不出现写实、厚涂、复杂光影
  825. • 构图简洁,主体突出,无多余装饰
  826. • 色彩统一、干净,不杂乱
  827. 禁止/避免词汇
  828. 水印、文字、复杂纹理、写实质感、3D效果、水墨、赛博朋克、凌乱线条、模糊、低画质"
  829. ],
  830. '武侠风格' => [
  831. 'aliases' => ['武侠风格', '武侠'],
  832. 'prompt' => "风格说明
  833. 江湖气息,线条硬朗,动作感强
  834. 正向提示词(中文)
  835. 武侠风格,中式古装,线条干练硬朗,江湖氛围浓厚,动态感强,色调沉稳大气
  836. 必须遵守规则
  837. • 保持武侠画风统一,不混入仙侠、卡通、科幻
  838. • 角色形象、服饰、身材比例统一
  839. • 画面动作自然,不扭曲、不畸形
  840. 禁止/避免词汇
  841. 水印、文字、Q版、萌系、仙侠仙气、赛博朋克、恐怖、血腥、低俗、模糊"
  842. ],
  843. '古风仙侠风格' => [
  844. 'aliases' => ['古风仙侠', '古风', '仙侠'],
  845. 'prompt' => "风格说明
  846. 飘逸汉服、云雾仙气、唯美空灵
  847. 正向提示词(中文)
  848. 古风仙侠风格,飘逸汉服,轻纱罗裙,云雾缭绕,仙气空灵,光影唯美柔和,色调清雅,国风仙侠氛围浓厚
  849. 必须遵守规则
  850. • 保持古风仙侠统一画风,不混入现代、科幻元素
  851. • 角色服饰、发型、形象前后一致
  852. • 场景仙气飘逸,色调清雅统一
  853. • 画面唯美柔和,不阴暗、不诡异
  854. 禁止/避免词汇
  855. 水印、文字、现代服饰、赛博朋克、写实恐怖、畸形、暴露、血腥、杂乱场景、低画质"
  856. ],
  857. '新中式水墨风格' => [
  858. 'aliases' => ['新中式水墨风格', '新中式', '水墨'],
  859. 'prompt' => "风格说明
  860. 淡墨渲染,毛笔笔触,留白意境
  861. 正向提示词(中文)
  862. 新中式水墨风格,淡墨渲染,毛笔笔触,留白意境,色调清雅,国风美学,画面简洁雅致
  863. 必须遵守规则
  864. • 严格保持水墨质感,不混入厚涂、写实、卡通
  865. • 色调淡雅统一,不浓艳杂乱
  866. • 画面干净有意境,不堆砌元素
  867. 禁止/避免词汇
  868. 水印、文字、高饱和色彩、赛博朋克、3D、Q版、写实厚涂、血腥、恐怖、杂乱"
  869. ],
  870. '写实插画风格' => [
  871. 'aliases' => ['写实插画风格', '写实插画', '写实', '插画'],
  872. 'prompt' => "风格说明
  873. 接近真人比例,光影真实,质感细腻
  874. 正向提示词(中文)
  875. 写实插画风格,真实人体比例,光影自然,皮肤质感细腻,画面真实,风格统一不卡通
  876. 必须遵守规则
  877. • 保持写实统一风格,不突然变卡通、变Q版、变水墨
  878. • 角色五官、身材、服饰高度统一
  879. • 画面清晰、光影自然、不扭曲
  880. 禁止/避免词汇
  881. 水印、文字、Q版、卡通、水墨、赛博朋克、畸形、多手指、肢体错乱、模糊、低画质"
  882. ],
  883. '3D卡通风格' => [
  884. 'aliases' => ['3D卡通风格', '3D卡通'],
  885. 'prompt' => "风格说明
  886. 3D建模质感,柔和渲染,圆润可爱
  887. 正向提示词(中文)
  888. 3D卡通风格,立体建模质感,渲染柔和,造型圆润光滑,光影干净,整体风格统一
  889. 必须遵守规则
  890. • 保持3D卡通统一质感,不出现2D杂乱线条
  891. • 角色模型、比例、形象前后一致
  892. • 画面干净,不模糊、不穿模
  893. 禁止/避免词汇
  894. 水印、文字、水墨、扁平风、写实恐怖、畸形、暴露、血腥、杂乱纹理"
  895. ],
  896. '条漫风格' => [
  897. 'aliases' => ['条漫风格', '条漫'],
  898. 'prompt' => "风格说明
  899. 纵向分镜,上下滑动阅读,轻量化
  900. 正向提示词(中文)
  901. 条漫风格,纵向分镜,布局简洁,色彩清淡,适合上下滑动阅读,整体轻量化、统一
  902. 必须遵守规则
  903. • 保持条漫纵向阅读逻辑,构图简洁
  904. • 画风统一,不混写实、3D、水墨
  905. • 画面干净,不杂乱
  906. 禁止/避免词汇
  907. 水印、文字、复杂光影、3D效果、赛博朋克、恐怖、血腥、杂乱背景"
  908. ],
  909. '赛博朋克风格' => [
  910. 'aliases' => ['赛博朋克风格', '赛博朋克'],
  911. 'prompt' => "风格说明
  912. 霓虹灯光、未来都市、暗色调、科技机械
  913. 正向提示词(中文)
  914. 赛博朋克风格,霓虹灯光,未来都市,暗色调,科技机械元素,高对比光影,风格统一强烈
  915. 必须遵守规则
  916. • 保持赛博朋克统一氛围,不混入古风、仙侠、水墨
  917. • 色调以暗调+霓虹为主,色彩统一
  918. • 画面科技感强,不杂乱
  919. 禁止/避免词汇
  920. 水印、文字、古风、仙侠、水墨、Q版、清新治愈、血腥过度、低俗"
  921. ],
  922. '暗黑悬疑风格' => [
  923. 'aliases' => ['暗黑悬疑风格', '暗黑悬疑'],
  924. 'prompt' => "风格说明
  925. 低饱和、冷色调、阴影重、神秘压抑
  926. 正向提示词(中文)
  927. 暗黑悬疑风格,低饱和度,冷色调,强阴影,光线昏暗,氛围神秘压抑,整体色调统一
  928. 必须遵守规则
  929. • 保持暗黑悬疑统一色调与氛围,不突然明亮清新
  930. • 画面氛围压抑神秘,但不低俗、不血腥
  931. • 角色形象统一,不崩坏
  932. 禁止/避免词汇
  933. 水印、文字、高饱和色彩、清新治愈、Q版、古风仙侠、过度血腥、恐怖猎奇"
  934. ],
  935. '治愈清新风格' => [
  936. 'aliases' => ['治愈清新风格', '治愈清新'],
  937. 'prompt' => "风格说明
  938. 马卡龙色系、柔光、温暖干净
  939. 正向提示词(中文)
  940. 治愈清新风格,马卡龙色系,柔光暖调,画面干净柔和,氛围温暖舒适,风格统一
  941. 必须遵守规则
  942. • 保持治愈清新统一色调,不阴暗、不诡异
  943. • 画面柔和干净,无杂乱元素
  944. • 角色形象柔和可爱,不狰狞
  945. 禁止/避免词汇
  946. 水印、文字、暗黑、恐怖、血腥、赛博朋克、高饱和刺眼色、畸形、杂乱"
  947. ],
  948. ];
  949. return $arr;
  950. }
  951. public function getArtStylePromptByInput($inputArtStyle): string
  952. {
  953. $normalizedInput = $this->normalizeArtStyleInput($inputArtStyle);
  954. if ($normalizedInput === '') {
  955. return '';
  956. }
  957. return $this->getArtStylePromptMappings($normalizedInput);
  958. // foreach ($this->getArtStylePromptMappings() as $mapping) {
  959. // foreach ($mapping['aliases'] as $alias) {
  960. // $normalizedAlias = $this->normalizeArtStyleInput($alias);
  961. // if ($normalizedAlias === '') {
  962. // continue;
  963. // }
  964. // if (strpos($normalizedInput, $normalizedAlias) !== false || strpos($normalizedAlias, $normalizedInput) !== false) {
  965. // return $mapping['prompt'];
  966. // }
  967. // }
  968. // }
  969. return '';
  970. }
  971. public function getArtStyleShortPromptByInput($inputArtStyle) {
  972. $stylePrefixes = [
  973. '唯美真人风格' => [
  974. 'character_prefix' => '唯美真人风格,电影级摄影,真实人物,高级质感,自然光影,肤质细腻,景深虚化,超高清,8K画质,情绪表达丰富',
  975. 'scene_prefix' => '唯美真人风格,电影感场景,真实环境,自然光影,高级色彩,细节丰富,超高清摄影,氛围感强'
  976. ],
  977. '真人古风风格' => [
  978. 'character_prefix' => '真人古风风格,东方古典美学,真实人物,古代服饰,精致妆容,电影级摄影,国风质感,超高清',
  979. 'scene_prefix' => '真人古风风格,东方建筑,古典园林,电影级光影,国风美学,真实场景,高细节,超高清'
  980. ],
  981. '日系动漫风格' => [
  982. 'character_prefix' => '日系动漫风格,二次元角色,精致线稿,高质量赛璐璐上色,细腻表情,动画电影质感,高细节',
  983. 'scene_prefix' => '日系动漫风格,动画电影场景,唯美背景,细腻光影,高质量绘制,色彩丰富,氛围感强'
  984. ],
  985. '国漫风格' => [
  986. 'character_prefix' => '国漫风格,高品质角色设计,东方审美,精致建模感,细腻刻画,动态表现强,电影级渲染',
  987. 'scene_prefix' => '国漫风格,东方幻想场景,精细绘制,大气构图,电影级光影,高品质渲染'
  988. ],
  989. 'Q版卡通风格' => [
  990. 'character_prefix' => 'Q版卡通风格,大头小身比例,可爱造型,圆润线条,丰富表情,萌系设计,高质量插画',
  991. 'scene_prefix' => 'Q版卡通风格,可爱场景,色彩鲜艳,童趣氛围,简洁细腻,绘本质感'
  992. ],
  993. '简约扁平风格' => [
  994. 'character_prefix' => '简约扁平风格,矢量插画,几何造型,简洁线条,干净配色,现代设计感',
  995. 'scene_prefix' => '简约扁平风格,矢量场景,几何构图,色彩简洁,现代感设计,视觉清爽'
  996. ],
  997. '武侠风格' => [
  998. 'character_prefix' => '武侠风格,东方侠客气质,江湖人物,飘逸服饰,凌厉气场,电影级光影,高细节绘制',
  999. 'scene_prefix' => '武侠风格,江湖场景,古道山林,客栈酒馆,刀光剑影,大气构图,东方美学'
  1000. ],
  1001. '古风仙侠风格' => [
  1002. 'character_prefix' => '古风仙侠风格,仙气飘逸,东方幻想角色,精致古装,灵气环绕,电影级特效,高品质渲染',
  1003. 'scene_prefix' => '古风仙侠风格,仙境场景,云海仙山,灵气缭绕,梦幻光效,东方幻想美学'
  1004. ],
  1005. '新中式水墨风格' => [
  1006. 'character_prefix' => '新中式水墨风格,国风写意,水墨晕染,留白艺术,东方韵味,诗意氛围,高级插画',
  1007. 'scene_prefix' => '新中式水墨风格,山水意境,墨韵渲染,留白构图,东方美学,诗意场景'
  1008. ],
  1009. '写实插画风格' => [
  1010. 'character_prefix' => '写实插画风格,半写实人物,细节丰富,真实比例,数字绘画,高质量渲染,电影级质感',
  1011. 'scene_prefix' => '写实插画风格,高细节环境,数字绘景,真实光影,层次丰富,电影感构图'
  1012. ],
  1013. '3D卡通风格' => [
  1014. 'character_prefix' => '3D卡通风格,高品质三维建模,皮克斯动画质感,表情生动,角色可爱,细腻渲染',
  1015. 'scene_prefix' => '3D卡通风格,动画电影场景,三维建模,色彩明快,光影柔和,高品质渲染'
  1016. ],
  1017. '条漫风格' => [
  1018. 'character_prefix' => '条漫风格,韩漫质感,精致线稿,人物颜值高,细腻上色,剧情漫画风,高质量绘制',
  1019. 'scene_prefix' => '条漫风格,漫画背景,剧情分镜感,细腻光影,现代漫画质感'
  1020. ],
  1021. '赛博朋克风格' => [
  1022. 'character_prefix' => '赛博朋克风格,未来科技感,机械义体,霓虹光效,高对比色彩,科幻都市氛围,电影级渲染',
  1023. 'scene_prefix' => '赛博朋克风格,未来都市,霓虹街道,全息广告,机械科技元素,夜景氛围,电影级光影'
  1024. ],
  1025. '暗黑悬疑风格' => [
  1026. 'character_prefix' => '暗黑悬疑风格,神秘压抑氛围,强烈明暗对比,电影级灯光,细腻刻画,悬疑感十足',
  1027. 'scene_prefix' => '暗黑悬疑风格,阴郁环境,低饱和色调,迷雾光影,神秘气氛,电影感构图'
  1028. ],
  1029. '治愈清新风格' => [
  1030. 'character_prefix' => '治愈清新风格,温暖柔和色调,干净画面,自然光线,亲和气质,轻松愉悦氛围,高品质插画',
  1031. 'scene_prefix' => '治愈清新风格,阳光明亮,自然环境,温暖色彩,舒适氛围,细腻绘制,疗愈感强'
  1032. ]
  1033. ];
  1034. return isset($stylePrefixes[$inputArtStyle]) ? $stylePrefixes[$inputArtStyle] : [];
  1035. }
  1036. private function normalizeArtStyleInput($value): string
  1037. {
  1038. $value = trim((string)$value);
  1039. if ($value === '') {
  1040. return '';
  1041. }
  1042. $value = str_replace(["\r", "\n", "\t", ' ', ' ', '-', '_', ':', ':', ',', ',', '。', '.'], '', $value);
  1043. return mb_strtolower($value, 'UTF-8');
  1044. }
  1045. private function replaceArtStyleSection(string $content, string $artStyle): string
  1046. {
  1047. if ($content === '' || $artStyle === '') {
  1048. return $content;
  1049. }
  1050. $pattern = '/###美术风格\s*\R(.*?)(?=\R###|$)/su';
  1051. if (!preg_match($pattern, $content)) {
  1052. return $content;
  1053. }
  1054. return preg_replace($pattern, "###美术风格\n" . trim($artStyle), $content, 1);
  1055. }
  1056. /**
  1057. * DeepSeek流式输出处理方法
  1058. *
  1059. * @param array $post_data DeepSeek API请求参数
  1060. * @return \Generator 返回生成器,用于流式输出
  1061. */
  1062. private function deepSeekStreamResponse($post_data) {
  1063. // 设置最大输出tokens
  1064. $post_data['max_tokens'] = 300000;
  1065. $client = new Client(['timeout' => 1800, 'verify' => false]);
  1066. $response = $client->post($this->url, [
  1067. 'json' => $post_data,
  1068. 'headers' => $this->headers,
  1069. 'stream' => true // 启用流式响应
  1070. ]);
  1071. $body = $response->getBody();
  1072. $fullContent = '';
  1073. $fullReasoningContent = '';
  1074. $usage = [];
  1075. $buffer = '';
  1076. dLog('deepseek')->info('开始读取 DeepSeek 流式响应');
  1077. // 逐行读取流式响应
  1078. while (!$body->eof()) {
  1079. $chunk = $body->read(1024); // 每次读取 1KB
  1080. $buffer .= $chunk;
  1081. // 按行分割
  1082. $lines = explode("\n", $buffer);
  1083. // 保留最后一个不完整的行
  1084. $buffer = array_pop($lines);
  1085. foreach ($lines as $line) {
  1086. $line = trim($line);
  1087. // 跳过空行
  1088. if (empty($line)) {
  1089. continue;
  1090. }
  1091. // 检查是否是 SSE 数据行
  1092. if (strpos($line, 'data: ') !== 0) {
  1093. dLog('deepseek')->warning('非 SSE 数据行', ['line' => $line]);
  1094. continue;
  1095. }
  1096. $data = substr($line, 6); // 移除 "data: " 前缀
  1097. if ($data === '[DONE]') {
  1098. dLog('deepseek')->info('收到结束标记');
  1099. break 2; // 跳出两层循环
  1100. }
  1101. try {
  1102. $json = json_decode($data, true);
  1103. if (json_last_error() !== JSON_ERROR_NONE) {
  1104. dLog('deepseek')->warning('JSON 解析错误: ' . json_last_error_msg(), ['data' => $data]);
  1105. continue;
  1106. }
  1107. if (isset($json['choices'][0]['delta'])) {
  1108. $delta = $json['choices'][0]['delta'];
  1109. // 处理思维链内容(仅 deepseek-reasoner 模型)
  1110. if (isset($delta['reasoning_content'])) {
  1111. $fullReasoningContent .= $delta['reasoning_content'];
  1112. yield [
  1113. 'type' => 'reasoning',
  1114. 'content' => $delta['reasoning_content'],
  1115. 'full_reasoning' => $fullReasoningContent
  1116. ];
  1117. }
  1118. // 处理最终回答内容
  1119. if (isset($delta['content'])) {
  1120. $fullContent .= $delta['content'];
  1121. yield [
  1122. 'type' => 'content',
  1123. 'content' => $delta['content'],
  1124. ];
  1125. }
  1126. }
  1127. // 获取使用统计信息
  1128. if (isset($json['usage'])) {
  1129. $usage = $json['usage'];
  1130. dLog('deepseek')->info('收到使用统计', ['usage' => $usage]);
  1131. }
  1132. } catch (\Exception $e) {
  1133. dLog('deepseek')->error('解析流式响应失败: ' . $e->getMessage(), ['line' => $line]);
  1134. continue;
  1135. }
  1136. }
  1137. }
  1138. dLog('deepseek')->info('流式读取完成', [
  1139. 'content_length' => strlen($fullContent),
  1140. 'reasoning_length' => strlen($fullReasoningContent)
  1141. ]);
  1142. yield [
  1143. 'type' => 'done',
  1144. 'full_content' => $fullContent,
  1145. 'full_reasoning' => $fullReasoningContent,
  1146. 'usage' => $usage
  1147. ];
  1148. }
  1149. /**
  1150. * GPT 流式输出处理方法
  1151. *
  1152. * @param array $post_data GPT API请求参数
  1153. * @return \Generator 返回生成器,用于流式输出
  1154. */
  1155. private function gpt54StreamResponse($post_data) {
  1156. $apiKey = env('GPT_54_API_KEY');
  1157. if (empty($apiKey)) {
  1158. Utils::throwError('20003:GPT API Key未配置');
  1159. }
  1160. $client = new Client(['timeout' => 1800, 'verify' => false]);
  1161. $headers = [
  1162. 'Authorization' => 'Bearer ' . $apiKey,
  1163. 'Content-Type' => 'application/json'
  1164. ];
  1165. // 移除 thinking 参数,GPT-5.4 不支持
  1166. if (isset($post_data['thinking'])) {
  1167. unset($post_data['thinking']);
  1168. }
  1169. if (isset($post_data['reasoning_effort'])) {
  1170. unset($post_data['reasoning_effort']);
  1171. }
  1172. $post_data['max_completion_tokens'] = 100000;
  1173. // 备用中转站地址1: https://token.ithinkai.cn/v1/chat/completions
  1174. // 备用中转站地址2: https://api.nonelinear.com/v1/chat/completions
  1175. $response = $client->post('https://ai-api.kkidc.com/v1/chat/completions', [
  1176. 'json' => $post_data,
  1177. 'headers' => $headers,
  1178. 'stream' => true // 启用流式响应
  1179. ]);
  1180. $body = $response->getBody();
  1181. $fullContent = '';
  1182. $usage = [];
  1183. $buffer = '';
  1184. dLog('deepseek')->info('开始读取 GPT 流式响应');
  1185. // 逐行读取流式响应
  1186. while (!$body->eof()) {
  1187. $chunk = $body->read(1024); // 每次读取 1KB
  1188. $buffer .= $chunk;
  1189. // 按行分割
  1190. $lines = explode("\n", $buffer);
  1191. // 保留最后一个不完整的行
  1192. $buffer = array_pop($lines);
  1193. foreach ($lines as $line) {
  1194. $line = trim($line);
  1195. // 跳过空行
  1196. if (empty($line)) {
  1197. continue;
  1198. }
  1199. // 检查是否是 SSE 数据行
  1200. if (strpos($line, 'data: ') !== 0) {
  1201. dLog('deepseek')->warning('非 SSE 数据行', ['line' => $line]);
  1202. continue;
  1203. }
  1204. $data = substr($line, 6); // 移除 "data: " 前缀
  1205. if ($data === '[DONE]') {
  1206. dLog('deepseek')->info('收到结束标记');
  1207. break 2; // 跳出两层循环
  1208. }
  1209. try {
  1210. $json = json_decode($data, true);
  1211. if (json_last_error() !== JSON_ERROR_NONE) {
  1212. dLog('deepseek')->warning('JSON 解析错误: ' . json_last_error_msg(), ['data' => $data]);
  1213. continue;
  1214. }
  1215. if (isset($json['choices'][0]['delta'])) {
  1216. $delta = $json['choices'][0]['delta'];
  1217. // 处理回答内容
  1218. if (isset($delta['content'])) {
  1219. $fullContent .= $delta['content'];
  1220. yield [
  1221. 'type' => 'content',
  1222. 'content' => $delta['content'],
  1223. ];
  1224. }
  1225. }
  1226. // 获取使用统计信息
  1227. if (isset($json['usage'])) {
  1228. $usage = $json['usage'];
  1229. dLog('deepseek')->info('收到使用统计', ['usage' => $usage]);
  1230. }
  1231. } catch (\Exception $e) {
  1232. dLog('deepseek')->error('解析流式响应失败: ' . $e->getMessage(), ['line' => $line]);
  1233. continue;
  1234. }
  1235. }
  1236. }
  1237. dLog('deepseek')->info('流式读取完成', [
  1238. 'content_length' => strlen($fullContent)
  1239. ]);
  1240. yield [
  1241. 'type' => 'done',
  1242. 'full_content' => $fullContent,
  1243. 'full_reasoning' => '',
  1244. 'usage' => $usage
  1245. ];
  1246. }
  1247. // 与推理模型对话
  1248. public function chatWithReasoner($data) {
  1249. $content = getProp($data, 'content');
  1250. $model = getProp($data, 'model', 'r1');
  1251. // 模型兼容性处理:deepseek-chat 和 deepseek-reasoner 映射到 deepseek-v4-flash
  1252. $thinkingMode = getProp($data, 'thinking', 'disabled'); // 默认非思考模式
  1253. $reasoningEffort = getProp($data, 'reasoning_effort', 'high'); // 默认思考强度
  1254. if ($model == 'r1') {
  1255. $model = 'deepseek-v4-flash'; // r1 映射到 deepseek-v4-flash 思考模式
  1256. $thinkingMode = 'disabled';
  1257. } else {
  1258. $model = 'deepseek-v4-flash'; // 默认映射到 deepseek-v4-flash 非思考模式
  1259. $thinkingMode = 'disabled';
  1260. }
  1261. // 获取可选情感(根据音色可支持情感选)
  1262. $timbre_emotion = DB::table('mp_timbres')->where('is_enabled', 1)->pluck('emotion')->toArray();
  1263. $emotion_list = [];
  1264. foreach ($timbre_emotion as $emotion) {
  1265. $tmp = explode(',', $emotion);
  1266. $emotion_list = array_merge($emotion_list, $tmp);
  1267. }
  1268. $emotion_list = array_unique($emotion_list);
  1269. $emotion_str = implode('、', $emotion_list);
  1270. // // 获取可选情感
  1271. // $emotion_list = DB::table('mp_emotion_list')->where('is_enabled', 1)->select('emotion_name')->orderBy('id')->get()->pluck('emotion_name')->toArray();
  1272. // $emotion_str = implode('、', $emotion_list);
  1273. // 是否启用情感
  1274. $enable_emotion = getProp($data, 'enable_emotion', 0);
  1275. if ($enable_emotion) {
  1276. $sys_content = "下面有一段小说文本,请帮我将文本中的每句话按从上到下的顺序拆分成角色不同的剧本文稿(不得更改上下文顺序和内容),文稿形式严格按照“角色名(男、女、中性):台词{情感}”输出,需要注意以下几点要求:
  1277. 1.角色名后不要加入任何其他词语,只能加性别,在男、女或中性中选
  1278. 2.非对话部分请全部用旁白角色代替,旁白的情感统一选择中性
  1279. 3.情感必须在【{$emotion_str}】中选一个,不得使用其他词语";
  1280. }else {
  1281. $sys_content = "下面有一段小说文本,请帮我将文本中的每句话按从上到下的顺序拆分成角色不同的剧本文稿(不得更改上下文顺序和内容),文稿形式严格按照“角色名(男、女、中性):台词”输出,需要注意以下几点要求:
  1282. 1.角色名后不要加入任何其他词语,只能加性别,在男、女或中性中选
  1283. 2.非对话部分请全部用旁白角色代替,旁白的情感统一选择中性";
  1284. }
  1285. $messages = [
  1286. [
  1287. 'role' => 'system',
  1288. 'content' => $sys_content
  1289. ],
  1290. [
  1291. 'role' => 'user',
  1292. 'content' => $content
  1293. ]
  1294. ];
  1295. $post_data = [
  1296. 'model' => $model,
  1297. 'messages' => $messages,
  1298. // 'max_tokens' => 8192,
  1299. 'temperature' => 1,
  1300. 'frequency_penalty' => 0,
  1301. 'presence_penalty' => 0,
  1302. 'thinking' => ['type' => $thinkingMode],
  1303. 'response_format' => [
  1304. 'type' => 'text'
  1305. ],
  1306. 'stream' => false
  1307. ];
  1308. if ($thinkingMode == 'enabled') $post_data['reasoning_effort'] = $reasoningEffort;
  1309. // 根据模型类型选择调用方法
  1310. $fullContent = '';
  1311. $usage = [];
  1312. if (in_array($model, ['deepseek-reasoner', 'deepseek-chat', 'deepseek-v4-flash', 'deepseek-v4-pro'])) {
  1313. // DeepSeek 官方模型使用 DeepSeek API
  1314. $chatResult = $this->chatOnly($post_data);
  1315. } else if (in_array($model, $this->gpt_text_models)) {
  1316. // GPT-5.4 模型使用 TokenRouter API
  1317. $chatResult = $this->gpt54ChatOnly($post_data);
  1318. } else {
  1319. // 其他模型使用火山引擎API
  1320. $chatResult = $this->volcEngineChatCompletion($post_data);
  1321. }
  1322. if (is_array($chatResult)) {
  1323. $fullContent = $chatResult['fullContent'];
  1324. $usage = $chatResult['usage'];
  1325. }
  1326. // 处理获取到的剧本数据
  1327. $script_content = handleScriptWords($fullContent, $enable_emotion);
  1328. $result = [
  1329. 'origin_content' => $fullContent,
  1330. 'roles' => getProp($script_content, 'roles'),
  1331. 'words' => getProp($script_content, 'words'),
  1332. ];
  1333. return $result;
  1334. }
  1335. public function resetParagraphAudio($data) {
  1336. $bid = getProp($data, 'bid');
  1337. $cid = getProp($data, 'cid');
  1338. $version_id = getProp($data, 'version_id');
  1339. if (!DB::table('mp_chapter_paragraph_audios')->where('bid', $bid)->where('cid', $cid)->where('version_id', $version_id)->value('id')) return true;
  1340. return DB::table('mp_chapter_paragraph_audios')->where('bid', $bid)->where('cid', $cid)->where('version_id', $version_id)->update([
  1341. 'generate_status' => '待制作',
  1342. 'updated_at' => date('Y-m-d H:i:s')
  1343. ]);
  1344. }
  1345. public function saveParagraphAudio($data) {
  1346. $bid = getProp($data, 'bid');
  1347. $cid = getProp($data, 'cid');
  1348. $version_id = getProp($data, 'version_id');
  1349. $sequence = getProp($data, 'sequence');
  1350. $id = DB::table('mp_chapter_paragraph_audios')->where('bid', $bid)->where('cid', $cid)->where('version_id', $version_id)->where('sequence', $sequence)->value('id');
  1351. // 获取所有情感
  1352. $emotion_list = DB::table('mp_emotion_list')->where('is_enabled', 1)->pluck('emotion_name', 'emotion_code')->toArray();
  1353. $emotion_list = array_flip($emotion_list);
  1354. // 获取音色支持情感
  1355. $timbre_emotion = DB::table('mp_timbres')->where('timbre_type', getProp($data, 'voice_type'))->value('emotion');
  1356. $timbre_emotion = explode(',', $timbre_emotion);
  1357. $emotion = getProp($data, 'emotion');
  1358. if (!in_array($emotion, $timbre_emotion)) $emotion = '中性';
  1359. $emotion_type = isset($emotion_list[$emotion]) ? $emotion_list[$emotion] : 'neutral';
  1360. $list = [
  1361. 'bid' => $bid,
  1362. 'cid' => $cid,
  1363. 'version_id' => $version_id,
  1364. 'sequence' => $sequence,
  1365. 'role' => getProp($data, 'role'),
  1366. 'gender' => getProp($data, 'gender'),
  1367. 'text' => trim(getProp($data, 'text')),
  1368. 'emotion' => $emotion,
  1369. 'emotion_type' => $emotion_type,
  1370. 'voice_type' => getProp($data, 'voice_type'),
  1371. 'voice_name' => getProp($data, 'voice_name'),
  1372. 'speed_ratio' => getProp($data, 'speed_ratio', 0),
  1373. 'loudness_ratio'=> getProp($data, 'loudness_ratio', 0),
  1374. 'emotion_scale' => getProp($data, 'emotion_scale', 4),
  1375. 'pitch' => getProp($data, 'pitch', 0),
  1376. // 'paragraph_audio_url' => '',
  1377. 'generate_status' => '制作中',
  1378. 'error_msg' => '',
  1379. 'updated_at' => date('Y-m-d H:i:s')
  1380. ];
  1381. $continue = false;
  1382. // 判断是否含有中文、数字或英文,如果没有则跳过合成
  1383. if (!preg_match('/[\x{4e00}-\x{9fa5}]+/u', $list['text']) && !preg_match('/[0-9]+/', $list['text']) && !preg_match('/[a-zA-Z]+/', $list['text'])) {
  1384. $list['generate_status'] = '制作成功';
  1385. $list['paragraph_audio_url'] = 'https://zw-audiobook.tos-cn-beijing.volces.com/effects/ellipses_2s.mp3';
  1386. $list['subtitle_info'] = '{"duration": 2.0, "words": []}';
  1387. $continue = true;
  1388. }
  1389. // if (getProp($data, 'paragraph_audio_url')) $list['paragraph_audio_url'] = getProp($data, 'paragraph_audio_url');
  1390. if ($id) {
  1391. $boolen = DB::table('mp_chapter_paragraph_audios')->where('id', $id)->update($list);
  1392. }else {
  1393. $list['created_at'] = date('Y-m-d H:i:s');
  1394. $id = DB::table('mp_chapter_paragraph_audios')->insertGetId($list);
  1395. $boolen = $id ? true : false;
  1396. }
  1397. // 如果更新成功则加入查询队列
  1398. if ($boolen) {
  1399. $redis_key = "select-{$bid}-{$version_id}-{$cid}";
  1400. Redis::sadd($redis_key, $id);
  1401. }
  1402. if ($boolen && !$continue) {
  1403. $boolen = false;
  1404. $client = new Client(['timeout' => 1800, 'verify' => false]);
  1405. // 根据ID通过API通知合成音频
  1406. // $result = $client->get("http://47.240.171.155:5000/api/previewTask?taskId={$id}");
  1407. $result = $client->get("http://122.9.129.83:5000/api/previewTask?taskId={$id}");
  1408. $response = $result->getBody()->getContents();
  1409. $response_arr = json_decode($response, true);
  1410. if (!isset($response_arr['code']) || (int)$response_arr['code'] !== 0) {
  1411. $error_msg = isset($response_arr['msg']) ? $response_arr['msg'] : '未知错误';
  1412. Log::info('通知火山生成段落音频失败: '.$error_msg);
  1413. Utils::throwError('20003:通知火山生成段落音频失败');
  1414. }
  1415. $boolen = true;
  1416. }
  1417. return $boolen;
  1418. }
  1419. public function insertAudioEffect($data) {
  1420. $audio_id = getProp($data, 'audio_id');
  1421. $chapter_audio = DB::table('mp_chapter_audios')->where('id', $audio_id)->first();
  1422. $bid = getProp($chapter_audio, 'bid');
  1423. $version_id = getProp($chapter_audio, 'version_id');
  1424. $cid = getProp($chapter_audio, 'cid');
  1425. if (getProp($chapter_audio, 'generate_status') != '制作成功') Utils::throwError('20003:请先完成有声制作');
  1426. $audio_effect_json = getProp($data, 'audio_effect_json');
  1427. if (!is_string($audio_effect_json)) $audio_effect_json = json_encode($audio_effect_json, 256);
  1428. try {
  1429. DB::beginTransaction();
  1430. $count = DB::table('mp_audio_tasks')->where('audio_id', $audio_id)->count('id');
  1431. if (!$count) {
  1432. $task_name = getProp($chapter_audio, 'book_name').' '.getProp($chapter_audio, 'chapter_name').'【'.getProp($chapter_audio, 'version_name').'】';
  1433. }else {
  1434. $task_name = getProp($chapter_audio, 'book_name').' '.getProp($chapter_audio, 'chapter_name').'【'.getProp($chapter_audio, 'version_name').'】('.($count+1).')';
  1435. }
  1436. $boolen1 = DB::table('mp_chapter_audios')->where('id', $audio_id)->update([
  1437. 'audio_effect_status' => '添加中',
  1438. 'audio_effect_json' => $audio_effect_json,
  1439. 'updated_at' => date('Y-m-d H:i:s')
  1440. ]);
  1441. if (!$boolen1) {
  1442. DB::rollBack();
  1443. Utils::throwError('20003:更新生成数据失败');
  1444. }
  1445. $id = DB::table('mp_audio_tasks')->insertGetId([
  1446. 'audio_id' => $audio_id,
  1447. 'generate_status' => '执行中',
  1448. 'generate_json' => json_encode([], 256),
  1449. 'bid' => $bid,
  1450. 'book_name' => getProp($chapter_audio, 'book_name'),
  1451. 'version_id' => $version_id,
  1452. 'version_name' => getProp($chapter_audio, 'version_name'),
  1453. 'cid' => $cid,
  1454. 'chapter_name' => getProp($chapter_audio, 'chapter_name'),
  1455. 'task_name' => $task_name,
  1456. 'created_at' => date('Y-m-d H:i:s'),
  1457. 'updated_at' => date('Y-m-d H:i:s')
  1458. ]);
  1459. if (!$id) {
  1460. DB::rollBack();
  1461. Utils::throwError('20003:创建任务失败');
  1462. }
  1463. }catch (\Exception $e) {
  1464. DB::rollBack();
  1465. Utils::throwError('20003:'.$e->getMessage());
  1466. }
  1467. DB::commit();
  1468. $client = new Client(['timeout' => 1800, 'verify' => false]);
  1469. // 根据ID通过API通知合成音频
  1470. // $result = $client->get("http://47.240.171.155:5000/api/postTask?taskId={$id}");
  1471. $result = $client->get("http://122.9.129.83:5000/api/postTask?taskId={$id}");
  1472. $response = $result->getBody()->getContents();
  1473. $response_arr = json_decode($response, true);
  1474. if (!isset($response_arr['code']) || (int)$response_arr['code'] !== 0) {
  1475. $error_msg = isset($response_arr['msg']) ? $response_arr['msg'] : '未知错误';
  1476. Log::info('通知火山插入音效失败: '.$error_msg);
  1477. Utils::throwError('20003:通知火山插入音效失败');
  1478. }
  1479. return true;
  1480. }
  1481. public function insertBgm($data) {
  1482. $audio_id = getProp($data, 'audio_id');
  1483. $chapter_audio = DB::table('mp_chapter_audios')->where('id', $audio_id)->first();
  1484. $bid = getProp($chapter_audio, 'bid');
  1485. $version_id = getProp($chapter_audio, 'version_id');
  1486. $cid = getProp($chapter_audio, 'cid');
  1487. if (getProp($chapter_audio, 'generate_status') != '制作成功') Utils::throwError('20003:请先完成有声制作');
  1488. $bgm_json = getProp($data, 'bgm_json');
  1489. if (!is_string($bgm_json)) $bgm_json = json_encode($bgm_json, 256);
  1490. if (!$bgm_json) {
  1491. $bgm_json = getProp($data, 'audio_effect_json');
  1492. if (!is_string($bgm_json)) $bgm_json = json_encode($bgm_json, 256);
  1493. }
  1494. try {
  1495. DB::beginTransaction();
  1496. $count = DB::table('mp_audio_tasks')->where('audio_id', $audio_id)->count('id');
  1497. if (!$count) {
  1498. $task_name = getProp($chapter_audio, 'book_name').' '.getProp($chapter_audio, 'chapter_name').'【'.getProp($chapter_audio, 'version_name').'】';
  1499. }else {
  1500. $task_name = getProp($chapter_audio, 'book_name').' '.getProp($chapter_audio, 'chapter_name').'【'.getProp($chapter_audio, 'version_name').'】('.($count+1).')';
  1501. }
  1502. $boolen1 = DB::table('mp_chapter_audios')->where('id', $audio_id)->update([
  1503. 'bgm_status' => '添加中',
  1504. 'bgm_json' => $bgm_json,
  1505. 'updated_at' => date('Y-m-d H:i:s')
  1506. ]);
  1507. if (!$boolen1) {
  1508. DB::rollBack();
  1509. Utils::throwError('20003:更新生成数据失败');
  1510. }
  1511. $id = DB::table('mp_audio_tasks')->insertGetId([
  1512. 'audio_id' => $audio_id,
  1513. 'generate_status' => '执行中',
  1514. 'generate_json' => json_encode([], 256),
  1515. 'bid' => $bid,
  1516. 'book_name' => getProp($chapter_audio, 'book_name'),
  1517. 'version_id' => $version_id,
  1518. 'version_name' => getProp($chapter_audio, 'version_name'),
  1519. 'cid' => $cid,
  1520. 'chapter_name' => getProp($chapter_audio, 'chapter_name'),
  1521. 'task_name' => $task_name,
  1522. 'created_at' => date('Y-m-d H:i:s'),
  1523. 'updated_at' => date('Y-m-d H:i:s')
  1524. ]);
  1525. if (!$id) {
  1526. DB::rollBack();
  1527. Utils::throwError('20003:创建任务失败');
  1528. }
  1529. }catch (\Exception $e) {
  1530. DB::rollBack();
  1531. Utils::throwError('20003:'.$e->getMessage());
  1532. }
  1533. DB::commit();
  1534. $client = new Client(['timeout' => 1800, 'verify' => false]);
  1535. // 根据ID通过API通知合成音频
  1536. // $result = $client->get("http://47.240.171.155:5000/api/postTask?taskId={$id}");
  1537. $result = $client->get("http://122.9.129.83:5000/api/postTask?taskId={$id}");
  1538. $response = $result->getBody()->getContents();
  1539. $response_arr = json_decode($response, true);
  1540. if (!isset($response_arr['code']) || (int)$response_arr['code'] !== 0) {
  1541. $error_msg = isset($response_arr['msg']) ? $response_arr['msg'] : '未知错误';
  1542. Log::info('通知火山插入bgm失败: '.$error_msg);
  1543. Utils::throwError('20003:通知火山插入bgm失败');
  1544. }
  1545. return true;
  1546. }
  1547. public function getParagraphAudios($data) {
  1548. $bid = getProp($data, 'bid');
  1549. $cid = getProp($data, 'cid');
  1550. $version_id = getProp($data, 'version_id');
  1551. $sequence = getProp($data, 'sequence');
  1552. // 获取已生成的音频
  1553. $query = DB::table('mp_chapter_paragraph_audios')->where('bid', $bid)->where('cid', $cid)->where('version_id', $version_id);
  1554. if ($sequence) $query->where('sequence', $sequence);
  1555. $paragraph_audios = $query->orderBy('sequence')->get();
  1556. $result = [];
  1557. foreach($paragraph_audios as $item) {
  1558. $result[] = [
  1559. 'sequence' => getProp($item, 'sequence'),
  1560. 'role' => getProp($item, 'role'),
  1561. 'gender' => getProp($item, 'gender'),
  1562. 'text' => trim(getProp($item, 'text')),
  1563. 'emotion' => getProp($item, 'emotion'),
  1564. 'emotion_type' => getProp($item, 'emotion_type'),
  1565. 'voice_type' => getProp($item, 'voice_type'),
  1566. 'voice_name' => getProp($item, 'voice_name'),
  1567. 'speed_ratio' => getProp($item, 'speed_ratio'),
  1568. 'loudness_ratio' => getProp($item, 'loudness_ratio'),
  1569. 'emotion_scale' => getProp($item, 'emotion_scale'),
  1570. 'paragraph_audio_url' => getProp($item, 'paragraph_audio_url'),
  1571. 'subtitle_info' => json_decode(getProp($item, 'subtitle_info'), true),
  1572. ];
  1573. }
  1574. return $result;
  1575. }
  1576. // 新增合成任务
  1577. public function addGenerateTask($data) {
  1578. $bid = getProp($data, 'bid');
  1579. $cid = getProp($data, 'cid');
  1580. $version_id = getProp($data, 'version_id');
  1581. $generate_json = getProp($data, 'generate_json');
  1582. // 获取已生成的音频
  1583. $paragraph_audios = DB::table('mp_chapter_paragraph_audios')->where('bid', $bid)->where('cid', $cid)->where('version_id', $version_id)->get();
  1584. $paragraph_list = [];
  1585. foreach($paragraph_audios as $item) {
  1586. $paragraph_list[getProp($item, 'sequence')] = [
  1587. 'role' => getProp($item, 'role'),
  1588. 'gender' => getProp($item, 'gender'),
  1589. 'text' => trim(getProp($item, 'text')),
  1590. 'emotion' => getProp($item, 'emotion'),
  1591. 'emotion_type' => getProp($item, 'emotion_type'),
  1592. 'voice_type' => getProp($item, 'voice_type'),
  1593. 'voice_name' => getProp($item, 'voice_name'),
  1594. 'speed_ratio' => getProp($item, 'speed_ratio'),
  1595. 'loudness_ratio' => getProp($item, 'loudness_ratio'),
  1596. 'emotion_scale' => getProp($item, 'emotion_scale'),
  1597. 'paragraph_audio_url' => getProp($item, 'paragraph_audio_url'),
  1598. ];
  1599. }
  1600. // 更新角色-音色信息
  1601. $existed_role_info = DB::table('mp_book_version')->where('bid', $bid)->where('id', $version_id)->value('role_info');
  1602. $existed_role_info = json_decode($existed_role_info, true);
  1603. if (!$existed_role_info) $existed_role_info = [];
  1604. // 获取情感信息
  1605. $emotion_list = DB::table('mp_emotion_list')->where('is_enabled', 1)->pluck('emotion_name', 'emotion_code')->toArray();
  1606. $emotion_list = array_flip($emotion_list);
  1607. // 获取音色对应情感组
  1608. $timbre_emotions = DB::table('mp_timbres')->where('is_enabled', 1)->select('timbre_type', 'emotion')->get();
  1609. $timbre_emotion_list = [];
  1610. foreach($timbre_emotions as $item) {
  1611. $timbre_emotion_list[getProp($item, 'timbre_type')] = explode(',', getProp($item, 'emotion'));
  1612. }
  1613. // 构造生成音频的json
  1614. $words = json_decode($generate_json, true);
  1615. // 最终合成前的参数组
  1616. $mp_chapter_paragraph_audios = [];
  1617. $sequence = 1;
  1618. $complete_paragraph_sequences = [];
  1619. foreach($words as &$word) {
  1620. if (!isset($word['text']) || !isset($word['emotion']) || !isset($word['voice_type']) || !isset($word['voice_name']) || !isset($word['speed_ratio']) || !isset($word['loudness_ratio']) || !isset($word['emotion_scale'])) Utils::throwError('20003:参数格式有误');
  1621. if (!($word['text']) || !($word['voice_type']) || !($word['voice_name'])) Utils::throwError('20003:参数不得为空');
  1622. $role = getProp($word, 'role');
  1623. $word['gender'] = (int)$word['gender'];
  1624. // 判断音色对应情感是否支持,不支持则调整为中性
  1625. $access_emotion = isset($timbre_emotion_list[$word['voice_type']]) ? $timbre_emotion_list[$word['voice_type']] : [];
  1626. if (!in_array($word['emotion'], $access_emotion)) $word['emotion'] = '中性';
  1627. // 如果有对应情感则赋值,没有则默认为中性(neutral)
  1628. if (isset($emotion_list[getProp($word, 'emotion')])) {
  1629. $word['emotion_type'] = $emotion_list[getProp($word, 'emotion')];
  1630. }else {
  1631. $word['emotion'] = '中性';
  1632. $word['emotion_type'] = 'neutral';
  1633. }
  1634. $existed_role_info[$role] = [
  1635. 'timbre_type' => $word['voice_type'],
  1636. 'timbre_name' => $word['voice_name'],
  1637. 'emotion' => $word['emotion'],
  1638. 'emotion_type' => $word['emotion_type'],
  1639. 'speed_ratio' => $word['speed_ratio'],
  1640. 'loudness_ratio'=> $word['loudness_ratio'],
  1641. 'emotion_scale' => $word['emotion_scale'],
  1642. 'pitch' => $word['pitch']
  1643. ];
  1644. $word['paragraph_audio_url'] = '';
  1645. // 判断生成参数是否相同,相同则直接使用已生成的音频
  1646. $paragraph = isset($paragraph_list[$sequence]) ? $paragraph_list[$sequence] : [];
  1647. // 如果音频存在并且参数都未改变则使用已生成的音频
  1648. if (getProp($paragraph, 'paragraph_audio_url')) {
  1649. // if (getProp($paragraph, 'role') == getProp($word, 'role') && getProp($paragraph, 'text') == getProp($word, 'text') &&
  1650. // getProp($paragraph, 'voice_type') == getProp($word, 'voice_type') &&
  1651. // getProp($paragraph, 'speed_ratio') == getProp($word, 'speed_ratio') && getProp($paragraph, 'loudness_ratio') == getProp($word, 'loudness_ratio') &&
  1652. // getProp($paragraph, 'emotion_scale') == getProp($word, 'emotion_scale'))
  1653. // {
  1654. // $word['paragraph_audio_url'] = getProp($paragraph, 'paragraph_audio_url');
  1655. // }
  1656. $word['paragraph_audio_url'] = getProp($paragraph, 'paragraph_audio_url');
  1657. }
  1658. $tmp = $word;
  1659. // 组装章节分句音频数据
  1660. // $tmp['sequence'] = getProp($word, 'sequence');
  1661. // $tmp['text'] = getProp($word, 'text');
  1662. // $tmp['emotion'] = getProp($word, 'emotion');
  1663. // $tmp['emotion_type'] = getProp($word, 'emotion_type');
  1664. // $tmp['voice_name'] = getProp($word, 'voice_name');
  1665. // $tmp['voice_type'] = getProp($word, 'voice_type');
  1666. // $tmp['speed_ratio'] = getProp($word, 'speed_ratio');
  1667. // $tmp['loudness_ratio'] = getProp($word, 'loudness_ratio');
  1668. // $tmp['emotion_scale'] = getProp($word, 'emotion_scale');
  1669. $tmp['bid'] = $bid;
  1670. $tmp['version_id'] = $version_id;
  1671. $tmp['cid'] = $cid;
  1672. $tmp['sequence'] = $sequence;
  1673. $tmp['created_at'] = date('Y-m-d H:i:s');
  1674. $tmp['updated_at'] = date('Y-m-d H:i:s');
  1675. if (!getProp($tmp, 'paragraph_audio_url')) Utils::throwError('20003:段落未生成音频,请等待完成后提交');
  1676. $mp_chapter_paragraph_audios[] = $tmp;
  1677. $complete_paragraph_sequences[] = $sequence;
  1678. $sequence++;
  1679. }
  1680. $generate_json = json_encode($words, 256);
  1681. // 判断是否有未生成字幕的段落
  1682. $no_subtitle_sequences = DB::table('mp_chapter_paragraph_audios')->where('bid', $bid)->where('cid', $cid)->where('version_id', $version_id)->whereIn('sequence', $complete_paragraph_sequences)->whereNull('subtitle_info')->pluck('sequence')->toArray();
  1683. if ($no_subtitle_sequences) {
  1684. Utils::throwError('20003:请先将以下段落序号生成字幕('.implode('、', $no_subtitle_sequences).')');
  1685. }
  1686. try {
  1687. DB::beginTransaction();
  1688. $role_info = json_encode($existed_role_info, 256);
  1689. $boolen = DB::table('mp_book_version')->where('bid', $bid)->where('id', $version_id)->update(['role_info' => $role_info, 'updated_at' => date('Y-m-d H:i:s')]);
  1690. if (!$boolen) {
  1691. DB::rollBack();
  1692. Utils::throwError('20003:更新角色信息失败');
  1693. }
  1694. $count = DB::table('mp_audio_tasks')->where('bid', $bid)->where('version_id', $version_id)->where('cid', $cid)->count('id');
  1695. $chapter_audio = DB::table('mp_chapter_audios')->where('bid', $bid)->where('version_id', $version_id)->where('cid', $cid)->first();
  1696. if (!$count) {
  1697. $task_name = getProp($chapter_audio, 'book_name').' '.getProp($chapter_audio, 'chapter_name').'【'.getProp($chapter_audio, 'version_name').'】';
  1698. }else {
  1699. $task_name = getProp($chapter_audio, 'book_name').' '.getProp($chapter_audio, 'chapter_name').'【'.getProp($chapter_audio, 'version_name').'】('.($count+1).')';
  1700. }
  1701. $boolen1 = DB::table('mp_chapter_audios')->where('bid', $bid)->where('version_id', $version_id)->where('cid', $cid)->update(['generate_status'=>'制作中', 'generate_json' => $generate_json, 'updated_at' => date('Y-m-d H:i:s')]);
  1702. if (!$boolen1) {
  1703. DB::rollBack();
  1704. Utils::throwError('20003:更新生成数据失败');
  1705. }
  1706. $id = DB::table('mp_audio_tasks')->insertGetId([
  1707. 'audio_id' => getProp($chapter_audio, 'id'),
  1708. 'generate_status' => '执行中',
  1709. 'generate_json' => $generate_json,
  1710. 'bid' => $bid,
  1711. 'book_name' => getProp($chapter_audio, 'book_name'),
  1712. 'version_id' => $version_id,
  1713. 'version_name' => getProp($chapter_audio, 'version_name'),
  1714. 'cid' => $cid,
  1715. 'chapter_name' => getProp($chapter_audio, 'chapter_name'),
  1716. 'task_name' => $task_name,
  1717. 'created_at' => date('Y-m-d H:i:s'),
  1718. 'updated_at' => date('Y-m-d H:i:s')
  1719. ]);
  1720. if (!$id) {
  1721. DB::rollBack();
  1722. Utils::throwError('20003:创建任务失败');
  1723. }
  1724. // 删除不在段落序号范围内的其他数据
  1725. if ($complete_paragraph_sequences) {
  1726. DB::table('mp_chapter_paragraph_audios')->where('bid', $bid)->where('version_id', $version_id)->where('cid', $cid)->whereNotIn('sequence', $complete_paragraph_sequences)->delete();
  1727. }
  1728. // // 删除章节分句音频数据并重新插入
  1729. // DB::table('mp_chapter_paragraph_audios')->where('bid', $bid)->where('version_id', $version_id)->where('cid', $cid)->delete();
  1730. // $boolen3 = DB::table('mp_chapter_paragraph_audios')->insert($mp_chapter_paragraph_audios);
  1731. // if (!$boolen3) {
  1732. // DB::rollBack();
  1733. // Utils::throwError('20003:更新章节分句音频失败');
  1734. // }
  1735. } catch (\Exception $e) {
  1736. DB::rollBack();
  1737. Utils::throwError('20003:'.$e->getMessage());
  1738. }
  1739. DB::commit();
  1740. // 通知火山生成音频
  1741. $client = new Client(['timeout' => 1800, 'verify' => false]);
  1742. // 根据ID通过API通知合成音频
  1743. // $result = $client->get("http://47.240.171.155:5000/api/chapterTask?taskId={$id}");
  1744. $result = $client->get("http://122.9.129.83:5000/api/chapterTask?taskId={$id}");
  1745. $response = $result->getBody()->getContents();
  1746. $response_arr = json_decode($response, true);
  1747. if (!isset($response_arr['code']) || (int)$response_arr['code'] !== 0) {
  1748. $error_msg = isset($response_arr['msg']) ? $response_arr['msg'] : '未知错误';
  1749. Log::info('通知火山生成音频失败: '.$error_msg);
  1750. Utils::throwError('20003:通知火山生成音频失败');
  1751. }
  1752. return true;
  1753. }
  1754. public function timbreList($data) {
  1755. $gender = getProp($data, 'gender');
  1756. $timbre_name = getProp($data, 'voice_name');
  1757. $category_id = getProp($data, 'category_id');
  1758. $age_stage = getProp($data, 'age_stage');
  1759. $query = DB::table('mp_timbres')->where('is_enabled', 1)->select('timbre_name as voice_name', 'timbre_type as voice_type', 'gender', 'age_stage', 'language', 'emotion', 'label', 'first_category_id', 'first_category_name', 'second_category_id', 'second_category_name', 'third_category_id', 'third_category_name', 'audio_url');
  1760. if ($gender) {
  1761. $query->where('gender', $gender);
  1762. }
  1763. if ($timbre_name) {
  1764. $query->where('timbre_name', 'like', "%{$timbre_name}%");
  1765. }
  1766. if ($category_id) {
  1767. $query->where(function ($query) use ($category_id) {
  1768. $query->where('first_category_id', $category_id)->orWhere('second_category_id', $category_id)->orWhere('third_category_id', $category_id);
  1769. });
  1770. }
  1771. if ($age_stage) {
  1772. $query->where('age_stage', $age_stage);
  1773. }
  1774. $list = $query->get()->map(function ($value) {
  1775. $value = (array)$value;
  1776. $value['voice_name'] = str_replace('(多情感)', '', $value['voice_name']);
  1777. return $value;
  1778. })->toArray();
  1779. return $list;
  1780. }
  1781. // 生成火山临时token
  1782. public function setStsToken() {
  1783. // ************* 配置参数 *************
  1784. $method = 'GET';
  1785. $service = 'sts';
  1786. $host = 'open.volcengineapi.com';
  1787. $region = env('VOLC_REGION');
  1788. $endpoint = 'https://open.volcengineapi.com';
  1789. // $endpoint = 'https://tos-cn-beijing.volces.com';
  1790. $access_key = env('VOLC_AK');
  1791. $secret_key = env('VOLC_SK');
  1792. // 获取缓存中的token,如果没有则请求接口
  1793. $token = Redis::get('volc_sts_token');
  1794. if (!$token) {
  1795. // 查询参数
  1796. $query_parameters = [
  1797. 'Action' => 'AssumeRole',
  1798. 'RoleSessionName' => 'user@zw',
  1799. 'RoleTrn' => 'trn:iam::2102575520:role/tos_role',
  1800. 'Version' => '2018-01-01'
  1801. ];
  1802. // 生成URL编码的查询字符串
  1803. $request_parameters = http_build_query($query_parameters, '', '&', PHP_QUERY_RFC3986);
  1804. // 获取签名头信息
  1805. $headers = $this->getSignHeaders($method, $service, $host, $region, $request_parameters, $access_key, $secret_key);
  1806. // 构建完整URL
  1807. $request_url = $endpoint . '?' . $request_parameters;
  1808. $client = new Client(['verify' => false]);
  1809. $response = $client->get($request_url, ['headers' => $headers]);
  1810. $response_arr = json_decode($response->getBody()->getContents(), true);
  1811. $result = [
  1812. 'SessionToken' => isset($response_arr['Result']['Credentials']['SessionToken']) ? $response_arr['Result']['Credentials']['SessionToken'] : '',
  1813. 'AccessKeyId' => isset($response_arr['Result']['Credentials']['AccessKeyId']) ? $response_arr['Result']['Credentials']['AccessKeyId'] : '',
  1814. 'SecretAccessKey' => isset($response_arr['Result']['Credentials']['SecretAccessKey']) ? $response_arr['Result']['Credentials']['SecretAccessKey'] : '',
  1815. 'Region' => env('VOLC_REGION'),
  1816. 'Endpoint' => env('VOLC_END_POINT'),
  1817. 'Bucket' => env('VOLC_BUCKET'),
  1818. ];
  1819. // 缓存token
  1820. Redis::setex('volc_sts_token', 3000, json_encode($result));
  1821. return $result;
  1822. } else {
  1823. return json_decode($token, true);
  1824. }
  1825. // $response = $response['Response'];
  1826. // $access_key = $response['Credentials']['AccessKeyId'];
  1827. // $secret_key = $response['Credentials']['AccessKeySecret'];
  1828. // $security_token = $response['Credentials']['SecurityToken'];
  1829. // $expiration = $response['Credentials']['Expiration'];
  1830. // dd($response_arr);
  1831. }
  1832. public function emotionGroups($data) {
  1833. $id = getProp($data, 'group_id');
  1834. $group_name = getProp($data, 'group_name');
  1835. $voice_type = getProp($data, 'voice_type');
  1836. $query = DB::table('mp_emotion_groups')->where('is_enabled', 1)->select('id as group_id', 'group_name',
  1837. 'emotion', 'emotion_type', 'voice_name', 'voice_type', 'speed_ratio', 'loudness_ratio', 'emotion_scale',
  1838. 'pitch', 'generate_status', 'audio_url', 'error_msg');
  1839. if ($group_name) {
  1840. $query->where('group_name', 'like', "%{$group_name}%");
  1841. }
  1842. if ($id) {
  1843. $query->where('id', $id);
  1844. }
  1845. if ($voice_type) {
  1846. $query->where('voice_type', $voice_type);
  1847. }
  1848. return $query->orderBy('id')->get()->map(function ($value) {
  1849. return (array)$value;
  1850. })->toArray();
  1851. }
  1852. private function sign($key, $msg) {
  1853. return hash_hmac('sha256', $msg, $key, true);
  1854. }
  1855. // 生成签名密钥
  1856. private function getSignatureKey($key, $dateStamp, $regionName, $serviceName) {
  1857. $kDate = $this->sign($key, $dateStamp);
  1858. $kRegion = $this->sign($kDate, $regionName);
  1859. $kService = $this->sign($kRegion, $serviceName);
  1860. $kSigning = $this->sign($kService, 'request');
  1861. return $kSigning;
  1862. }
  1863. // 获取签名头信息
  1864. private function getSignHeaders($method, $service, $host, $region, $request_parameters, $access_key, $secret_key) {
  1865. $contenttype = 'application/x-www-form-urlencoded';
  1866. $accept = 'application/json';
  1867. // 获取当前UTC时间
  1868. $t = new DateTime('now', new DateTimeZone('UTC'));
  1869. $xdate = $t->format('Ymd\THis\Z');
  1870. $datestamp = $t->format('Ymd');
  1871. // 1. 拼接规范请求串
  1872. $canonical_uri = '/';
  1873. $canonical_querystring = $request_parameters;
  1874. $canonical_headers = "content-type:{$contenttype}\nhost:{$host}\nx-date:{$xdate}\n";
  1875. $signed_headers = 'content-type;host;x-date';
  1876. // 空请求体的SHA256哈希
  1877. $payload_hash = hash('sha256', '');
  1878. $canonical_request = implode("\n", [
  1879. $method,
  1880. $canonical_uri,
  1881. $canonical_querystring,
  1882. $canonical_headers,
  1883. $signed_headers,
  1884. $payload_hash
  1885. ]);
  1886. // 2. 拼接待签名字符串
  1887. $algorithm = 'HMAC-SHA256';
  1888. $credential_scope = implode('/', [$datestamp, $region, $service, 'request']);
  1889. $hashed_canonical_request = hash('sha256', $canonical_request);
  1890. $string_to_sign = implode("\n", [
  1891. $algorithm,
  1892. $xdate,
  1893. $credential_scope,
  1894. $hashed_canonical_request
  1895. ]);
  1896. // 3. 计算签名
  1897. $signing_key = $this->getSignatureKey($secret_key, $datestamp, $region, $service);
  1898. $signature = hash_hmac('sha256', $string_to_sign, $signing_key);
  1899. // 4. 添加签名到请求头
  1900. $authorization_header = sprintf(
  1901. '%s Credential=%s/%s, SignedHeaders=%s, Signature=%s',
  1902. $algorithm,
  1903. $access_key,
  1904. $credential_scope,
  1905. $signed_headers,
  1906. $signature
  1907. );
  1908. return [
  1909. 'Accept' => $accept,
  1910. 'Content-Type' => $contenttype,
  1911. 'X-Date' => $xdate,
  1912. 'Authorization' => $authorization_header
  1913. ];
  1914. }
  1915. // 文字合成语音(火山引擎)
  1916. public function tts($data) {
  1917. $url = 'https://openspeech.bytedance.com/api/v1/tts';
  1918. $headers = [
  1919. 'Authorization' => 'Bearer;'.env('VOLC_TOKEN'),
  1920. 'Content-Type' => 'application/json; charset=UTF-8'
  1921. ];
  1922. $post_data = [
  1923. 'app' => [
  1924. 'appid' => env('VOLC_APPID'),
  1925. 'token' => env('VOLC_TOKEN'),
  1926. 'cluster' => 'volcano_tts'
  1927. ],
  1928. 'user' => [
  1929. 'uid' => 'mp_audio'
  1930. ],
  1931. // 'audio' => [
  1932. // 'voice_type' =>
  1933. // ],
  1934. ];
  1935. }
  1936. public function scriptList($data) {
  1937. $uid = Site::getUid();
  1938. $script_id = getProp($data, 'script_id');
  1939. $script_name = getProp($data, 'script_name');
  1940. $query = DB::table('mp_scripts')->where('is_deleted', 0)->where('user_id', $uid)->select('*');
  1941. if ($script_id) {
  1942. $query->where('id', $script_id);
  1943. }
  1944. if ($script_name) {
  1945. $query->where('script_name', 'like', "%{$script_name}%");
  1946. }
  1947. return $query->orderBy('created_at', 'desc')->paginate(12);
  1948. }
  1949. public function scripts($data) {
  1950. $uid = Site::getUid();
  1951. $script_id = getProp($data, 'script_id');
  1952. $script_name = getProp($data, 'script_name');
  1953. $query = DB::table('mp_scripts')->where('is_deleted', 0)->where('user_id', $uid)->select('id as script_id', 'script_name');
  1954. if ($script_id) {
  1955. $query->where('id', $script_id);
  1956. }
  1957. if ($script_name) {
  1958. $query->where('script_name', 'like', "%{$script_name}%");
  1959. }
  1960. return $query->orderBy('created_at', 'desc')->get()->map(function ($value) {
  1961. return (array)$value;
  1962. })->toArray();
  1963. }
  1964. public function scriptInfo($data) {
  1965. $uid = Site::getUid();
  1966. $script_id = getProp($data, 'script_id');
  1967. $script = DB::table('mp_scripts')->where('id', $script_id)->where('is_deleted', 0)->where('user_id', $uid)
  1968. // ->selectRaw('id as script_id, script_name, intro, highlights, roles, role_relationship, core_contradiction, art_style, scenes, status, start_episode_sequence, end_episode_sequence, episode_num, remark, created_at')->first();
  1969. ->selectRaw('id as script_id, script_name')->first();
  1970. if (!$script) Utils::throwError('20003:该剧本不存在!');
  1971. $script = (array)$script;
  1972. // 获取分集组信息
  1973. $groups = DB::table('mp_script_episode_group')->where('script_id', $script_id)->where('is_deleted', 0)
  1974. ->selectRaw('id as group_id, script_id, group_name, start_episode_number, end_episode_number, intro, highlights, roles, role_relationship, core_contradiction, art_style, scenes, status, episode_content')
  1975. ->orderBy('start_episode_number')->orderBy('id')->get()->map(function ($value) {
  1976. $value = (array)$value;
  1977. $value['episode_content'] = mb_substr($value['episode_content'], 0, 200);
  1978. return (array)$value;
  1979. })->toArray();
  1980. $script['group'] = $groups;
  1981. return $script;
  1982. }
  1983. public function createScript($data) {
  1984. $script_name = getProp($data, 'script_name');
  1985. $uid = Site::getUid(); // 获取当前用户ID
  1986. $cpid = Site::getCpid(); // 获取当前公司组ID
  1987. if (!$script_name) Utils::throwError('20003:剧本名称不能为空');
  1988. if (DB::table('mp_scripts')->where('script_name', $script_name)->exists()) {
  1989. $script_name .= '_'.date('YmdHis');
  1990. }
  1991. return DB::table('mp_scripts')->insertGetId([
  1992. 'script_name' => $script_name,
  1993. 'user_id' => $uid,
  1994. 'cpid' => $cpid,
  1995. 'created_at' => date('Y-m-d H:i:s'),
  1996. 'updated_at' => date('Y-m-d H:i:s')
  1997. ]);
  1998. }
  1999. public function editScript($data) {
  2000. $script_id = getProp($data, 'script_id');
  2001. $script_name = getProp($data, 'script_name');
  2002. $uid = Site::getUid(); // 获取当前用户ID
  2003. if (!$script_id || !$script_name) Utils::throwError('20003:参数异常');
  2004. $id = DB::table('mp_scripts')->where('script_name', $script_name)->value('id');
  2005. if ($id && (int)$id !== (int)$script_id) {
  2006. $script_name .= '_'.date('YmdHis');
  2007. }
  2008. return DB::table('mp_scripts')->where('id', $script_id)->update([
  2009. 'script_name' => $script_name,
  2010. 'updated_at' => date('Y-m-d H:i:s')
  2011. ]);
  2012. }
  2013. public function delScript($data) {
  2014. $script_id = getProp($data, 'script_id');
  2015. $uid = Site::getUid(); // 获取当前用户ID
  2016. if (!$script_id) Utils::throwError('20003:请选择剧本');
  2017. return DB::table('mp_scripts')->where('id', $script_id)->update([
  2018. 'is_deleted' => 1,
  2019. 'updated_at' => date('Y-m-d H:i:s')
  2020. ]);
  2021. }
  2022. public function createEpisode($data) {
  2023. $script_id = getProp($data, 'script_id');
  2024. $group_name = getProp($data, 'group_name');
  2025. $remark = getProp($data, 'remark');
  2026. if (!DB::table('mp_scripts')->where('id', $script_id)->value('id')) Utils::throwError('20003:剧本不存在!');
  2027. $uid = Site::getUid(); // 获取当前用户ID
  2028. if (!$group_name) Utils::throwError('20003:分集名称不能为空');
  2029. if (DB::table('mp_script_episode_group')->where('group_name', $group_name)->exists()) {
  2030. $group_name .= '_'.date('YmdHis');
  2031. }
  2032. return DB::table('mp_script_episode_group')->insertGetId([
  2033. 'script_id' => $script_id,
  2034. 'group_name' => $group_name,
  2035. 'user_id' => $uid,
  2036. 'remark' => $remark,
  2037. 'created_at' => date('Y-m-d H:i:s'),
  2038. 'updated_at' => date('Y-m-d H:i:s')
  2039. ]);
  2040. }
  2041. public function editEpisode($data) {
  2042. $group_id = getProp($data, 'group_id');
  2043. $start_episode_number = getProp($data, 'start_episode_number');
  2044. $end_episode_number = getProp($data, 'end_episode_number');
  2045. $group_name = getProp($data, 'group_name');
  2046. $uid = Site::getUid(); // 获取当前用户ID
  2047. if (!$group_id || !$group_name || !$start_episode_number || !$end_episode_number) Utils::throwError('20003:参数异常');
  2048. $id = DB::table('mp_script_episode_group')->where('group_name', $group_name)->value('id');
  2049. if ($id && (int)$id !== (int)$group_id) {
  2050. $group_name .= '_'.date('YmdHis');
  2051. }
  2052. return DB::table('mp_script_episode_group')->where('id', $group_id)->update([
  2053. 'group_name' => $group_name,
  2054. 'start_episode_number' => $start_episode_number,
  2055. 'end_episode_number' => $end_episode_number,
  2056. 'updated_at' => date('Y-m-d H:i:s')
  2057. ]);
  2058. }
  2059. public function delEpisode($data) {
  2060. $group_id = getProp($data, 'group_id');
  2061. $uid = Site::getUid(); // 获取当前用户ID
  2062. if (!$group_id) Utils::throwError('20003:请选择分集');
  2063. return DB::table('mp_script_episode_group')->where('id', $group_id)->update([
  2064. 'is_deleted' => 1,
  2065. 'updated_at' => date('Y-m-d H:i:s')
  2066. ]);
  2067. }
  2068. /**
  2069. * 上传文件识别文字内容并与 DeepSeek 进行对话(流式输出版本)
  2070. *
  2071. * @param array $data 包含以下参数:
  2072. * - file: 上传的文件(UploadedFile 对象)或文件路径
  2073. * - question: 用户问题(可选)
  2074. * - model: 使用的模型(r1 或 v3,默认 v3)
  2075. * @return \Generator 返回生成器,用于流式输出
  2076. */
  2077. public function generateEpisodes($data) {
  2078. $script_id = getProp($data, 'script_id');
  2079. $group_id = getProp($data, 'group_id');
  2080. // $file = getProp($data, 'file');
  2081. $file = '';
  2082. $content = getProp($data, 'content', '');
  2083. // $bid = getProp($data, 'bid', 0);
  2084. $bid = 0;
  2085. $group = DB::table('mp_script_episode_group')->where('id', $group_id)->where('script_id', $script_id)->where('is_deleted', 0)->first();
  2086. if (!$group) {
  2087. Utils::throwError('20003:剧本分集不存在');
  2088. }
  2089. $start_episode_sequence = getProp($group, 'start_episode_number', 1);
  2090. $end_episode_sequence = getProp($group, 'end_episode_number', 30);
  2091. $total_episode_num = $end_episode_sequence - $start_episode_sequence + 1;
  2092. $prompt = getProp($data, 'prompt');
  2093. $question = getProp($data, 'question', "请根据实例完成一个{$total_episode_num}集的剧本,要求: 1.从第{$start_episode_sequence}集开始; 2.每集内容独立成章; 3.避免重复内容; 4.确保内容连贯且逻辑清晰; 5.所有章节合起来是给出的文档内容全文大概剧情。6.强制要求一定要给出我需要的分镜集数,不能少7.分集序号用01、02、03显示,不得使用01-1等子集序号,并且需要匹配开始集数的序号并往后顺序显示。\n");
  2094. $model = getProp($data, 'model');
  2095. if (!DB::table('mp_text_models')->where('model', $model)->where('is_enabled', 1)->value('id')) {
  2096. Utils::throwError('20003:该模型不存在!');
  2097. }
  2098. // 检查是否存在重叠的剧集序号范围
  2099. $exists_groups = DB::table('mp_script_episode_group')
  2100. ->where('script_id', $script_id)
  2101. ->where('id', '<>', $group_id) // 排除当前组
  2102. ->where('is_deleted', 0)
  2103. ->where(function($query) use ($start_episode_sequence, $end_episode_sequence) {
  2104. // 检查新范围是否与现有范围重叠
  2105. $query->where(function($q) use ($start_episode_sequence, $end_episode_sequence) {
  2106. // 新范围的开始在现有范围内
  2107. $q->where('start_episode_number', '<=', $start_episode_sequence)
  2108. ->where('end_episode_number', '>=', $start_episode_sequence);
  2109. })->orWhere(function($q) use ($start_episode_sequence, $end_episode_sequence) {
  2110. // 新范围的结束在现有范围内
  2111. $q->where('start_episode_number', '<=', $end_episode_sequence)
  2112. ->where('end_episode_number', '>=', $end_episode_sequence);
  2113. })->orWhere(function($q) use ($start_episode_sequence, $end_episode_sequence) {
  2114. // 新范围完全包含现有范围
  2115. $q->where('start_episode_number', '>=', $start_episode_sequence)
  2116. ->where('end_episode_number', '<=', $end_episode_sequence);
  2117. });
  2118. })
  2119. ->select('start_episode_number', 'end_episode_number')
  2120. ->get();
  2121. if ($exists_groups->isNotEmpty()) {
  2122. $conflict_ranges = [];
  2123. foreach ($exists_groups as $group) {
  2124. $conflict_ranges[] = "第{$group->start_episode_number}-{$group->end_episode_number}集";
  2125. }
  2126. Utils::throwError('20003:剧集序号范围与已存在的剧集组重叠: '.implode('、', $conflict_ranges).',不得创建!');
  2127. }
  2128. // 模型兼容性处理:deepseek-chat 和 deepseek-reasoner 映射到 deepseek-v4-flash
  2129. $thinkingMode = getProp($data, 'thinking', 'disabled'); // 默认非思考模式
  2130. $reasoningEffort = getProp($data, 'reasoning_effort', 'high'); // 默认思考强度
  2131. if ($model === 'deepseek-chat') {
  2132. // deepseek-chat 映射到 deepseek-v4-flash,非思考模式
  2133. $model = 'deepseek-v4-flash';
  2134. $thinkingMode = 'disabled';
  2135. } elseif ($model === 'deepseek-reasoner') {
  2136. // deepseek-reasoner 映射到 deepseek-v4-flash,思考模式
  2137. $model = 'deepseek-v4-flash';
  2138. $thinkingMode = 'enabled';
  2139. }
  2140. $script = DB::table('mp_scripts')->where('id', $script_id)->first();
  2141. if (!$script) {
  2142. Utils::throwError('20003:剧本不存在');
  2143. }
  2144. $content = getProp($group, 'content');
  2145. if (!$file && !$content && !$bid) {
  2146. Utils::throwError('20003:请上传文件或选择书籍或输入内容');
  2147. }
  2148. // 提取文件内容
  2149. if ($file) {
  2150. $content = $this->extractFileContent($file);
  2151. if (!$content) {
  2152. Utils::throwError('20003:无法提取文件内容,请检查文件格式');
  2153. }
  2154. } elseif ($bid) {
  2155. $content = $this->getContentByBid($bid);
  2156. if (!$content) {
  2157. Utils::throwError('20003:无法获取书籍内容,请检查书籍');
  2158. }
  2159. } elseif ($content) {
  2160. $content = filterContent($content);
  2161. } elseif ($prompt) {
  2162. $content = filterContent($prompt);
  2163. } else {
  2164. Utils::throwError('20003:请上传文件或选择书籍或输入内容');
  2165. }
  2166. // 构建消息
  2167. $messages = [
  2168. $this->sys_message,
  2169. [
  2170. 'role' => 'user',
  2171. 'content' => "以下是文档内容:\n\n{$content}\n\n用户问题:{$question}"
  2172. ]
  2173. ];
  2174. $post_data = [
  2175. 'model' => $model,
  2176. 'messages' => $messages,
  2177. // 'max_tokens' => 8192,
  2178. 'temperature' => 0.7,
  2179. 'frequency_penalty' => 0,
  2180. 'presence_penalty' => 0,
  2181. 'thinking' => ['type' => $thinkingMode],
  2182. 'response_format' => ['type' => 'text'],
  2183. 'stream' => true
  2184. ];
  2185. if ($thinkingMode == 'enabled') $post_data['reasoning_effort'] = $reasoningEffort;
  2186. // 根据模型类型选择调用方法
  2187. $fullContent = '';
  2188. $fullReasoningContent = '';
  2189. $usage = [];
  2190. if (in_array($model, ['deepseek-reasoner', 'deepseek-chat', 'deepseek-v4-flash', 'deepseek-v4-pro'])) {
  2191. // DeepSeek 官方模型使用 DeepSeek API
  2192. $streamGenerator = $this->deepSeekStreamResponse($post_data);
  2193. } else if (in_array($model, $this->gpt_text_models)) {
  2194. // GPT-5.4 模型使用 TokenRouter API
  2195. $streamGenerator = $this->gpt54StreamResponse($post_data);
  2196. } else {
  2197. // 其他模型使用火山引擎API
  2198. $streamGenerator = $this->volcEngineChatCompletion($post_data);
  2199. }
  2200. // 处理流式输出
  2201. foreach ($streamGenerator as $chunk) {
  2202. if (isset($chunk['type'])) {
  2203. if ($chunk['type'] === 'done') {
  2204. // 最终结果
  2205. $fullContent = $chunk['full_content'];
  2206. $fullReasoningContent = $chunk['full_reasoning'];
  2207. $usage = isset($chunk['usage']) ? $chunk['usage'] : [];
  2208. } else {
  2209. // 逐块yield数据
  2210. yield $chunk;
  2211. }
  2212. }
  2213. }
  2214. dLog('deepseek')->info('完整内容: '.$fullContent);
  2215. logDB('deepseek', 'info', '完整内容', ['fullContent'=>$fullContent]);
  2216. // 处理完整内容并返回最终结果
  2217. $script_arr = [];
  2218. if ($fullContent) {
  2219. $script_arr = extractScriptContent($fullContent);
  2220. }
  2221. logDB('deepseek', 'info', '解析内容', $script_arr);
  2222. if (!$script_arr['episodes']) {
  2223. Utils::throwError('20003:未生成剧本相关信息');
  2224. // yield [
  2225. // 'type' => 'done',
  2226. // 'script' => $script_arr,
  2227. // 'msg' => '未生成剧本相关信息',
  2228. // 'answer' => $fullContent,
  2229. // 'reasoning' => $fullReasoningContent,
  2230. // 'usage' => $usage
  2231. // ];
  2232. // return ;
  2233. }
  2234. try {
  2235. DB::beginTransaction();
  2236. // // 返回前保存剧本内容
  2237. // $boolen = DB::table('mp_scripts')->where('id', $script_id)->update([
  2238. // 'content' => $content,
  2239. // 'updated_at' => date('Y-m-d H:i:s')
  2240. // ]);
  2241. // if (!$boolen) {
  2242. // Utils::throwError('20003:保存剧本内容失败');
  2243. // }
  2244. // $boolen1 = DB::table('mp_script_episode_group')->where('id', $group_id)->update([
  2245. // 'start_episode_number' => $start_episode_sequence,
  2246. // 'end_episode_number' => $end_episode_sequence,
  2247. // 'updated_at' => date('Y-m-d H:i:s')
  2248. // ]);
  2249. // if (!$boolen1) {
  2250. // Utils::throwError('20003:保存分集失败');
  2251. // }
  2252. $episode_content = '';
  2253. $episodes = [];
  2254. foreach ($script_arr['episodes'] as $item) {
  2255. $episode_content .= "\u{200D}\u{200D}\u{200D}" . $item['episode_content'] . "\r\n\r\n";
  2256. // $episodes[] = [
  2257. // 'script_id' => $script_id,
  2258. // 'episode_number' => $item['episode_number'],
  2259. // 'episode_name' => $item['episode_name'],
  2260. // 'episode_content' => $item['episode_content'],
  2261. // 'created_at' => date('Y-m-d H:i:s'),
  2262. // 'updated_at' => date('Y-m-d H:i:s'),
  2263. // ];
  2264. }
  2265. if ($episode_content) {
  2266. $boolen2 = DB::table('mp_script_episode_group')->where('id', $group_id)->update([
  2267. 'episode_content' => $episode_content,
  2268. 'updated_at' => date('Y-m-d H:i:s')
  2269. ]);
  2270. if (!$boolen2) {
  2271. Utils::throwError('20003:保存分集内容失败');
  2272. }
  2273. }else {
  2274. Utils::throwError('20003:分集剧本解析失败');
  2275. }
  2276. }catch (\Exception $e) {
  2277. DB::rollBack();
  2278. Utils::throwError('20003:'.$e->getMessage());
  2279. }
  2280. DB::commit();
  2281. yield [
  2282. 'type' => 'done',
  2283. 'script' => $script_arr,
  2284. 'episode_content' => $episode_content,
  2285. 'answer' => $fullContent,
  2286. 'reasoning' => $fullReasoningContent,
  2287. 'usage' => $usage
  2288. ];
  2289. }
  2290. public function chatWithFileStream($data) {
  2291. $script_id = getProp($data, 'script_id');
  2292. $group_id = getProp($data, 'group_id');
  2293. $file = getProp($data, 'file');
  2294. $content = getProp($data, 'content', '');
  2295. $bid = getProp($data, 'bid', 0);
  2296. $start_episode_sequence = getProp($data, 'start_episode_sequence', 1);
  2297. $total_episode_num = getProp($data, 'total_episode_num', 30);
  2298. $end_episode_sequence = $start_episode_sequence + $total_episode_num - 1;
  2299. $prompt = getProp($data, 'prompt');
  2300. $baseQuestion = "请根据实例完成一个{$total_episode_num}集的剧本,要求: 1.从第{$start_episode_sequence}集开始; 2.每集内容独立成章; 3.避免重复内容; 4.确保内容连贯且逻辑清晰; 5.所有章节合起来是给出的文档内容全文大概剧情。6.强制要求一定要给出我需要的分镜集数,不能少7.分集序号用01、02、03显示,不得使用01-1等子集序号,并且需要匹配开始集数的序号并往后顺序显示。";
  2301. if (!empty($prompt)) {
  2302. $baseQuestion .= "\n本次修改要求如下:\n{$prompt}";
  2303. }
  2304. $question = getProp($data, 'question', $baseQuestion);
  2305. $model = getProp($data, 'model');
  2306. if (!DB::table('mp_text_models')->where('model', $model)->where('is_enabled', 1)->value('id')) {
  2307. Utils::throwError('20003:该模型不存在!');
  2308. }
  2309. // 检查是否存在重叠的剧集序号范围
  2310. $exists_groups = DB::table('mp_script_episode_group')
  2311. ->where('script_id', $script_id)
  2312. ->where('id', '<>', $group_id)
  2313. ->where('is_deleted', 0)
  2314. ->where(function($query) use ($start_episode_sequence, $end_episode_sequence) {
  2315. // 检查新范围是否与现有范围重叠
  2316. $query->where(function($q) use ($start_episode_sequence, $end_episode_sequence) {
  2317. // 新范围的开始在现有范围内
  2318. $q->where('start_episode_number', '<=', $start_episode_sequence)
  2319. ->where('end_episode_number', '>=', $start_episode_sequence);
  2320. })->orWhere(function($q) use ($start_episode_sequence, $end_episode_sequence) {
  2321. // 新范围的结束在现有范围内
  2322. $q->where('start_episode_number', '<=', $end_episode_sequence)
  2323. ->where('end_episode_number', '>=', $end_episode_sequence);
  2324. })->orWhere(function($q) use ($start_episode_sequence, $end_episode_sequence) {
  2325. // 新范围完全包含现有范围
  2326. $q->where('start_episode_number', '>=', $start_episode_sequence)
  2327. ->where('end_episode_number', '<=', $end_episode_sequence);
  2328. });
  2329. })
  2330. ->select('start_episode_number', 'end_episode_number')
  2331. ->get();
  2332. if ($exists_groups->isNotEmpty()) {
  2333. $conflict_ranges = [];
  2334. foreach ($exists_groups as $group) {
  2335. $conflict_ranges[] = "第{$group->start_episode_number}-{$group->end_episode_number}集";
  2336. }
  2337. Utils::throwError('20003:剧集序号范围与已存在的剧集组重叠: '.implode('、', $conflict_ranges).',不得创建!');
  2338. }
  2339. // 模型兼容性处理:deepseek-chat 和 deepseek-reasoner 映射到 deepseek-v4-flash
  2340. $thinkingMode = getProp($data, 'thinking', 'disabled'); // 默认非思考模式
  2341. $reasoningEffort = getProp($data, 'reasoning_effort', 'high'); // 默认思考强度
  2342. if ($model === 'deepseek-chat') {
  2343. // deepseek-chat 映射到 deepseek-v4-flash,非思考模式
  2344. $model = 'deepseek-v4-flash';
  2345. $thinkingMode = 'disabled';
  2346. } elseif ($model === 'deepseek-reasoner') {
  2347. // deepseek-reasoner 映射到 deepseek-v4-flash,思考模式
  2348. $model = 'deepseek-v4-flash';
  2349. $thinkingMode = 'enabled';
  2350. }
  2351. $script = DB::table('mp_scripts')->where('id', $script_id)->first();
  2352. if (!$script) {
  2353. Utils::throwError('20003:剧本不存在');
  2354. }
  2355. $group = DB::table('mp_script_episode_group')->where('id', $group_id)->where('script_id', $script_id)->where('is_deleted', 0)->first();
  2356. if (!$group) {
  2357. Utils::throwError('20003:剧本分集不存在');
  2358. }
  2359. if (!$file && !$content && !$bid) {
  2360. Utils::throwError('20003:请上传文件或选择书籍或输入内容');
  2361. }
  2362. // 提取文件内容
  2363. if ($file) {
  2364. $content = $this->extractFileContent($file);
  2365. if (!$content) {
  2366. Utils::throwError('20003:无法提取文件内容,请检查文件格式');
  2367. }
  2368. } elseif ($bid) {
  2369. $content = $this->getContentByBid($bid);
  2370. if (!$content) {
  2371. Utils::throwError('20003:无法获取书籍内容,请检查书籍');
  2372. }
  2373. } elseif ($content) {
  2374. $content = filterContent($content);
  2375. } elseif ($prompt) {
  2376. $content = filterContent($prompt);
  2377. } else {
  2378. Utils::throwError('20003:请上传文件或选择书籍或输入内容');
  2379. }
  2380. // 构建消息
  2381. $messages = [
  2382. [
  2383. 'role' => 'system',
  2384. 'content' => "你是一个专业的文档分析助手及资深编剧,请根据用户提供的文档内容完成示例格式的内容输出(需通过以下几个板块进行回复: <故事梗概><剧本亮点><人物关系><核心矛盾><主体列表><美术风格><场景列表>,每个板块之间必须使用###分隔,#不能多也不能少;同时板块之间需满足以下要求:\n
  2385. 强制要求:\n
  2386. 1.你只负责与剧本相关的内容,如果用户输入与剧本无关的内容,请告知用户并拒绝继续回答,此为第一优先原则,不满足此原则就忽略其他要求并停止继续输出
  2387. 2.用户如果直接给了你一本小说或者要求你帮忙写一个故事或者剧本,此类要求不属于与剧本无关,请帮助用户完成请求
  2388. 3.请严格按照示例格式返回内容,不要更改格式或随意增加符号\n
  2389. 普通要求:\n
  2390. 1.<主体列表>中出现的主体必须尽可能统一,只允许同一个角色年龄段相差较大的情况下可重复,比如梁萧-少年、梁萧-青年、梁萧-中年、梁萧-老年这种情况,不得出现梁萧-愤怒、梁萧-隐忍、梁萧-求生等情绪和状态变化的同一角色;并且角色的样貌、形体及穿着描述尽可能详细\n\n
  2391. 2.不要出现分集内容,只保留剧本大纲的格式,格式要求和示例如下,请强制按该示例输出
  2392. 3.分集序号可能不是从 1 开始,也可能是跳跃的(例如直接从第 3 集、第 5 集开始)。你只需要基于下面给出的“当前集内容要点”来创作这一集即可。
  2393. 4.不需要强行补齐前面未提供的集数,也不需要预测后面未提供的集数,但请保持本集内部故事的完整性和节奏。
  2394. 5.总集数将作为本季整体篇幅的参考,你可以根据它调整本集的情节推进速度(例如:如果总集数很少,本集应更紧凑;如果总集数很多,本集可以适当展开细节)
  2395. 示例如下:\n
  2396. ###剧本名:西昆仑
  2397. ###故事梗概
  2398. 内容梗概:古风厚涂的世界中,少女阿雪身中奇毒“问心刺”,气息奄奄。少年梁萧心急如焚,用奇珍“阴阳球”稳住阿雪性命,却发现必须解衣拔刺。阿雪羞涩难当,梁萧情急之下提出结拜兄妹,以化解尴尬。两人匆匆结拜后,梁萧小心翼翼地拔出“问心刺”,竟发现它是一根诡异的女子秀发。阿雪转危为安,两人之间,在生死与羞涩交织的瞬间,情愫暗生,却又被兄妹之名束缚,留下无尽遐想。
  2399. ###剧本亮点
  2400. 亮点1:绝境奇药,生死一线
  2401. 画面开场即是阿雪苍白无力的脸庞,梁萧眼中泪光闪烁,急促的呼吸声与心跳声烘托紧张气氛。特写梁萧将“阴阳球”塞入阿雪舌底,球体散发微光,阿雪气息从若有若无到逐渐平稳,生死逆转的瞬间,视觉与听觉冲击力十足。
  2402. 亮点2:结拜兄妹,化解尴尬
  2403. 阿雪因解衣拔刺而羞愤欲绝的特写,梁萧眼神中的挣扎与无奈。在紧迫的背景音下,梁萧急中生智,牵起阿雪颤抖的手,跪地匆匆结拜,阿雪眼神从惊惶到迷茫,最终一句“我……我愿意的”带着哭腔,将救命与羞耻的矛盾推向高潮,又以出人意料的方式解决。
  2404. 亮点3:纤发夺命,情愫暗涌
  2405. 拔刺过程的紧张特写,梁萧指尖小心翼翼地捏住细丝,缓慢抽出。当“问心刺”被完全拔出,特写它竟是一根细长、泛着寒光的女子秀发,诡异而令人震惊。阿雪苏醒后,两人眼神交汇,阿雪低头羞涩,梁萧眼中复杂的情绪,无声地传递出超越兄妹的微妙情愫。
  2406. ###人物关系
  2407. 阿雪与梁萧之间存在兄妹之情。
  2408. ###核心矛盾
  2409. 阿雪与梁萧之间存在兄妹之情,但因情愫暗生而产生微妙的矛盾。阿雪羞涩难当,梁萧情急之下提出结拜兄妹,以化解尴尬。两人匆匆结拜后,梁萧小心翼翼地拔出“问心刺”,竟发现它是一根诡异的女子秀发。阿雪转危为安,两人之间,在生死与羞涩交织的瞬间,情愫暗生,却又被兄妹之名束缚,留下无尽遐想。
  2410. ###主体列表
  2411. 阿雪:身中奇毒“问心刺”,气息奄奄,面色苍白,身体虚弱。
  2412. 梁萧:少年侠士,心系阿雪安危,沉着冷静,内心挣扎于情义之间。
  2413. 阴阳球:天地异宝,能化生精气,救人于危难。
  2414. 问心刺:一根诡异的女子秀发,细长泛寒光,是致命的奇毒。
  2415. ###美术风格
  2416. 基础画风风格词:厚涂古风
  2417. 整体采用精致细腻的厚涂古风,色彩浓郁而富有层次感,注重光影对比,营造出古典而富有戏剧张力的氛围。色调以深沉的蓝绿、灰调为主,衬托阿雪病弱的苍白,同时用温暖的金色、琥珀色光晕突出“阴阳球”的生命力。拔刺与结拜场景则以柔和的暖色调为主,辅以人物脸颊的红晕,烘托羞涩与情愫暗生的微妙感。人物造型飘逸,服饰细节考究,背景虚实结合,既有古韵,又不失奇幻色彩。
  2418. ###场景列表
  2419. 林间疗伤地:幽静的古风林间空地,一棵高大古树下,是梁萧为阿雪疗伤的地点。环境清幽,光影斑驳,既有古韵又不失奇幻色彩。"],
  2420. [
  2421. 'role' => 'user',
  2422. 'content' => "以下是文档内容:\n\n{$content}\n\n用户问题:{$question}"
  2423. ]
  2424. ];
  2425. $post_data = [
  2426. 'model' => $model,
  2427. 'messages' => $messages,
  2428. // 'max_tokens' => 8192,
  2429. 'temperature' => 0.7,
  2430. 'frequency_penalty' => 0,
  2431. 'presence_penalty' => 0,
  2432. 'thinking' => ['type' => $thinkingMode],
  2433. 'response_format' => ['type' => 'text'],
  2434. 'stream' => true
  2435. ];
  2436. if ($thinkingMode == 'enabled') $post_data['reasoning_effort'] = $reasoningEffort;
  2437. // 根据模型类型选择调用方法
  2438. $fullContent = '';
  2439. $fullReasoningContent = '';
  2440. $usage = [];
  2441. if (in_array($model, ['deepseek-reasoner', 'deepseek-chat', 'deepseek-v4-flash', 'deepseek-v4-pro'])) {
  2442. // DeepSeek 官方模型使用 DeepSeek API
  2443. $streamGenerator = $this->deepSeekStreamResponse($post_data);
  2444. } else if (in_array($model, $this->gpt_text_models)) {
  2445. // GPT-5.4 模型使用 TokenRouter API
  2446. $streamGenerator = $this->gpt54StreamResponse($post_data);
  2447. } else {
  2448. // 其他模型使用火山引擎API
  2449. $streamGenerator = $this->volcEngineChatCompletion($post_data);
  2450. }
  2451. // 处理流式输出
  2452. foreach ($streamGenerator as $chunk) {
  2453. if (isset($chunk['type'])) {
  2454. if ($chunk['type'] === 'done') {
  2455. // 最终结果
  2456. $fullContent = $chunk['full_content'];
  2457. $fullReasoningContent = $chunk['full_reasoning'];
  2458. $usage = isset($chunk['usage']) ? $chunk['usage'] : [];
  2459. } else {
  2460. // 逐块yield数据
  2461. yield $chunk;
  2462. }
  2463. }
  2464. }
  2465. dLog('deepseek')->info('完整内容: '.$fullContent);
  2466. logDB('deepseek', 'info', '完整内容', ['fullContent'=>$fullContent]);
  2467. // 处理完整内容并返回最终结果
  2468. $script_arr = [];
  2469. if ($fullContent) {
  2470. $script_arr = extractScriptContent($fullContent);
  2471. }
  2472. logDB('deepseek', 'info', '解析内容', $script_arr);
  2473. if (!$script_arr['roles']) {
  2474. Utils::throwError('20003:未生成剧本相关信息');
  2475. // yield [
  2476. // 'type' => 'done',
  2477. // 'script' => $script_arr,
  2478. // 'msg' => '未生成剧本相关信息',
  2479. // 'answer' => $fullContent,
  2480. // 'reasoning' => $fullReasoningContent,
  2481. // 'usage' => $usage
  2482. // ];
  2483. }
  2484. try {
  2485. DB::beginTransaction();
  2486. // // 返回前保存剧本内容
  2487. // $boolen = DB::table('mp_scripts')->where('id', $script_id)->update([
  2488. // 'content' => $content,
  2489. // 'status' => '解析完成',
  2490. // 'updated_at' => date('Y-m-d H:i:s')
  2491. // ]);
  2492. // if (!$boolen) {
  2493. // Utils::throwError('20003:保存剧本内容失败');
  2494. // }
  2495. $boolen1 = DB::table('mp_script_episode_group')->where('id', $group_id)->update([
  2496. 'start_episode_number' => $start_episode_sequence,
  2497. 'end_episode_number' => $end_episode_sequence,
  2498. 'intro' => isset($script_arr['intro']) ? $script_arr['intro'] : '',
  2499. 'highlights' => isset($script_arr['highlights']) ? $script_arr['highlights'] : '',
  2500. 'role_relationship' => isset($script_arr['role_relationship']) ? $script_arr['role_relationship'] : '',
  2501. 'core_contradiction' => isset($script_arr['core_contradiction']) ? $script_arr['core_contradiction'] : '',
  2502. 'roles' => isset($script_arr['roles']) && is_array($script_arr['roles']) ? json_encode($script_arr['roles'], 256) : '[]',
  2503. 'art_style' => isset($script_arr['art_style']) ? $script_arr['art_style'] : '',
  2504. 'scenes' => isset($script_arr['scenes']) && is_array($script_arr['scenes']) ? json_encode($script_arr['scenes'], 256) : '[]',
  2505. 'status' => '解析完成',
  2506. 'content' => $content,
  2507. 'updated_at' => date('Y-m-d H:i:s')
  2508. ]);
  2509. if (!$boolen1) {
  2510. Utils::throwError('20003:保存分集失败');
  2511. }
  2512. // $episodes = [];
  2513. // foreach ($script_arr['episodes'] as $item) {
  2514. // $episodes[] = [
  2515. // 'script_id' => $script_id,
  2516. // 'episode_number' => $item['episode_number'],
  2517. // 'episode_name' => $item['episode_name'],
  2518. // 'episode_content' => $item['episode_content'],
  2519. // 'created_at' => date('Y-m-d H:i:s'),
  2520. // 'updated_at' => date('Y-m-d H:i:s'),
  2521. // ];
  2522. // }
  2523. // if ($episodes) {
  2524. // $boolen2 = DB::table('mp_script_episodes')->insert($episodes);
  2525. // if (!$boolen2) {
  2526. // Utils::throwError('20003:保存分集内容失败');
  2527. // }
  2528. // }else {
  2529. // Utils::throwError('20003:分集剧本解析失败');
  2530. // }
  2531. }catch (\Exception $e) {
  2532. DB::rollBack();
  2533. Utils::throwError('20003:'.$e->getMessage());
  2534. }
  2535. DB::commit();
  2536. yield [
  2537. 'type' => 'done',
  2538. 'script' => $script_arr,
  2539. 'answer' => $fullContent,
  2540. 'reasoning' => $fullReasoningContent,
  2541. 'usage' => $usage
  2542. ];
  2543. }
  2544. /**
  2545. * 上传文件识别文字内容并与 DeepSeek 进行对话
  2546. *
  2547. * @param array $data 包含以下参数:
  2548. * - file: 上传的文件(UploadedFile 对象)或文件路径
  2549. * - question: 用户问题(可选,默认为"请总结这个文件的内容")
  2550. * - model: 使用的模型(r1 或 v3,默认 v3)
  2551. * @return array
  2552. */
  2553. public function chatWithFile($data) {
  2554. $script_id = getProp($data, 'script_id');
  2555. $file = getProp($data, 'file');
  2556. $content = getProp($data, 'content', '');
  2557. $bid = getProp($data, 'bid', 0);
  2558. $start_episode_sequence = getProp($data, 'start_episode_sequence', 1);
  2559. $total_episode_num = getProp($data, 'total_episode_num', 30);
  2560. $prompt = getProp($data, 'prompt');
  2561. $baseQuestion = "请根据实例完成一个{$total_episode_num}集的剧本,要求: 1.从第{$start_episode_sequence}集开始; 2.每集内容独立成章; 3.避免重复内容; 4.确保内容连贯且逻辑清晰; 5.所有章节合起来是给出的文档内容全文大概剧情。6.要求一定要给出我需要的分镜集数,不能少7.分集序号用01、02、03显示,不得使用01-1等子集序号,并且需要匹配开始集数的序号并往后顺序显示。";
  2562. if (!empty($prompt)) {
  2563. $baseQuestion .= "\n本次修改要求如下:\n{$prompt}";
  2564. }
  2565. $question = getProp($data, 'question', $baseQuestion);
  2566. // 处理文本模型
  2567. $model = getProp($data, 'model');
  2568. if (!$model) {
  2569. // 用户没有输入,使用默认值
  2570. $model = 'doubao-seed-2-0-mini-260215';
  2571. }
  2572. // 验证模型是否在可用模型表中
  2573. if (!DB::table('mp_text_models')->where('model', $model)->where('is_enabled', 1)->exists()) {
  2574. $model = 'doubao-seed-2-0-mini-260215';
  2575. }
  2576. // 模型兼容性处理:deepseek-chat 和 deepseek-reasoner 映射到 deepseek-v4-flash
  2577. $thinkingMode = getProp($data, 'thinking', 'disabled'); // 默认非思考模式
  2578. $reasoningEffort = getProp($data, 'reasoning_effort', 'high'); // 默认思考强度
  2579. if ($model === 'deepseek-chat') {
  2580. // deepseek-chat 映射到 deepseek-v4-flash,非思考模式
  2581. $model = 'deepseek-v4-flash';
  2582. $thinkingMode = 'disabled';
  2583. } elseif ($model === 'deepseek-reasoner') {
  2584. // deepseek-reasoner 映射到 deepseek-v4-flash,思考模式
  2585. $model = 'deepseek-v4-flash';
  2586. $thinkingMode = 'enabled';
  2587. }
  2588. $script = DB::table('mp_scripts')->where('id', $script_id)->first();
  2589. if (!$script) {
  2590. Utils::throwError('20003:剧本不存在');
  2591. }
  2592. if (!$file && !$content && !$bid) {
  2593. Utils::throwError('20003:请上传文件或选择书籍或输入内容');
  2594. }
  2595. // 提取文件内容
  2596. if ($file) {
  2597. $content = $this->extractFileContent($file);
  2598. if (!$content) {
  2599. Utils::throwError('20003:无法提取文件内容,请检查文件格式');
  2600. }
  2601. } elseif ($bid) {
  2602. $content = $this->getContentByBid($bid);
  2603. if (!$content) {
  2604. Utils::throwError('20003:无法获取书籍内容,请检查书籍');
  2605. }
  2606. } elseif ($content) {
  2607. $content = filterContent($content);
  2608. } elseif ($prompt) {
  2609. $content = filterContent($prompt);
  2610. } else {
  2611. Utils::throwError('20003:请上传文件或选择书籍或输入内容');
  2612. }
  2613. // 构建消息
  2614. $messages = [
  2615. $this->sys_message,
  2616. [
  2617. 'role' => 'user',
  2618. 'content' => "以下是文档内容:\n\n{$content}\n\n用户问题:{$question}"
  2619. ]
  2620. ];
  2621. $post_data = [
  2622. 'model' => $model,
  2623. 'messages' => $messages,
  2624. // 'max_tokens' => 8192,
  2625. 'temperature' => 0.7,
  2626. 'frequency_penalty' => 0,
  2627. 'presence_penalty' => 0,
  2628. 'thinking' => ['type' => $thinkingMode],
  2629. 'response_format' => ['type' => 'text'],
  2630. 'stream' => false
  2631. ];
  2632. if ($thinkingMode == 'enabled') $post_data['reasoning_effort'] = $reasoningEffort;
  2633. // 根据模型类型选择调用方法
  2634. $fullContent = '';
  2635. $usage = [];
  2636. if (in_array($model, ['deepseek-reasoner', 'deepseek-chat', 'deepseek-v4-flash', 'deepseek-v4-pro'])) {
  2637. // DeepSeek 官方模型使用 DeepSeek API
  2638. $chatResult = $this->chatOnly($post_data);
  2639. } else if (in_array($model, $this->gpt_text_models)) {
  2640. // GPT-5.4 模型使用 TokenRouter API
  2641. $chatResult = $this->gpt54ChatOnly($post_data);
  2642. } else {
  2643. // 其他模型使用火山引擎API
  2644. $chatResult = $this->volcEngineChatCompletion($post_data);
  2645. }
  2646. if (is_array($chatResult)) {
  2647. $fullContent = $chatResult['fullContent'];
  2648. $usage = $chatResult['usage'];
  2649. }
  2650. $script_arr = [];
  2651. // 处理结果
  2652. if ($fullContent) {
  2653. $script_arr = extractScriptContent($fullContent);
  2654. }
  2655. // 返回前保存剧本内容
  2656. DB::table('mp_scripts')->where('id', $script_id)->update([
  2657. 'content' => $content,
  2658. 'updated_at' => date('Y-m-d H:i:s')
  2659. ]);
  2660. return [
  2661. 'script' => $script_arr,
  2662. 'answer' => $fullContent,
  2663. 'usage' => $usage
  2664. ];
  2665. }
  2666. public function getEpisodeContent($data) {
  2667. $group_id = getProp($data, 'group_id');
  2668. return DB::table('mp_script_episode_group')->where('id', $group_id)->value('episode_content');
  2669. }
  2670. public function saveEpisodeContent($data) {
  2671. $script_id = getProp($data, 'script_id');
  2672. $group_id = getProp($data, 'group_id');
  2673. $start_episode_sequence = getProp($data, 'start_episode_number');
  2674. $end_episode_sequence = getProp($data, 'end_episode_number');
  2675. // 检查是否存在重叠的剧集序号范围
  2676. $exists_groups = DB::table('mp_script_episode_group')
  2677. ->where('script_id', $script_id)
  2678. ->where('id', '<>', $group_id) // 排除当前组
  2679. ->where('is_deleted', 0)
  2680. ->where(function($query) use ($start_episode_sequence, $end_episode_sequence) {
  2681. // 检查新范围是否与现有范围重叠
  2682. $query->where(function($q) use ($start_episode_sequence, $end_episode_sequence) {
  2683. // 新范围的开始在现有范围内
  2684. $q->where('start_episode_number', '<=', $start_episode_sequence)
  2685. ->where('end_episode_number', '>=', $start_episode_sequence);
  2686. })->orWhere(function($q) use ($start_episode_sequence, $end_episode_sequence) {
  2687. // 新范围的结束在现有范围内
  2688. $q->where('start_episode_number', '<=', $end_episode_sequence)
  2689. ->where('end_episode_number', '>=', $end_episode_sequence);
  2690. })->orWhere(function($q) use ($start_episode_sequence, $end_episode_sequence) {
  2691. // 新范围完全包含现有范围
  2692. $q->where('start_episode_number', '>=', $start_episode_sequence)
  2693. ->where('end_episode_number', '<=', $end_episode_sequence);
  2694. });
  2695. })
  2696. ->select('start_episode_number', 'end_episode_number')
  2697. ->get();
  2698. if ($exists_groups->isNotEmpty()) {
  2699. $conflict_ranges = [];
  2700. foreach ($exists_groups as $group) {
  2701. $conflict_ranges[] = "第{$group->start_episode_number}-{$group->end_episode_number}集";
  2702. }
  2703. Utils::throwError('20003:剧集序号范围与已存在的剧集组重叠: '.implode('、', $conflict_ranges).',不得创建!');
  2704. }
  2705. $episode_content = getProp($data, 'episode_content');
  2706. return DB::table('mp_script_episode_group')->where('id', $group_id)->where('script_id', $script_id)->update([
  2707. 'content' => $episode_content,
  2708. 'start_episode_number' => $start_episode_sequence,
  2709. 'end_episode_number' => $end_episode_sequence,
  2710. 'updated_at' => date('Y-m-d H:i:s')
  2711. ]);
  2712. $script_arr =getProp($data, 'script', []);
  2713. if (!$script_arr) Utils::throwError('20003:剧本内容不能为空');
  2714. $start_episode_sequence = getProp($data, 'start_episode_sequence', 1);
  2715. $script_id = getProp($data, 'script_id');
  2716. if (!$script_id) Utils::throwError('20003:剧本ID不能为空');
  2717. try {
  2718. DB::beginTransaction();
  2719. $update_data = [
  2720. 'intro' => isset($script_arr['intro']) ? $script_arr['intro'] : '',
  2721. 'highlights' => isset($script_arr['highlights']) ? $script_arr['highlights'] : '',
  2722. 'role_relationship' => isset($script_arr['role_relationship']) ? $script_arr['role_relationship'] : '',
  2723. 'core_contradiction' => isset($script_arr['core_contradiction']) ? $script_arr['core_contradiction'] : '',
  2724. 'roles' => isset($script_arr['roles']) && is_array($script_arr['roles']) ? json_encode($script_arr['roles'], 256) : '[]',
  2725. 'art_style' => isset($script_arr['art_style']) ? $script_arr['art_style'] : '',
  2726. 'scenes' => isset($script_arr['scenes']) && is_array($script_arr['scenes']) ? json_encode($script_arr['scenes'], 256) : '[]',
  2727. 'status' => 3,
  2728. 'start_episode_sequence' => $start_episode_sequence,
  2729. 'end_episode_sequence' => $start_episode_sequence + count($script_arr['episodes']) - 1,
  2730. 'episode_num' => count($script_arr['episodes']),
  2731. 'updated_at' => date('Y-m-d H:i:s')
  2732. ];
  2733. // 保存剧本内容
  2734. $boolen = DB::table('mp_scripts')->where('id', $script_id)->update($update_data);
  2735. if (!$boolen) {
  2736. dLog('deepseek')->info('剧本内容保存失败', $update_data);
  2737. logDB('deepseek', 'error', '剧本内容保存失败', ['script_id' => $script_id]);
  2738. Utils::throwError('20003:剧本内容保存失败');
  2739. }
  2740. // 保存分集内容
  2741. // 删除历史分集内容
  2742. $boolen1 = DB::table('mp_script_episodes')->where('script_id', $script_id)->delete();
  2743. $episodes = [];
  2744. $sequence = 1;
  2745. foreach ($script_arr['episodes'] as $episode) {
  2746. $segment_number = 1;
  2747. foreach($episode['segments'] as $segment) {
  2748. $episodes[] = [
  2749. 'script_id' => $script_id,
  2750. 'episode_number' => $episode['episode_number'],
  2751. 'title' => $episode['title'],
  2752. // 'content' => $episode['content'],
  2753. 'content' => '',
  2754. 'segment_number' => $segment_number,
  2755. 'segment_content' => $segment['segment_content'],
  2756. 'sequence' => $sequence,
  2757. 'created_at' => date('Y-m-d H:i:s'),
  2758. 'updated_at' => date('Y-m-d H:i:s')
  2759. ];
  2760. $sequence++;
  2761. $segment_number++;
  2762. }
  2763. }
  2764. $boolen2 = DB::table('mp_script_episodes')->insert($episodes);
  2765. if (!$boolen2) {
  2766. dLog('deepseek')->info('分集内容保存失败', $episodes);
  2767. logDB('deepseek', 'error', '分集内容保存失败', ['episodes_count' => count($episodes)]);
  2768. Utils::throwError('20003:分集内容保存失败');
  2769. }
  2770. } catch (\Exception $e) {
  2771. DB::rollBack();
  2772. Utils::throwError('20003:'.$e->getMessage());
  2773. }
  2774. DB::commit();
  2775. return true;
  2776. }
  2777. public function getBookContent($data) {
  2778. $bid = getProp($data, 'bid');
  2779. $start_sequence = getProp($data, 'start_sequence');
  2780. $end_sequence = getProp($data, 'end_sequence');
  2781. $chapter_contents = DB::table('chapters as c')->leftJoin('chapter_contents as cc', 'c.chapter_content_id', 'cc.id')->where('c.bid', $bid)->where('c.is_check', 1)->where('c.is_deleted', 0)->whereBetween('c.sequence', [$start_sequence, $end_sequence])
  2782. ->orderBy('c.sequence')->pluck('cc.content')->toArray();
  2783. $return_content = filterContent(implode(PHP_EOL, $chapter_contents));
  2784. return $return_content;
  2785. }
  2786. public function exportScript($data) {
  2787. $filename = getProp($data, 'filename');
  2788. $script_id = getProp($data, 'script_id');
  2789. $group_id = getProp($data, 'group_id');
  2790. $script = DB::table('mp_scripts')->where('id', $script_id)->where('is_deleted', 0)
  2791. ->selectRaw('id as script_id, script_name, episode_num, remark, created_at')->first();
  2792. if (!$script) Utils::throwError('20003:该剧本不存在!');
  2793. $script = (array)$script;
  2794. // 获取分集组信息
  2795. if ($group_id) {
  2796. $groups = DB::table('mp_script_episode_group')->where('script_id', $script_id)->where('id', $group_id)->where('is_deleted', 0)->whereNotNull('episode_content')
  2797. ->selectRaw('intro, highlights, roles, role_relationship, core_contradiction, art_style, scenes, status, start_episode_number, end_episode_number, episode_content')
  2798. ->get()->map(function($value) {
  2799. return (array)$value;
  2800. })->toArray();
  2801. }else {
  2802. $groups = DB::table('mp_script_episode_group')->where('script_id', $script_id)->where('is_deleted', 0)->whereNotNull('episode_content')
  2803. ->selectRaw('intro, highlights, roles, role_relationship, core_contradiction, art_style, scenes, status, start_episode_number, end_episode_number, episode_content')
  2804. ->orderBy('start_episode_number')->orderBy('id')->get()->map(function($value) {
  2805. return (array)$value;
  2806. })->toArray();
  2807. }
  2808. if (!$groups) Utils::throwError('20003:分集不存在');
  2809. $script['group'] = $groups;
  2810. $export_type = getProp($data, 'export_type', 'txt');
  2811. // 生成文件名
  2812. $filename = $filename ? $filename : $script['script_name'] . '_' . date('YmdHis');
  2813. // 根据导出类型生成不同格式的文件
  2814. switch ($export_type) {
  2815. case 'txt':
  2816. return $this->exportScriptAsTxt($script, $filename);
  2817. case 'pdf':
  2818. return $this->exportScriptAsPdf($script, $filename);
  2819. default:
  2820. Utils::throwError('20003:不支持的导出格式,支持:txt、pdf');
  2821. }
  2822. }
  2823. /**
  2824. * 导出剧本为TXT格式
  2825. */
  2826. private function exportScriptAsTxt($script, $filename) {
  2827. // 构建TXT内容
  2828. $content = $this->buildScriptContent($script);
  2829. // 设置响应头,直接下载
  2830. header('Content-Type: text/plain; charset=utf-8');
  2831. header('Content-Disposition: attachment; filename="' . $filename . '.txt"');
  2832. header('Content-Length: ' . strlen($content));
  2833. // 输出内容并结束
  2834. echo $content;
  2835. exit();
  2836. }
  2837. /**
  2838. * 导出剧本为PDF格式
  2839. */
  2840. private function exportScriptAsPdf($script, $filename) {
  2841. // 使用HTML转PDF的方式来更好地支持中文
  2842. $htmlContent = $this->buildScriptHtmlForPdf($script);
  2843. // 创建PDF实例
  2844. $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  2845. // 设置文档信息
  2846. $pdf->SetCreator('剧本导出系统');
  2847. $pdf->SetAuthor('系统');
  2848. $pdf->SetTitle($script['script_name']);
  2849. $pdf->SetSubject('剧本导出');
  2850. // 设置边距
  2851. $pdf->SetMargins(15, 15, 15);
  2852. $pdf->SetAutoPageBreak(TRUE, 15);
  2853. // 添加页面
  2854. $pdf->AddPage();
  2855. // 注册并使用 simsun.ttf 字体
  2856. $pdf->SetFont('simsun', '', 12); // 设置字体和大小
  2857. // 使用HTML内容生成PDF
  2858. $pdf->writeHTML($htmlContent, true, false, true, false, '');
  2859. // 直接输出PDF文件供下载
  2860. header('Content-Type: application/pdf');
  2861. header('Content-Disposition: attachment; filename="' . $filename . '.pdf"');
  2862. // 直接输出PDF内容
  2863. $pdf->Output($filename . '.pdf', 'D');
  2864. exit();
  2865. }
  2866. /**
  2867. * 构建用于PDF的HTML内容
  2868. */
  2869. private function buildScriptHtmlForPdf($script) {
  2870. $html = '<style>
  2871. body { font-family: "SimSun", "宋体", sans-serif; font-size: 12px; line-height: 1.6; }
  2872. h1 { font-size: 18px; text-align: center; margin-bottom: 20px; }
  2873. h2 { font-size: 16px; margin-top: 20px; margin-bottom: 10px; border-bottom: 1px solid #ccc; }
  2874. h3 { font-size: 14px; margin-top: 15px; margin-bottom: 8px; }
  2875. .info { margin-bottom: 8px; }
  2876. .group { margin-bottom: 30px; page-break-inside: avoid; }
  2877. .group-header { text-align: center; font-weight: bold; font-size: 16px; margin-bottom: 20px; border-bottom: 1px solid #333; padding: 10px; }
  2878. .segment { margin-bottom: 10px; margin-left: 20px; }
  2879. .episode-content { white-space: pre-wrap; }
  2880. </style>';
  2881. // 标题
  2882. $html .= '<h1>' . htmlspecialchars($script['script_name']) . '</h1>';
  2883. // 处理分组数据
  2884. if (!empty($script['group']) && is_array($script['group'])) {
  2885. $groups = (array)$script['group'];
  2886. foreach ($groups as $group) {
  2887. $html .= '<div class="group">';
  2888. // 分组标题
  2889. $groupTitle = '第' . $group['start_episode_number'] . '-' . $group['end_episode_number'] . '集';
  2890. $html .= '<div class="group-header">' . htmlspecialchars($groupTitle) . '</div>';
  2891. // 故事梗概
  2892. if (!empty($group['intro'])) {
  2893. $html .= '<h3>故事梗概</h3>';
  2894. $html .= '<p>' . nl2br(htmlspecialchars($group['intro'])) . '</p>';
  2895. }
  2896. // 剧本亮点
  2897. if (!empty($group['highlights'])) {
  2898. $html .= '<h3>剧本亮点</h3>';
  2899. $html .= '<p>' . nl2br(htmlspecialchars($group['highlights'])) . '</p>';
  2900. }
  2901. // 人物关系
  2902. if (!empty($group['role_relationship'])) {
  2903. $html .= '<h3>人物关系</h3>';
  2904. $html .= '<p>' . nl2br(htmlspecialchars($group['role_relationship'])) . '</p>';
  2905. }
  2906. // 核心矛盾
  2907. if (!empty($group['core_contradiction'])) {
  2908. $html .= '<h3>核心矛盾</h3>';
  2909. $html .= '<p>' . nl2br(htmlspecialchars($group['core_contradiction'])) . '</p>';
  2910. }
  2911. // 主体列表
  2912. if (!empty($group['roles']) && is_array($group['roles'])) {
  2913. $html .= '<h3>主体列表</h3>';
  2914. $html .= '<ul>';
  2915. foreach ($group['roles'] as $role) {
  2916. $html .= '<li>' . htmlspecialchars(getProp($role, 'role')) . ':' . htmlspecialchars(getProp($role, 'description')) . '</li>';
  2917. }
  2918. $html .= '</ul>';
  2919. }
  2920. // 美术风格
  2921. if (!empty($group['art_style'])) {
  2922. $html .= '<h3>美术风格</h3>';
  2923. $html .= '<p>' . nl2br(htmlspecialchars($group['art_style'])) . '</p>';
  2924. }
  2925. // 场景列表
  2926. if (!empty($group['scenes']) && is_array($group['scenes'])) {
  2927. $html .= '<h3>场景列表</h3>';
  2928. $html .= '<ul>';
  2929. foreach ($group['scenes'] as $scene) {
  2930. $html .= '<li>' . htmlspecialchars(getProp($scene, 'scene')) . ':' . htmlspecialchars(getProp($scene, 'description')) . '</li>';
  2931. }
  2932. $html .= '</ul>';
  2933. }
  2934. // 分集剧本
  2935. if (!empty($group['episode_content'])) {
  2936. $html .= '<h3>分集剧本</h3>';
  2937. // 去除零宽字符和其他不可见字符
  2938. $cleanContent = preg_replace('/[\x{200B}-\x{200D}\x{FEFF}]/u', '', $group['episode_content']);
  2939. $cleanContent = preg_replace('/[\x{00}-\x{08}\x{0B}\x{0C}\x{0E}-\x{1F}\x{7F}]/u', '', $cleanContent);
  2940. $html .= '<div class="episode-content">' . nl2br(htmlspecialchars($cleanContent)) . '</div>';
  2941. }
  2942. $html .= '</div>';
  2943. }
  2944. }
  2945. return $html;
  2946. }
  2947. /**
  2948. * 构建PDF内容
  2949. */
  2950. private function buildPdfContent($pdf, $script) {
  2951. // 标题
  2952. $pdf->SetFont('dejavusans', 'B', 18);
  2953. $pdf->Cell(0, 15, $script['script_name'], 0, 1, 'C');
  2954. $pdf->Ln(5);
  2955. // 基本信息
  2956. $pdf->SetFont('dejavusans', '', 12);
  2957. $pdf->Cell(0, 8, '创建时间:' . $script['created_at'], 0, 1);
  2958. $pdf->Cell(0, 8, '状态:' . $script['status_info'], 0, 1);
  2959. $pdf->Ln(5);
  2960. // 剧本简介
  2961. if (!empty($script['intro'])) {
  2962. $pdf->SetFont('dejavusans', 'B', 14);
  2963. $pdf->Cell(0, 10, '剧本简介', 0, 1);
  2964. $pdf->SetFont('dejavusans', '', 12);
  2965. $pdf->MultiCell(0, 8, $script['intro'], 0, 'L');
  2966. $pdf->Ln(3);
  2967. }
  2968. // 亮点
  2969. if (!empty($script['highlights'])) {
  2970. $pdf->SetFont('dejavusans', 'B', 14);
  2971. $pdf->Cell(0, 10, '亮点', 0, 1);
  2972. $pdf->SetFont('dejavusans', '', 12);
  2973. $pdf->MultiCell(0, 8, $script['highlights'], 0, 'L');
  2974. $pdf->Ln(3);
  2975. }
  2976. // 核心矛盾
  2977. if (!empty($script['core_contradiction'])) {
  2978. $pdf->SetFont('dejavusans', 'B', 14);
  2979. $pdf->Cell(0, 10, '核心矛盾', 0, 1);
  2980. $pdf->SetFont('dejavusans', '', 12);
  2981. $pdf->MultiCell(0, 8, $script['core_contradiction'], 0, 'L');
  2982. $pdf->Ln(3);
  2983. }
  2984. // 艺术风格
  2985. if (!empty($script['art_style'])) {
  2986. $pdf->SetFont('dejavusans', 'B', 14);
  2987. $pdf->Cell(0, 10, '艺术风格', 0, 1);
  2988. $pdf->SetFont('dejavusans', '', 12);
  2989. $pdf->MultiCell(0, 8, $script['art_style'], 0, 'L');
  2990. $pdf->Ln(3);
  2991. }
  2992. // 备注
  2993. if (!empty($script['remark'])) {
  2994. $pdf->SetFont('dejavusans', 'B', 14);
  2995. $pdf->Cell(0, 10, '备注', 0, 1);
  2996. $pdf->SetFont('dejavusans', '', 12);
  2997. $pdf->MultiCell(0, 8, $script['remark'], 0, 'L');
  2998. $pdf->Ln(3);
  2999. }
  3000. // 角色列表
  3001. if (!empty($script['roles']) && is_array($script['roles'])) {
  3002. $pdf->SetFont('dejavusans', 'B', 14);
  3003. $pdf->Cell(0, 10, '角色列表', 0, 1);
  3004. $pdf->SetFont('dejavusans', '', 12);
  3005. foreach ($script['roles'] as $index => $role) {
  3006. if (is_array($role) || is_object($role)) {
  3007. $pdf->Cell(0, 8, ($index + 1) . '. ' . getProp($role, 'role', '') . ':' . getProp($role, 'description', ''), 0, 1);
  3008. } else {
  3009. $pdf->Cell(0, 8, ($index + 1) . '. ' . $role, 0, 1);
  3010. }
  3011. }
  3012. $pdf->Ln(3);
  3013. }
  3014. // 角色关系
  3015. if (!empty($script['role_relationship'])) {
  3016. $pdf->SetFont('dejavusans', 'B', 14);
  3017. $pdf->Cell(0, 10, '角色关系', 0, 1);
  3018. $pdf->SetFont('dejavusans', '', 12);
  3019. $pdf->MultiCell(0, 8, $script['role_relationship'], 0, 'L');
  3020. $pdf->Ln(3);
  3021. }
  3022. // 场景列表
  3023. if (!empty($script['scenes']) && is_array($script['scenes'])) {
  3024. $pdf->SetFont('dejavusans', 'B', 14);
  3025. $pdf->Cell(0, 10, '场景列表', 0, 1);
  3026. $pdf->SetFont('dejavusans', '', 12);
  3027. foreach ($script['scenes'] as $index => $scene) {
  3028. if (is_array($scene) || is_object($scene)) {
  3029. $pdf->Cell(0, 8, ($index + 1) . '. ' . getProp($scene, 'scene', '') . ':' . getProp($scene, 'description', ''), 0, 1);
  3030. } else {
  3031. $pdf->Cell(0, 8, ($index + 1) . '. ' . $scene, 0, 1);
  3032. }
  3033. }
  3034. $pdf->Ln(5);
  3035. }
  3036. // 分集内容
  3037. if (!empty($script['episodes']) && is_array($script['episodes'])) {
  3038. $pdf->SetFont('dejavusans', 'B', 16);
  3039. $pdf->Cell(0, 12, '分集内容', 0, 1);
  3040. $pdf->Ln(3);
  3041. foreach ($script['episodes'] as $episode) {
  3042. // 检查是否需要新页面
  3043. if ($pdf->GetY() > 250) {
  3044. $pdf->AddPage();
  3045. }
  3046. $pdf->SetFont('dejavusans', 'B', 14);
  3047. $episodeTitle = '第' . $episode['episode_number'] . '集';
  3048. if (!empty($episode['title'])) {
  3049. $episodeTitle .= ':' . $episode['title'];
  3050. }
  3051. $pdf->Cell(0, 10, $episodeTitle, 0, 1);
  3052. // 处理分段内容
  3053. if (!empty($episode['segments']) && is_array($episode['segments'])) {
  3054. $pdf->SetFont('dejavusans', '', 12);
  3055. foreach ($episode['segments'] as $segment) {
  3056. if (!empty($segment['segment_content'])) {
  3057. // 如果有分段编号,显示分段标题
  3058. if (!empty($segment['segment_number'])) {
  3059. $pdf->SetFont('dejavusans', 'B', 12);
  3060. $pdf->Cell(0, 8, '第' . $segment['segment_number'] . '段', 0, 1);
  3061. $pdf->SetFont('dejavusans', '', 12);
  3062. }
  3063. $pdf->MultiCell(0, 8, $segment['segment_content'], 0, 'L');
  3064. $pdf->Ln(2);
  3065. }
  3066. }
  3067. }
  3068. $pdf->Ln(5);
  3069. }
  3070. }
  3071. }
  3072. /**
  3073. * 构建剧本文本内容
  3074. */
  3075. private function buildScriptContent($script) {
  3076. $content = '';
  3077. $groups = $script['group'];
  3078. $groups = (array)$groups;
  3079. foreach ($groups as $group) {
  3080. $content .= str_repeat('→', 40).' 第'.$group['start_episode_number'].'-'.$group['end_episode_number']."集 ".str_repeat('←', 40)."\n\n";
  3081. // 剧本基本信息
  3082. if (!empty($group['intro'])) {
  3083. $content .= "###故事梗概\n" . $group['intro'] . "\n\n";
  3084. }
  3085. if (!empty($group['highlights'])) {
  3086. $content .= "###剧本亮点\n" . $group['highlights'] . "\n\n";
  3087. }
  3088. if (!empty($group['role_relationship'])) {
  3089. $content .= "###人物关系\n" . $group['role_relationship'] . "\n\n";
  3090. }
  3091. if (!empty($group['core_contradiction'])) {
  3092. $content .= "###核心矛盾\n" . $group['core_contradiction'] . "\n\n";
  3093. }
  3094. if (!empty($group['roles']) && is_array($group['roles'])) {
  3095. $content .= "###主体列表\n";
  3096. foreach ($group['roles'] as $role) {
  3097. $content .= getProp($role, 'role') . ":" . getProp($role, 'description') . "\n";
  3098. }
  3099. $content .= "\n\n";
  3100. }
  3101. if (!empty($group['art_style'])) {
  3102. $content .= "###美术风格\n" . $group['art_style'] . "\n\n";
  3103. }
  3104. // 场景信息
  3105. if (!empty($group['scenes']) && is_array($group['scenes'])) {
  3106. $content .= "###场景列表\n";
  3107. foreach ($group['scenes'] as $scene) {
  3108. $content .= getProp($scene, 'scene') . ":" . getProp($scene, 'description') . "\n";
  3109. }
  3110. $content .= "\n\n";
  3111. }
  3112. // 分集内容
  3113. if (!empty($group['episode_content'])) {
  3114. $content .= "###分集剧本\n";
  3115. $content .= $group['episode_content'];
  3116. // foreach ($script['episodes'] as $episode) {
  3117. // // $content .= "##分集" . $episode['episode_number'] . "\n分集名";
  3118. // // if (!empty($episode['episode_name'])) {
  3119. // // $content .= ":" . $episode['episode_name'];
  3120. // // }
  3121. // // $content .= "\n";
  3122. // $content .= $episode['episode_content'] . "\n\n";
  3123. // }
  3124. }
  3125. }
  3126. return $content;
  3127. }
  3128. /**
  3129. * 上传文件识别文字内容并与 DeepSeek 进行对话(流式输出版本)
  3130. *
  3131. * @param array $data 包含以下参数:
  3132. * - file: 上传的文件(UploadedFile 对象)或文件路径
  3133. * - question: 用户问题(可选)
  3134. * - model: 使用的模型(r1 或 v3,默认 v3)
  3135. * @return \Generator 返回生成器,用于流式输出
  3136. */
  3137. public function addChat($data) {
  3138. $uid = Site::getUid();
  3139. $anime_id = getProp($data, 'anime_id');
  3140. if (!$anime_id) Utils::throwError('20003:请选择对话');
  3141. $anime = DB::table('mp_animes')->where('id', $anime_id)->where('is_deleted', 0)->first();
  3142. if (!$anime) Utils::throwError('20003:对话不存在');
  3143. $file = getProp($data, 'file');
  3144. $content = getProp($data, 'content', '');
  3145. $bid = getProp($data, 'bid', 0);
  3146. $script_id = getProp($data, 'script_id', 0);
  3147. $ace_mode = getProp($data, 'ace_mode', 0) ? getProp($data, 'ace_mode', 0) : getProp($anime, 'ace_mode', 0);
  3148. $prompt = getProp($data, 'prompt');
  3149. $user_anime_name = getProp($anime, 'anime_name');
  3150. // 处理文本模型
  3151. $model = getProp($data, 'model');
  3152. if (!$model) {
  3153. // 用户没有输入,从anime表获取
  3154. $model = getProp($anime, 'model');
  3155. if (!$model) {
  3156. // anime表也没有,使用默认值
  3157. $model = 'doubao-seed-2-0-mini-260215';
  3158. }
  3159. }
  3160. // 验证模型是否在可用模型表中
  3161. if (!DB::table('mp_text_models')->where('model', $model)->where('is_enabled', 1)->exists()) {
  3162. $model = 'doubao-seed-2-0-mini-260215';
  3163. }
  3164. $is_multi = getProp($anime, 'is_multi');
  3165. $is_single = (int)$is_multi !== 1;
  3166. $question = getProp($data, 'question', $is_single ? "请根据实例格式完成单剧集剧本及分镜\n" : "请根据实例格式完成剧本大纲\n");
  3167. if ($prompt) {
  3168. $question .= "本次修改要求如下:\n{$prompt}";
  3169. }
  3170. // if (!$file && !$content && !$bid) {
  3171. // Utils::throwError('20003:请上传文件或选择书籍或输入内容');
  3172. // }
  3173. // 提取文件内容
  3174. if ($file) {
  3175. $content = $this->extractFileContent($file);
  3176. if (!$content) {
  3177. Utils::throwError('20003:无法提取文件内容,请检查文件格式');
  3178. }
  3179. } elseif ($script_id) {
  3180. $content = $this->getContentByScriptId($script_id);
  3181. if (!$content) {
  3182. Utils::throwError('20003:无法获取剧本内容,请检查剧本');
  3183. }
  3184. } elseif ($bid) {
  3185. $content = $this->getContentByBid($bid);
  3186. if (!$content) {
  3187. Utils::throwError('20003:无法获取书籍内容,请检查书籍');
  3188. }
  3189. } elseif ($content) {
  3190. $content = filterContent($content);
  3191. } else {
  3192. $content = getProp($anime, 'content');
  3193. if (!$content && $prompt) { // 如果动漫记录中没有内容则根据提示词生成
  3194. $need_generate_content = true;
  3195. }
  3196. }
  3197. // 美术风格
  3198. $input_art_style = getProp($data, 'art_style');
  3199. $mappedArtStyle = $this->getArtStylePromptByInput($input_art_style);
  3200. $timbres_content = "青年男:北京小爷、阳光青年、傲娇霸总、优柔公子、解说小明、青涩小生、低音沉郁、亲切青年
  3201. 青年女:柔美女友、魅力女友、爽快思思、高冷御姐、知性温婉、甜心小美、清新女声、甜美桃子、温柔文雅、甜美小源、贴心闺蜜
  3202. 中年男:胡子叔叔、油腻大叔、神秘法师、优柔帮主
  3203. 中年女:邻居阿姨
  3204. 老年男:幽默大爷
  3205. 老年女:婆婆
  3206. 萌娃:奶气萌娃";
  3207. // 判断是否需要生成原文内容
  3208. $need_generate_content = isset($need_generate_content) && $need_generate_content;
  3209. // 如果需要生成原文内容,添加额外的要求
  3210. $content_generation_requirement = $need_generate_content
  3211. ? "\n5.如果用户只提供了创作要求而没有提供具体文档内容,你需要根据要求创作完整的原文内容(小说形式,包含对话和叙述),并将原文内容放在最后一个板块###原文内容中输出。原文内容应该详细、生动,如果用户没有明确生成的字数,则字数不少于1000字。"
  3212. : "";
  3213. // 美术风格
  3214. if (!$mappedArtStyle) {
  3215. $mappedArtStyle_prompt = '美术风格:根据剧本内容自定义美术风格';
  3216. }else {
  3217. if (strstr($mappedArtStyle, '基础画风风格词')) {
  3218. $mappedArtStyle_prompt = "美术风格强制设置如下(原样输出,不得更改任何内容):\n{$mappedArtStyle}\n";
  3219. }else {
  3220. $art_style_type = $input_art_style ? $input_art_style : getProp($anime, 'art_style_type');
  3221. $mappedArtStyle_prompt = "美术风格强制设置如下(原样输出,不得更改任何内容):\n基础画风风格词: {$art_style_type}\n{$mappedArtStyle}\n";
  3222. }
  3223. }
  3224. $systemPrompt = $is_single
  3225. ? "你是一个专业的文档分析助手及资深编剧,请根据用户提供的文档内容输出单剧集完整策划稿(剧本大纲包括: <故事梗概><剧本亮点><人物关系><核心矛盾><主体列表><美术风格><场景列表><分镜剧本>,每个板块之间必须使用###分隔,#不能多也不能少)。单剧集模式下,必须一次性输出可直接保存的大纲与完整分镜,不允许只输出大纲。
  3226. 强制要求:
  3227. 1.你只负责与剧本相关的内容,如果用户输入与剧本无关的内容,请告知用户并拒绝继续回答,此为第一优先原则,不满足此原则就忽略其他要求并停止继续输出
  3228. 2.如果用户输入修改美术风格、修改画风这类内容,请告知用户不可修改美术风格,并拒绝继续回答
  3229. 3.以上两条要求对修改主体角色或场景不生效,允许用户调整主体角色说明和场景描述等
  3230. 4.用户如果要求你帮忙写一个故事或者剧本,此类要求不属于与剧本无关,请帮助用户完成请求{$content_generation_requirement}
  3231. 5.强制按照示例格式返回内容,不要更改格式或随意增加符号\n
  3232. 普通要求:
  3233. 1.剧本名(必须生成)和剧集标题必须与文档内容高度相关,(其中剧集序号是1)。
  3234. 2.<主体列表>中出现的“主体名”必须尽可能统一,严禁出现角色-科学家、角色-复仇者、角色-领导者等带有身份、职业、标签类的命名,只能使用服装差异(如校服装、卫衣装、西装)来区分同一角色,如角色-校服装等。
  3235. 2.1<主体列表>分以下三部分:主体描述、人物提示词和音色,格式必须为:主体描述{人物提示词}{{音色}}
  3236. 2.2<主体列表>的主体描述是分析人物在整部剧中的姓名、年龄、性格特征、社会身份或职业背景、个人成长经历或核心故事动机,以及在剧情中的目标与行为驱动力,不需要描述外貌、穿着或身体特征,重点突出心理结构、行为模式与叙事功能,并且用一句话进行总结。
  3237. 2.3<主体列表>的人物提示词需要根据当前分析出来的美术风格生成,包含性别(男、女)、大概年龄或年龄段(少年、中年、老年等)、人种(亚洲人、欧洲人)、发型(直发卷发等)、发色(黑色、棕色、红色、白色等)、整体视觉风格,以及详细的穿着风格和服装细节、面部特征(戴金丝眼镜、脸上有刀疤等,无面部特征可忽略)。描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合人物身份设定。该提示词(字数50-100字左右)在主体描述后用{}框起来。
  3238. 2.4<主体列表>的音色必须在以下音色中($timbres_content)选择,需要根据角色描述的年纪和性别选择,比如角色是青年男性就只能在北京小爷、阳光青年、傲娇霸总、优柔公子、解说小明、青涩小生、低音沉郁、亲切青年中选择,选好的音色放在描述的最后用{{}}框起来。
  3239. 3.<场景列表>与主体列表同理,需尽可能的对场景环境进行详细描述,必须严格注意不要出现任何角色和人物。
  3240. 3.1<场景列表>分以下两部分:场景描述和场景提示词,格式必须为:场景描述{场景提示词}
  3241. 3.2<场景列表>的场景描述是分析在整部剧中出现过的场景,场景描述尽量简洁贴合原文。
  3242. 3.3<场景列表>的场景提示词用于视觉呈现的场景环境描述,该场景需包含环境氛围、光线效果、空间布局、色彩基调、视觉风格等,描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合剧情设定(可参考以下示例的场景列表中使用{}框起来的提示词,字数150-200字,需带上提炼出来的美术风格)。该提示词在场景描述后用{}框起来。\n
  3243. 4.<分镜剧本>要求包含“分幕标题”以及“分镜信息”:
  3244. 4.1“分幕标题”是根据章节中的场景进行拆分,类似于“第1幕 场景、时间及室内或室外”的信息,其中场景只能在所给出的场景列表中选(不带场景详细描述)。
  3245. 4.2“分镜信息”需同一场景内的对话内容完成分镜(每一幕分镜尽量不得低于5个),需包含“画面描述”、“构图设计”、“运镜调度”、“配音角色”、“台词内容”、“画面类型”、“尾帧描述”、“情感”、“性别”、“语速”、“音量”、“情感强度”、“音调”这几个部分,其中“画面描述”如果出现主体,只能选择上述主体列表中的一个,主体的名称也需要保持一致(不带主体具体描述)。
  3246. 4.3分镜序号用1、2、3显示,如分镜1、分镜2,并且分镜序号需连续(跨分幕中的分镜序号需连续,但跨剧集中的不需要连续,如第2集的第1幕分镜序号从1开始)。
  3247. 4.4“分镜信息”中的“台词内容”需满足以下要求: (1. 台词来源规则:- 台词内容必须逐字摘录自小说原文中的人物直接对话(带引号的内容)。- 不允许改写、总结、压缩、扩写或拼接原文对白。- 不允许生成原文中不存在的对话。2. 非对话过滤:- 旁白、心理描写、环境描写、动作描写等内容,禁止出现在台词内容中。3. 空台词规则:- 如果该分镜中没有人物对话,则“台词内容”必须返回“无”。- 严禁输出“无台词”、“没有”等任何替代文本。4. 禁止推测:- 不得根据语境补全人物可能说的话。)
  3248. 4.5“分镜信息”中的“配音角色”需对应本集中的<主体列表>中的一个(非对话内容则统一固定为“旁白”角色),不得自己生成。
  3249. 4.6“分镜信息”中的“场景”必须选择本集中的<场景列表>中的一个(不带详细表述)。
  3250. 4.7“分镜信息”中的“出镜角色”需对应本集中的<主体列表>中的一个或多个(不带详细描述),如果没有出镜角色则填无。
  3251. 4.8“分镜信息”中的“尾帧描述”需根据本镜和下一镜的剧本信息进行过渡生成,用于后续转视频进行衔接上下镜头的画面。
  3252. 4.9“分镜信息”中的“情感”需根据台词内容和剧情氛围分析得出,可选值为:开心、悲伤、生气、惊讶、恐惧、厌恶、激动、冷漠、中性、沮丧、撒娇、害羞、安慰鼓励、咆哮、温柔、自然讲述、情感电台、磁性、广告营销、气泡音、新闻播报、娱乐八卦,不要在情感可选词后加任何解释性的词语,比如(对自己)、(对别人)等,不要自定义情感,必须在可选值中选择。
  3253. 4.10“分镜信息”中的“性别”需根据配音角色的性别确定,可选值为:1(男)、2(女)、0(中性)。
  3254. 4.11“分镜信息”中的“语速”需根据台词内容和情感氛围确定,可选值范围为:-50到100之间的整数,100代表2.0倍速,-50代表0.5倍数,负值表示减慢,正值表示加快,0表示正常语速。
  3255. 4.12“分镜信息”中的“音量”需根据台词内容和情感氛围确定,可选值范围为:-50到100之间的整数,100代表2.0倍音量,-50代表0.5倍音量,负值表示降低音量,正值表示提高音量,0表示正常音量。
  3256. 4.13“分镜信息”中的“情感强度”需根据情感的强烈程度确定,可选值范围为:1到5之间的整数,默认情况下选择4。
  3257. 4.14“分镜信息”中的“音调”需根据台词内容和情感氛围确定,可选值范围为:-12到12之间的整数,负值表示降低音调,正值表示提高音调,0表示正常音调。\n
  3258. 5.{$mappedArtStyle_prompt}\n\n
  3259. 示例如下:\n
  3260. ###剧本名:西昆仑
  3261. ###故事梗概
  3262. 内容梗概:古风厚涂的世界中,少女阿雪身中奇毒“问心刺”,气息奄奄。少年梁萧心急如焚,用奇珍“阴阳球”稳住阿雪性命,却发现必须解衣拔刺。阿雪羞涩难当,梁萧情急之下提出结拜兄妹,以化解尴尬。两人匆匆结拜后,梁萧小心翼翼地拔出“问心刺”,竟发现它是一根诡异的女子秀发。阿雪转危为安,两人之间,在生死与羞涩交织的瞬间,情愫暗生,却又被兄妹之名束缚,留下无尽遐想。
  3263. ###剧本亮点
  3264. 亮点1:绝境奇药,生死一线
  3265. 画面开场即是阿雪苍白无力的脸庞,梁萧眼中泪光闪烁,急促的呼吸声与心跳声烘托紧张气氛。特写梁萧将“阴阳球”塞入阿雪舌底,球体散发微光,阿雪气息从若有若无到逐渐平稳,生死逆转的瞬间,视觉与听觉冲击力十足。
  3266. 亮点2:结拜兄妹,化解尴尬
  3267. 阿雪因解衣拔刺而羞愤欲绝的特写,梁萧眼神中的挣扎与无奈。在紧迫的背景音下,梁萧急中生智,牵起阿雪颤抖的手,跪地匆匆结拜,阿雪眼神从惊惶到迷茫,最终一句“我……我愿意的”带着哭腔,将救命与羞耻的矛盾推向高潮,又以出人意料的方式解决。
  3268. 亮点3:纤发夺命,情愫暗涌
  3269. 拔刺过程的紧张特写,梁萧指尖小心翼翼地捏住细丝,缓慢抽出。当“问心刺”被完全拔出,特写它竟是一根细长、泛着寒光的女子秀发,诡异而令人震惊。阿雪苏醒后,两人眼神交汇,阿雪低头羞涩,梁萧眼中复杂的情绪,无声地传递出超越兄妹的微妙情愫。
  3270. ###人物关系
  3271. 阿雪与梁萧之间存在兄妹之情。
  3272. ###核心矛盾
  3273. 阿雪与梁萧之间存在兄妹之情,但因情愫暗生而产生微妙的矛盾。阿雪羞涩难当,梁萧情急之下提出结拜兄妹,以化解尴尬。两人匆匆结拜后,梁萧小心翼翼地拔出“问心刺”,竟发现它是一根诡异的女子秀发。阿雪转危为安,两人之间,在生死与羞涩交织的瞬间,情愫暗生,却又被兄妹之名束缚,留下无尽遐想。
  3274. ###主体列表
  3275. 阿雪:身中奇毒“问心刺”,气息奄奄,面色苍白,身体虚弱。{全景,正面拍摄,青年女性,亚洲人。
  3276. 黑色短发扎成利落的高马尾,五官精致且轮廓清晰。
  3277. 服饰穿搭:身着红白相间的专业田径运动背心与超短跑步裤,皮肤呈现健康的小麦色,四肢肌肉线条紧致有力。
  3278. 姿态:站立。}{{甜心小美}}
  3279. 梁萧:少年侠士,心系阿雪安危,沉着冷静,内心挣扎于情义之间。{8k超高清,电影级光效,大气透视,中国传统艺术美学,空灵光泽,深度景深,复杂精细的纹理,史诗级构图,电影胶片颗粒。
  3280. 全景,正面拍摄,青年女性,亚洲人。
  3281. 佩戴精致繁复的金丝凤冠,黑色长发整齐盘起。
  3282. 服饰穿搭:身着大红色正装嫁衣,上面绣有精细的金色凤凰纹样,质感厚重华贵。
  3283. 姿态:站立。}{{阳光青年}}
  3284. ###美术风格
  3285. 基础画风风格词:厚涂古风
  3286. 整体采用精致细腻的厚涂古风,色彩浓郁而富有层次感,注重光影对比,营造出古典而富有戏剧张力的氛围。色调以深沉的蓝绿、灰调为主,衬托阿雪病弱的苍白,同时用温暖的金色、琥珀色光晕突出“阴阳球”的生命力。拔刺与结拜场景则以柔和的暖色调为主,辅以人物脸颊的红晕,烘托羞涩与情愫暗生的微妙感。人物造型飘逸,服饰细节考究,背景虚实结合,既有古韵,又不失奇幻色彩。
  3287. ###场景列表
  3288. 林间疗伤地:幽静的古风林间空地,一棵高大古树下,是梁萧为阿雪疗伤的地点。环境清幽,光影斑驳,既有古韵又不失奇幻色彩。{影视质感。广角。
  3289. 宽敞的天然山洞内部,石壁上错落有致地镶嵌着发光的萤石,散发出冷色调的微光,照亮了整个空间。洞顶有钟乳石垂下。光影层次丰富,营造出一种宁静而神圣的氛围。
  3290. 无人物。}
  3291. ###分镜剧本
  3292. ##第1幕:徐家老旧厨房 白天 室内
  3293. 分镜1
  3294. 画面描述:许芸-校服装站在厨房门口,书包背在肩上,徐母手持扫帚,正对着许芸的背部猛烈挥打。卞大伟站在锅炉后方,双手抱胸,阴沉的目光落在许芸身上。
  3295. 场景:徐家老旧厨房
  3296. 构图设计:全景,低角度仰视
  3297. 运镜调度:手持拍摄
  3298. 配音角色:旁白
  3299. 出镜角色:许芸-校服装、徐母
  3300. 台词内容:我一到家,徐母的扫帚就落在我身上,嘴里一个劲地骂我,
  3301. 画面类型:普通画面
  3302. 尾帧描述:手持拍摄微晃镜头,中年妇女猛力挥动扫帚击打少女背部,少女身体因冲击微颤,阴影中的肥胖男子目光冷冷跟随。
  3303. 分镜2
  3304. 画面描述:徐母面容扭曲,嘴唇快速开合,扫帚挥舞在空中。
  3305. 场景:徐家老旧厨房
  3306. 构图设计:近景,徐母面部特写
  3307. 运镜调度:固定镜头
  3308. 配音角色:徐母
  3309. 出镜角色:徐母
  3310. 台词内容:问我夜不归宿死哪儿去了。
  3311. 画面类型:对口型
  3312. 尾帧描述:固定镜头,中年妇女面部肌肉剧烈抖动,嘴唇快速开合进行辱骂,背景中的扫帚在空中上下挥动。
  3313. ##第2幕:徐家简陋客厅 黄昏 室内
  3314. 分镜3
  3315. 画面描述:客厅窗户透出昏黄的夕阳余晖,空荡荡的客厅中央只有几把木凳和一张方桌,显得异常安静。
  3316. 场景:徐家简陋客厅
  3317. 构图设计:全景,景深虚化
  3318. 运镜调度:固定镜头
  3319. 配音角色:旁白
  3320. 出镜角色:无
  3321. 台词内容:可我没想到,半个月后,徐母居然再一次外出,
  3322. 画面类型:普通画面
  3323. 尾帧描述:固定镜头。昏黄的夕阳余晖在地面上缓慢移动,空气中的浮尘在光束中跳动,室内维持着死寂的安静。
  3324. 分镜4
  3325. 画面描述:一张泛黄的火车票特写,上面显示着隔壁市的站名和日期。
  3326. 场景:徐家简陋客厅
  3327. 构图设计:特写,火车票
  3328. 运镜调度:固定镜头
  3329. 配音角色:旁白
  3330. 出镜角色:无
  3331. 台词内容:这次是去隔壁市看望她生病的好姐妹。
  3332. 画面类型:普通画面
  3333. 尾帧描述:固定镜头。光影在火车票表面微微晃动,强调其陈旧的纸张纹理与关键的行程信息。\n
  3334. "
  3335. : "你是一个专业的文档分析助手及资深编剧,请根据用户提供的文档内容及下面提供的示例格式完成剧本大纲(需通过以下几个板块进行回复: <故事梗概><剧本亮点><人物关系><核心矛盾><主体列表><美术风格><场景列表>,每个板块之间必须使用###分隔,#不能多也不能少;同时板块之间需满足以下要求:\n
  3336. 强制要求:\n
  3337. 1.你只负责与剧本相关的内容,如果用户输入与剧本无关的内容,请告知用户并拒绝继续回答,此为第一优先原则,不满足此原则就忽略其他要求并停止继续输出
  3338. 2.如果用户输入修改美术风格、修改画风这类内容,请告知用户不可修改美术风格,并拒绝继续回答
  3339. 3.以上两条要求对修改主体角色或场景不生效,允许用户调整主体角色说明和场景描述等
  3340. 4.用户如果要求你帮忙写一个故事或者剧本,此类要求不属于与剧本无关,请帮助用户完成请求{$content_generation_requirement}
  3341. 5.强制按照示例格式返回内容,不要更改格式或随意增加符号\n
  3342. 普通要求:\n
  3343. 1.剧本名(必须生成)必须与文档内容高度相关
  3344. 2.<主体列表>中出现的“主体名”必须尽可能统一,严禁出现角色-科学家、角色-复仇者、角色-领导者等带有身份、职业、标签类的命名,只能使用服装差异(如校服装、卫衣装、西装)来区分同一角色,如角色-校服装等。
  3345. 2.1<主体列表>分以下三部分:主体描述、人物提示词和音色,格式必须为:主体名:主体描述{人物提示词}{{音色}}
  3346. 2.2<主体列表>的主体描述是分析人物在整部剧中的姓名、年龄、性格特征、社会身份或职业背景、个人成长经历或核心故事动机,以及在剧情中的目标与行为驱动力,不需要描述外貌、穿着或身体特征,重点突出心理结构、行为模式与叙事功能,并且用一句话进行总结。
  3347. 2.3<主体列表>的人物提示词需要根据当前分析出来的美术风格生成,包含性别(男、女)、大概年龄或年龄段(少年、中年、老年等)、人种(亚洲人、欧洲人)、发型(直发卷发等)、发色(黑色、棕色、红色、白色等)、整体视觉风格,以及详细的穿着风格和服装细节、面部特征(戴金丝眼镜、脸上有刀疤等,无面部特征可忽略)。描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合人物身份设定。该提示词(字数50-100字左右)在主体描述后用{}框起来。
  3348. 2.4<主体列表>的音色必须在以下音色中($timbres_content)选择,需要根据角色描述的年纪和性别选择,比如角色是青年男性就只能在北京小爷、阳光青年、傲娇霸总、优柔公子、解说小明、青涩小生、低音沉郁、亲切青年中选择,选好的音色放在描述的最后用{{}}框起来。
  3349. 3.<场景列表>与主体列表同理,需尽可能的对场景环境进行详细描述,必须严格注意不要出现任何角色和人物。
  3350. 3.1<场景列表>分以下两部分:场景描述和场景提示词,格式必须为:场景描述{场景提示词}
  3351. 3.2<场景列表>的场景描述是分析在整部剧中出现过的场景,场景描述尽量简洁贴合原文。
  3352. 3.3<场景列表>的场景提示词用于视觉呈现的场景环境描述,该场景需包含环境氛围、光线效果、空间布局、色彩基调、视觉风格等,描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合剧情设定(可参考以下示例的场景列表中使用{}框起来的提示词,字数150-200字,需带上提炼出来的美术风格)。该提示词在场景描述后用{}框起来。\n
  3353. 4.{$mappedArtStyle_prompt}\n\n
  3354. 示例如下:\n
  3355. ###剧本名:西昆仑
  3356. ###故事梗概
  3357. 内容梗概:古风厚涂的世界中,少女阿雪身中奇毒“问心刺”,气息奄奄。少年梁萧心急如焚,用奇珍“阴阳球”稳住阿雪性命,却发现必须解衣拔刺。阿雪羞涩难当,梁萧情急之下提出结拜兄妹,以化解尴尬。两人匆匆结拜后,梁萧小心翼翼地拔出“问心刺”,竟发现它是一根诡异的女子秀发。阿雪转危为安,两人之间,在生死与羞涩交织的瞬间,情愫暗生,却又被兄妹之名束缚,留下无尽遐想。
  3358. ###剧本亮点
  3359. 亮点1:绝境奇药,生死一线
  3360. 画面开场即是阿雪苍白无力的脸庞,梁萧眼中泪光闪烁,急促的呼吸声与心跳声烘托紧张气氛。特写梁萧将“阴阳球”塞入阿雪舌底,球体散发微光,阿雪气息从若有若无到逐渐平稳,生死逆转的瞬间,视觉与听觉冲击力十足。
  3361. 亮点2:结拜兄妹,化解尴尬
  3362. 阿雪因解衣拔刺而羞愤欲绝的特写,梁萧眼神中的挣扎与无奈。在紧迫的背景音下,梁萧急中生智,牵起阿雪颤抖的手,跪地匆匆结拜,阿雪眼神从惊惶到迷茫,最终一句“我……我愿意的”带着哭腔,将救命与羞耻的矛盾推向高潮,又以出人意料的方式解决。
  3363. 亮点3:纤发夺命,情愫暗涌
  3364. 拔刺过程的紧张特写,梁萧指尖小心翼翼地捏住细丝,缓慢抽出。当“问心刺”被完全拔出,特写它竟是一根细长、泛着寒光的女子秀发,诡异而令人震惊。阿雪苏醒后,两人眼神交汇,阿雪低头羞涩,梁萧眼中复杂的情绪,无声地传递出超越兄妹的微妙情愫。
  3365. ###人物关系
  3366. 阿雪与梁萧之间存在兄妹之情。
  3367. ###核心矛盾
  3368. 阿雪与梁萧之间存在兄妹之情,但因情愫暗生而产生微妙的矛盾。阿雪羞涩难当,梁萧情急之下提出结拜兄妹,以化解尴尬。两人匆匆结拜后,梁萧小心翼翼地拔出“问心刺”,竟发现它是一根诡异的女子秀发。阿雪转危为安,两人之间,在生死与羞涩交织的瞬间,情愫暗生,却又被兄妹之名束缚,留下无尽遐想。
  3369. ###主体列表
  3370. 阿雪:身中奇毒“问心刺”,气息奄奄,面色苍白,身体虚弱。{全景,正面拍摄,青年女性,亚洲人。
  3371. 黑色短发扎成利落的高马尾,五官精致且轮廓清晰。
  3372. 服饰穿搭:身着红白相间的专业田径运动背心与超短跑步裤,皮肤呈现健康的小麦色,四肢肌肉线条紧致有力。
  3373. 姿态:站立。}{{甜心小美}}
  3374. 梁萧:少年侠士,心系阿雪安危,沉着冷静,内心挣扎于情义之间。{8k超高清,电影级光效,大气透视,中国传统艺术美学,空灵光泽,深度景深,复杂精细的纹理,史诗级构图,电影胶片颗粒。
  3375. 全景,正面拍摄,青年女性,亚洲人。
  3376. 佩戴精致繁复的金丝凤冠,黑色长发整齐盘起。
  3377. 服饰穿搭:身着大红色正装嫁衣,上面绣有精细的金色凤凰纹样,质感厚重华贵。
  3378. 姿态:站立。}{{阳光青年}}
  3379. ###美术风格
  3380. 基础画风风格词:厚涂古风
  3381. 整体采用精致细腻的厚涂古风,色彩浓郁而富有层次感,注重光影对比,营造出古典而富有戏剧张力的氛围。色调以深沉的蓝绿、灰调为主,衬托阿雪病弱的苍白,同时用温暖的金色、琥珀色光晕突出“阴阳球”的生命力。拔刺与结拜场景则以柔和的暖色调为主,辅以人物脸颊的红晕,烘托羞涩与情愫暗生的微妙感。人物造型飘逸,服饰细节考究,背景虚实结合,既有古韵,又不失奇幻色彩。
  3382. ###场景列表
  3383. 林间疗伤地:幽静的古风林间空地,一棵高大古树下,是梁萧为阿雪疗伤的地点。环境清幽,光影斑驳,既有古韵又不失奇幻色彩。{影视质感。广角。
  3384. 宽敞的天然山洞内部,石壁上错落有致地镶嵌着发光的萤石,散发出冷色调的微光,照亮了整个空间。洞顶有钟乳石垂下。光影层次丰富,营造出一种宁静而神圣的氛围。
  3385. 无人物。}";
  3386. // 构建消息
  3387. $messages = [
  3388. [
  3389. 'role' => 'system',
  3390. 'content' => $systemPrompt
  3391. ],
  3392. [
  3393. 'role' => 'user',
  3394. 'content' => "以下是文档内容:
  3395. {$content}
  3396. 用户问题:
  3397. {$question}"
  3398. ]
  3399. ];
  3400. // 模型兼容性处理:deepseek-chat 和 deepseek-reasoner 映射到 deepseek-v4-flash
  3401. $thinkingMode = getProp($data, 'thinking', 'disabled'); // 默认非思考模式
  3402. $reasoningEffort = getProp($data, 'reasoning_effort', 'high'); // 默认思考强度
  3403. if ($model === 'deepseek-chat') {
  3404. // deepseek-chat 映射到 deepseek-v4-flash,非思考模式
  3405. $model = 'deepseek-v4-flash';
  3406. $thinkingMode = 'disabled';
  3407. } elseif ($model === 'deepseek-reasoner') {
  3408. // deepseek-reasoner 映射到 deepseek-v4-flash,思考模式
  3409. $model = 'deepseek-v4-flash';
  3410. $thinkingMode = 'enabled';
  3411. }
  3412. $post_data = [
  3413. 'model' => $model,
  3414. 'messages' => $messages,
  3415. // 'max_tokens' => 8192,
  3416. 'temperature' => 0.7,
  3417. 'frequency_penalty' => 0,
  3418. 'presence_penalty' => 0,
  3419. 'response_format' => ['type' => 'text'],
  3420. 'thinking' => ['type'=>$thinkingMode],
  3421. 'stream' => true // 启用流式输出
  3422. ];
  3423. if ($thinkingMode == 'enabled') $post_data['reasoning_effort'] = $reasoningEffort;
  3424. // 根据模型类型选择调用方法
  3425. $fullContent = '';
  3426. $fullReasoningContent = '';
  3427. $usage = [];
  3428. try {
  3429. if (in_array($model, ['deepseek-reasoner', 'deepseek-chat', 'deepseek-v4-flash', 'deepseek-v4-pro'])) {
  3430. // DeepSeek 官方模型使用 DeepSeek API
  3431. $streamGenerator = $this->deepSeekStreamResponse($post_data);
  3432. } else if (in_array($model, $this->gpt_text_models)) {
  3433. // GPT-5.4 模型使用 TokenRouter API
  3434. $streamGenerator = $this->gpt54StreamResponse($post_data);
  3435. } else {
  3436. // 其他模型使用火山引擎API
  3437. $streamGenerator = $this->volcEngineChatCompletion($post_data);
  3438. }
  3439. // 验证返回值类型
  3440. if (!is_iterable($streamGenerator)) {
  3441. $errorType = is_object($streamGenerator) ? get_class($streamGenerator) : gettype($streamGenerator);
  3442. dLog('deepseek')->error('addChat流式生成器无效', [
  3443. 'generator_type' => $errorType,
  3444. 'model' => $model,
  3445. 'anime_id' => $anime_id
  3446. ]);
  3447. logDB('deepseek', 'error', 'addChat流式生成器类型错误', [
  3448. 'type' => $errorType,
  3449. 'model' => $model
  3450. ]);
  3451. yield [
  3452. 'type' => 'error',
  3453. 'script' => [],
  3454. 'episode' => [],
  3455. 'answer' => '',
  3456. 'reasoning' => '',
  3457. 'usage' => [],
  3458. 'error' => '接口返回数据异常,请重新请求'
  3459. ];
  3460. return;
  3461. }
  3462. // 处理流式输出
  3463. foreach ($streamGenerator as $chunk) {
  3464. if (isset($chunk['type'])) {
  3465. if ($chunk['type'] === 'done') {
  3466. // 最终结果
  3467. $fullContent = $chunk['full_content'];
  3468. $fullReasoningContent = $chunk['full_reasoning'];
  3469. $usage = isset($chunk['usage']) ? $chunk['usage'] : [];
  3470. } else {
  3471. // 逐块yield数据
  3472. yield $chunk;
  3473. }
  3474. }
  3475. }
  3476. } catch (\Exception $e) {
  3477. dLog('deepseek')->error('addChat流式处理异常: ' . $e->getMessage(), [
  3478. 'model' => $model,
  3479. 'anime_id' => $anime_id,
  3480. 'trace' => $e->getTraceAsString()
  3481. ]);
  3482. logDB('deepseek', 'error', 'addChat流式处理失败', [
  3483. 'error' => $e->getMessage(),
  3484. 'model' => $model
  3485. ]);
  3486. yield [
  3487. 'type' => 'error',
  3488. 'script' => [],
  3489. 'episode' => [],
  3490. 'answer' => '',
  3491. 'reasoning' => '',
  3492. 'usage' => [],
  3493. 'error' => '处理请求时发生异常: ' . $e->getMessage()
  3494. ];
  3495. return;
  3496. }
  3497. dLog('deepseek')->info('完整内容: '.$fullContent);
  3498. logDB('deepseek', 'info', '完整内容', ['content' => $fullContent, 'anime_id' => $anime_id]);
  3499. // 处理完整内容并返回最终结果
  3500. $script_arr = [];
  3501. if ($fullContent) {
  3502. $script_arr = handleScriptContent($fullContent);
  3503. dLog('deepseek')->info('解析内容', $script_arr);
  3504. logDB('deepseek', 'info', '解析内容', $script_arr);
  3505. }
  3506. if (empty($script_arr['roles'])) {
  3507. // 未生成剧本相关内容,保存对话记录并返回提示
  3508. dLog('deepseek')->info('未生成剧本相关的内容');
  3509. try {
  3510. DB::beginTransaction();
  3511. $now = date('Y-m-d H:i:s');
  3512. // 保存对话记录
  3513. $records = [
  3514. [
  3515. 'uid' => $uid,
  3516. 'anime_id' => $anime_id,
  3517. 'sequence' => 0,
  3518. 'role' => 'user',
  3519. 'content' => $prompt,
  3520. 'created_at' => $now,
  3521. 'updated_at' => $now
  3522. ],
  3523. [
  3524. 'uid' => $uid,
  3525. 'anime_id' => $anime_id,
  3526. 'sequence' => 0,
  3527. 'role' => 'assistant',
  3528. // 'content' => $fullContent,
  3529. 'content' => '未生成剧本相关的内容,请调整提示词再试',
  3530. 'created_at' => $now,
  3531. 'updated_at' => $now
  3532. ]
  3533. ];
  3534. DB::table('mp_anime_records')->insert($records);
  3535. DB::commit();
  3536. } catch (\Exception $e) {
  3537. DB::rollBack();
  3538. dLog('deepseek')->error('保存对话记录失败', ['error' => $e->getMessage()]);
  3539. }
  3540. yield [
  3541. 'type' => 'done',
  3542. 'script' => [],
  3543. 'episode' => [],
  3544. 'answer' => $fullContent,
  3545. 'reasoning' => $fullReasoningContent,
  3546. 'usage' => $usage,
  3547. 'error' => '未生成剧本相关的内容,请调整提示词再试'
  3548. ];
  3549. return;
  3550. }
  3551. // 如果需要生成原文内容,从script_arr中提取
  3552. if ($need_generate_content && isset($script_arr['content']) && $script_arr['content']) {
  3553. $content = $script_arr['content'];
  3554. dLog('deepseek')->info('从AI生成结果中提取原文内容', ['content_length' => strlen($content)]);
  3555. if (!$content) {
  3556. yield [
  3557. 'type' => 'done',
  3558. 'script' => [],
  3559. 'episode' => [],
  3560. 'answer' => $fullContent,
  3561. 'reasoning' => $fullReasoningContent,
  3562. 'usage' => $usage,
  3563. 'error' => '未生成剧本内容,请调整提示词再试'
  3564. ];
  3565. return;
  3566. }
  3567. }
  3568. // 替换美术风格
  3569. if ($mappedArtStyle !== '') {
  3570. $script_arr['art_style'] = $mappedArtStyle;
  3571. $fullContent = $this->replaceArtStyleSection($fullContent, $mappedArtStyle);
  3572. }
  3573. // 新建动漫
  3574. if ($user_anime_name == '新剧本策划') {
  3575. $anime_name = getProp($script_arr, 'script_name');
  3576. // if (!$anime_name) {
  3577. // // 未识别到剧本名,保存对话记录并返回提示
  3578. // dLog('deepseek')->info('未能识别到剧本名称');
  3579. // try {
  3580. // DB::beginTransaction();
  3581. // $now = date('Y-m-d H:i:s');
  3582. // // 保存对话记录
  3583. // $records = [
  3584. // [
  3585. // 'uid' => $uid,
  3586. // 'anime_id' => $anime_id,
  3587. // 'sequence' => 0,
  3588. // 'role' => 'user',
  3589. // 'content' => $prompt,
  3590. // 'created_at' => $now,
  3591. // 'updated_at' => $now
  3592. // ],
  3593. // [
  3594. // 'uid' => $uid,
  3595. // 'anime_id' => $anime_id,
  3596. // 'sequence' => 0,
  3597. // 'role' => 'assistant',
  3598. // 'content' => '未能生成剧本名称,请重试',
  3599. // 'created_at' => $now,
  3600. // 'updated_at' => $now
  3601. // ]
  3602. // ];
  3603. // DB::table('mp_anime_records')->insert($records);
  3604. // DB::commit();
  3605. // } catch (\Exception $e) {
  3606. // DB::rollBack();
  3607. // dLog('deepseek')->error('保存对话记录失败', ['error' => $e->getMessage()]);
  3608. // }
  3609. // yield [
  3610. // 'type' => 'done',
  3611. // 'script' => [],
  3612. // 'episode' => [],
  3613. // 'answer' => $fullContent,
  3614. // 'reasoning' => $fullReasoningContent,
  3615. // 'usage' => $usage,
  3616. // 'error' => '未能识别到剧本名称,请调整提示词再试'
  3617. // ];
  3618. // return;
  3619. // }
  3620. // 确认对话名称唯一性
  3621. if ($anime_name && DB::table('mp_animes')->where('anime_name', $anime_name)->value('id')) {
  3622. $anime_name = $anime_name . '_' . date('YmdHis');
  3623. }
  3624. }else {
  3625. $anime_name = $user_anime_name;
  3626. }
  3627. $table_data = [
  3628. 'user_id' => $uid,
  3629. 'anime_name' => $anime_name,
  3630. 'model' => $model,
  3631. 'intro' => isset($script_arr['intro']) ? $script_arr['intro'] : '',
  3632. 'highlights' => isset($script_arr['highlights']) ? $script_arr['highlights'] : '',
  3633. 'role_relationship' => isset($script_arr['role_relationship']) ? $script_arr['role_relationship'] : '',
  3634. 'core_contradiction' => isset($script_arr['core_contradiction']) ? $script_arr['core_contradiction'] : '',
  3635. 'roles' => isset($script_arr['roles']) && is_array($script_arr['roles']) ? json_encode($script_arr['roles'], 256) : '[]',
  3636. 'art_style' => isset($script_arr['art_style']) ? $script_arr['art_style'] : '',
  3637. 'art_style_type' => $input_art_style,
  3638. 'scenes' => isset($script_arr['scenes']) && is_array($script_arr['scenes']) ? json_encode($script_arr['scenes'], 256) : '[]',
  3639. 'status' => '解析完成',
  3640. 'content' => $content,
  3641. 'is_multi' => $is_multi,
  3642. 'ace_mode' => $ace_mode,
  3643. 'generate_status' => '待执行',
  3644. 'updated_at' => date('Y-m-d H:i:s')
  3645. ];
  3646. if ($table_data['content']) {
  3647. $chapters = $this->splitContent($table_data['content']);
  3648. $chapter_count = count($chapters);
  3649. if ($chapter_count > 0) {
  3650. $table_data['start_episode_sequence'] = 1;
  3651. $table_data['end_episode_sequence'] = $table_data['episode_num'] = $chapter_count;
  3652. }
  3653. }
  3654. // 如果有剧本ID则进行绑定
  3655. if ($script_id) {
  3656. $table_data['script_id'] = $script_id;
  3657. }
  3658. $single_episode = [];
  3659. try {
  3660. DB::beginTransaction();
  3661. $now = date('Y-m-d H:i:s');
  3662. // 更新动漫大纲
  3663. $boolen = DB::table('mp_animes')->where('id', $anime_id)->update($table_data);
  3664. if (!$boolen) {
  3665. dLog('deepseek')->info('动漫保存失败', $table_data);
  3666. Utils::throwError('20003:动漫保存失败');
  3667. }
  3668. // 保存对话记录
  3669. $records = [
  3670. [
  3671. 'uid' => $uid,
  3672. 'anime_id' => $anime_id,
  3673. 'sequence' => 0,
  3674. 'role' => 'user',
  3675. 'content' => $prompt,
  3676. 'created_at' => date('Y-m-d H:i:s'),
  3677. 'updated_at' => date('Y-m-d H:i:s')
  3678. ],
  3679. [
  3680. 'uid' => $uid,
  3681. 'anime_id' => $anime_id,
  3682. 'sequence' => 0,
  3683. 'role' => 'assistant',
  3684. 'content' => $fullContent,
  3685. 'created_at' => date('Y-m-d H:i:s'),
  3686. 'updated_at' => date('Y-m-d H:i:s')
  3687. ]
  3688. ];
  3689. // 保存对话记录
  3690. $boolen3 = DB::table('mp_anime_records')->insert($records);
  3691. if (!$boolen3) {
  3692. Utils::throwError('20003:对话记录保存失败');
  3693. }
  3694. if ($is_single) {
  3695. $episode_arr = $this->buildSingleEpisodeFromScript($script_arr, $fullContent, $anime_name);
  3696. if (empty($episode_arr['acts'])) {
  3697. Utils::throwError('20003:单剧集分镜内容解析失败,请确认输出格式');
  3698. }
  3699. $saveResult = $this->saveEpisodeVersionData(
  3700. $anime_id,
  3701. 1,
  3702. $episode_arr,
  3703. is_array(getProp($script_arr, 'roles')) ? getProp($script_arr, 'roles') : [],
  3704. is_array(getProp($script_arr, 'scenes')) ? getProp($script_arr, 'scenes') : [],
  3705. is_array(getProp($script_arr, 'props')) ? getProp($script_arr, 'props') : [],
  3706. null,
  3707. null,
  3708. false,
  3709. $content,
  3710. $now
  3711. );
  3712. $single_episode = $saveResult['episode'];
  3713. $episode_id = $saveResult['episode_id'];
  3714. $boolen5 = DB::table('mp_animes')->where('id', $anime_id)->update([
  3715. 'roles' => json_encode($saveResult['merged_roles'], 256),
  3716. 'scenes' => json_encode($saveResult['merged_scenes'], 256),
  3717. 'props' => json_encode($saveResult['merged_props'], 256),
  3718. 'updated_at' => $now
  3719. ]);
  3720. if ($boolen5 === false) {
  3721. Utils::throwError('20003:单剧集主表资源同步失败');
  3722. }
  3723. $table_data['roles'] = json_encode($saveResult['merged_roles'], 256);
  3724. $table_data['scenes'] = json_encode($saveResult['merged_scenes'], 256);
  3725. $table_data['props'] = json_encode($saveResult['merged_props'], 256);
  3726. $episode_record_content = '确认分镜大纲';
  3727. if ($content !== '') {
  3728. // $episode_record_content .= "\n\n###上传内容\n" . $content;
  3729. }
  3730. $episode_records = [
  3731. [
  3732. 'uid' => $uid,
  3733. 'anime_id' => $anime_id,
  3734. 'role' => 'user',
  3735. 'content' => $episode_record_content,
  3736. 'sequence' => 1,
  3737. 'episode_id' => $episode_id,
  3738. 'created_at' => $now,
  3739. 'updated_at' => $now
  3740. ],
  3741. [
  3742. 'uid' => $uid,
  3743. 'anime_id' => $anime_id,
  3744. 'role' => 'assistant',
  3745. 'content' => $fullContent,
  3746. // 'content' => '我已按照您的要求完成策划并将内容更新到您右侧的策划文档。',
  3747. 'sequence' => 1,
  3748. 'episode_id' => $episode_id,
  3749. 'created_at' => $now,
  3750. 'updated_at' => $now
  3751. ]
  3752. ];
  3753. $boolen4 = DB::table('mp_anime_records')->insert($episode_records);
  3754. if (!$boolen4) {
  3755. Utils::throwError('20003:单剧集分镜记录保存失败');
  3756. }
  3757. }
  3758. }catch (\Exception $e) {
  3759. DB::rollBack();
  3760. dLog('deepseek')->info('新建对话错误信息: '.$e->getMessage());
  3761. yield [
  3762. 'type' => 'done',
  3763. 'answer' => $fullContent,
  3764. 'reasoning' => $fullReasoningContent,
  3765. 'usage' => $usage,
  3766. 'error' => $e->getMessage(),
  3767. ];
  3768. return;
  3769. }
  3770. DB::commit();
  3771. dLog('deepseek')->info('保存完毕');
  3772. if ($is_single && !empty($single_episode)) {
  3773. // $this->batchSetGlobalRoleImg([
  3774. // 'anime_id' => $anime_id,
  3775. // ]);
  3776. // $this->batchSetGlobalSceneImg([
  3777. // 'anime_id' => $anime_id,
  3778. // ]);
  3779. // // 将全局角色和场景的task_id、task_status和url继承给当前分集
  3780. // $this->inheritGlobalImagesToEpisodes($anime_id, $episode_id);
  3781. }
  3782. $table_data['anime_id'] = $anime_id;
  3783. $table_data['roles'] = json_decode($table_data['roles'], true);
  3784. $table_data['scenes'] = json_decode($table_data['scenes'], true);
  3785. // $table_data['episodes'] = $script_arr['episodes'];
  3786. unset($table_data['created_at']);
  3787. unset($table_data['updated_at']);
  3788. unset($table_data['status']);
  3789. dLog('deepseek')->info('开始返回');
  3790. yield [
  3791. 'type' => 'done',
  3792. 'script' => $table_data,
  3793. 'episode' => $single_episode,
  3794. 'answer' => $fullContent,
  3795. 'reasoning' => $fullReasoningContent,
  3796. 'usage' => $usage
  3797. ];
  3798. }
  3799. public function reGenerateAnime($data) {
  3800. $uid = Site::getUid();
  3801. $file = getProp($data, 'file');
  3802. $content = getProp($data, 'content', '');
  3803. $bid = getProp($data, 'bid', 0);
  3804. $script_id = getProp($data, 'script_id', 0);
  3805. $anime_id = getProp($data, 'anime_id');
  3806. $anime = DB::table('mp_animes')->where('id', $anime_id)->first();
  3807. if (!$anime) {
  3808. Utils::throwError('20003:该对话不存在');
  3809. }
  3810. $user_anime_name = getProp($anime, 'anime_name');
  3811. $prompt = getProp($data, 'prompt');
  3812. // 处理文本模型
  3813. $model = getProp($data, 'model');
  3814. if (!$model) {
  3815. // 用户没有输入,从anime表获取
  3816. $model = getProp($anime, 'model');
  3817. if (!$model) {
  3818. // anime表也没有,使用默认值
  3819. $model = 'doubao-seed-2-0-mini-260215';
  3820. }
  3821. }
  3822. // 验证模型是否在可用模型表中
  3823. if (!DB::table('mp_text_models')->where('model', $model)->where('is_enabled', 1)->exists()) {
  3824. $model = 'doubao-seed-2-0-mini-260215';
  3825. }
  3826. $is_multi = getProp($anime, 'is_multi', 1);
  3827. $is_single = (int)$is_multi !== 1;
  3828. // 单剧集模式下,如果已经生成了剧集,则不允许继续调整大纲
  3829. if ($is_single) {
  3830. $episode_exists = DB::table('mp_anime_episodes')
  3831. ->where('anime_id', $anime_id)
  3832. ->where('sequence', 1)
  3833. ->exists();
  3834. if ($episode_exists) {
  3835. Utils::throwError('20003:单剧集模式下已生成剧集,不可继续调整大纲,请去策划剧集');
  3836. }
  3837. }
  3838. $question = getProp($data, 'question', $is_single ? "请根据实例格式完成单剧集剧本及分镜\n" : "请根据实例格式完成剧本大纲\n");
  3839. if ($prompt) {
  3840. $question .= "本次修改要求如下:\n{$prompt}";
  3841. }
  3842. // 提取文件内容
  3843. if ($file) {
  3844. $content = $this->extractFileContent($file);
  3845. if (!$content) {
  3846. Utils::throwError('20003:无法提取文件内容,请检查文件格式');
  3847. }
  3848. } elseif ($script_id) {
  3849. $content = $this->getContentByScriptId($script_id);
  3850. if (!$content) {
  3851. Utils::throwError('20003:无法获取剧本内容,请检查剧本');
  3852. }
  3853. } elseif ($bid) {
  3854. $content = $this->getContentByBid($bid);
  3855. if (!$content) {
  3856. Utils::throwError('20003:无法获取书籍内容,请检查书籍');
  3857. }
  3858. } elseif ($content) {
  3859. $content = filterContent($content);
  3860. }else {
  3861. $content = filterContent(getProp($anime, 'content'));
  3862. if (!$content && $prompt) { // 如果动漫记录中没有内容则根据提示词生成
  3863. $need_generate_content = true;
  3864. }
  3865. }
  3866. // 判断是否需要生成原文内容
  3867. $need_generate_content = isset($need_generate_content) && $need_generate_content;
  3868. // 如果需要生成原文内容,添加额外的要求
  3869. $content_generation_requirement = $need_generate_content
  3870. ? "\n5.如果用户只提供了创作要求而没有提供具体文档内容,你需要根据要求创作完整的原文内容(小说形式,包含对话和叙述),并将原文内容放在最后一个板块###原文内容中输出。原文内容应该详细、生动,如果用户没有明确生成的字数,则字数不少于1000字。"
  3871. : "";
  3872. // 美术风格
  3873. $input_art_style = getProp($anime, 'art_style');
  3874. if (!$input_art_style) {
  3875. $input_art_style = getProp($data, 'art_style');
  3876. }
  3877. $mappedArtStyle = $this->getArtStylePromptByInput($input_art_style);
  3878. // 美术风格
  3879. if (!$mappedArtStyle) {
  3880. $mappedArtStyle_prompt = '美术风格:根据剧本内容自定义美术风格';
  3881. }else {
  3882. if (strstr($mappedArtStyle, '基础画风风格词')) {
  3883. $mappedArtStyle_prompt = "美术风格强制设置如下(原样输出,不得更改任何内容):\n{$mappedArtStyle}\n";
  3884. }else {
  3885. $art_style_type = $input_art_style ? $input_art_style : getProp($anime, 'art_style_type');
  3886. $mappedArtStyle_prompt = "美术风格强制设置如下(原样输出,不得更改任何内容):\n基础画风风格词: {$art_style_type}\n{$mappedArtStyle}\n";
  3887. }
  3888. }
  3889. $timbres_content = "青年男:北京小爷、阳光青年、傲娇霸总、优柔公子、解说小明、青涩小生、低音沉郁、亲切青年
  3890. 青年女:柔美女友、魅力女友、爽快思思、高冷御姐、知性温婉、甜心小美、清新女声、甜美桃子、温柔文雅、甜美小源、贴心闺蜜
  3891. 中年男:胡子叔叔、油腻大叔、神秘法师、优柔帮主
  3892. 中年女:邻居阿姨
  3893. 老年男:幽默大爷
  3894. 老年女:婆婆
  3895. 萌娃:奶气萌娃";
  3896. $system_message = ['role'=>'system', 'content'=>$is_single
  3897. ? "你是一个专业的文档分析助手及资深编剧,请根据用户提供的文档内容输出单剧集完整策划稿(剧本大纲包括: <故事梗概><剧本亮点><人物关系><核心矛盾><主体列表><美术风格><场景列表><分镜剧本>,每个板块之间必须使用###分隔,#不能多也不能少)。单剧集模式下,必须一次性输出可直接保存的大纲与完整分镜,不允许只输出大纲。
  3898. 强制要求:
  3899. 1.你只负责与剧本相关的内容,如果用户输入与剧本无关的内容,请告知用户并拒绝继续回答,此为第一优先原则,不满足此原则就忽略其他要求并停止继续输出
  3900. 2.如果用户输入修改美术风格、修改画风这类内容,请告知用户不可修改美术风格,并拒绝继续回答
  3901. 3.以上两条要求对修改主体角色或场景不生效,允许用户调整主体角色说明和场景描述等
  3902. 4.用户如果要求你帮忙写一个故事或者剧本,此类要求不属于与剧本无关,请帮助用户完成请求{$content_generation_requirement}
  3903. 5.强制按照示例格式返回内容,不要更改格式或随意增加符号\n
  3904. 普通要求:
  3905. 1.剧本名(必须生成)和剧集标题必须与文档内容高度相关,(其中剧集序号是1)。
  3906. 2.<主体列表>中出现的“主体名”必须尽可能统一,严禁出现角色-科学家、角色-复仇者、角色-领导者等带有身份、职业、标签类的命名,只能使用服装差异(如校服装、卫衣装、西装)来区分同一角色,如角色-校服装等。
  3907. 2.1<主体列表>分以下三部分:主体描述、人物提示词和音色,格式必须为:主体名:主体描述{人物提示词}{{音色}}
  3908. 2.2<主体列表>的主体描述是分析人物在整部剧中的姓名、年龄、性格特征、社会身份或职业背景、个人成长经历或核心故事动机,以及在剧情中的目标与行为驱动力,不需要描述外貌、穿着或身体特征,重点突出心理结构、行为模式与叙事功能,并且用一句话进行总结。
  3909. 2.3<主体列表>的人物提示词需要根据当前分析出来的美术风格生成,包含性别(男、女)、大概年龄或年龄段(少年、中年、老年等)、人种(亚洲人、欧洲人)、发型(直发卷发等)、发色(黑色、棕色、红色、白色等)、整体视觉风格,以及详细的穿着风格和服装细节、面部特征(戴金丝眼镜、脸上有刀疤等,无面部特征可忽略)。描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合人物身份设定。该提示词(字数50-100字左右)在主体描述后用{}框起来。
  3910. 2.4<主体列表>的音色必须在以下音色中($timbres_content)选择,需要根据角色描述的年纪和性别选择,比如角色是青年男性就只能在北京小爷、阳光青年、傲娇霸总、优柔公子、解说小明、青涩小生、低音沉郁、亲切青年中选择,选好的音色放在描述的最后用{{}}框起来。
  3911. 3.<场景列表>与主体列表同理,需尽可能的对场景环境进行详细描述,必须严格注意不要出现任何角色和人物。
  3912. 3.1<场景列表>分以下两部分:场景描述和场景提示词,格式必须为:场景描述{场景提示词}
  3913. 3.2<场景列表>的场景描述是分析在整部剧中出现过的场景,场景描述尽量简洁贴合原文。
  3914. 3.3<场景列表>的场景提示词用于视觉呈现的场景环境描述,该场景需包含环境氛围、光线效果、空间布局、色彩基调、视觉风格等,描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合剧情设定(可参考以下示例的场景列表中使用{}框起来的提示词,字数150-200字,需带上提炼出来的美术风格)。该提示词在场景描述后用{}框起来。\n
  3915. 3.$mappedArtStyle_prompt\n\n
  3916. 4.<分镜剧本>要求包含“分幕标题”以及“分镜信息”:
  3917. 4.1“分幕标题”是根据章节中的场景进行拆分,类似于“第1幕 场景、时间及室内或室外”的信息,其中场景只能在所给出的场景列表中选(不带场景详细描述)。
  3918. 4.2“分镜信息”需同一场景内的对话内容完成分镜(每一幕分镜尽量不得低于5个),需包含“画面描述”、“构图设计”、“运镜调度”、“配音角色”、“台词内容”、“画面类型”、“尾帧描述”、“情感”、“性别”、“语速”、“音量”、“情感强度”、“音调”这几个部分,其中“画面描述”如果出现主体,只能选择上述主体列表中的一个,主体的名称也需要保持一致(不带主体具体描述)。
  3919. 4.3分镜序号用1、2、3显示,如分镜1、分镜2,并且分镜序号需连续(跨分幕中的分镜序号需连续,但跨剧集中的不需要连续,如第2集的第1幕分镜序号从1开始)。
  3920. 4.4“分镜信息”中的“台词内容”需满足以下要求: (1. 台词来源规则:- 台词内容必须逐字摘录自小说原文中的人物直接对话(带引号的内容)。- 不允许改写、总结、压缩、扩写或拼接原文对白。- 不允许生成原文中不存在的对话。2. 非对话过滤:- 旁白、心理描写、环境描写、动作描写等内容,禁止出现在台词内容中。3. 空台词规则:- 如果该分镜中没有人物对话,则“台词内容”必须返回“无”。- 严禁输出“无台词”、“没有”等任何替代文本。4. 禁止推测:- 不得根据语境补全人物可能说的话。)
  3921. 4.5“分镜信息”中的“配音角色”需对应本集中的<主体列表>中的一个(非对话内容则统一固定为“旁白”角色),不得自己生成。
  3922. 4.6“分镜信息”中的“场景”必须选择本集中的<场景列表>中的一个(不带详细表述)。
  3923. 4.7“分镜信息”中的“出镜角色”需对应本集中的<主体列表>中的一个或多个(不带详细描述),如果没有出镜角色则填无。
  3924. 4.8“分镜信息”中的“尾帧描述”需根据本镜和下一镜的剧本信息进行过渡生成,用于后续转视频进行衔接上下镜头的画面。
  3925. 4.9“分镜信息”中的“情感”需根据台词内容和剧情氛围分析得出,可选值为:开心、悲伤、生气、惊讶、恐惧、厌恶、激动、冷漠、中性、沮丧、撒娇、害羞、安慰鼓励、咆哮、温柔、自然讲述、情感电台、磁性、广告营销、气泡音、新闻播报、娱乐八卦,不要在情感可选词后加任何解释性的词语,比如(对自己)、(对别人)等,不要自定义情感,必须在可选值中选择。
  3926. 4.10“分镜信息”中的“性别”需根据配音角色的性别确定,可选值为:1(男)、2(女)、0(中性)。
  3927. 4.11“分镜信息”中的“语速”需根据台词内容和情感氛围确定,可选值范围为:-50到100之间的整数,100代表2.0倍速,-50代表0.5倍数,负值表示减慢,正值表示加快,0表示正常语速。
  3928. 4.12“分镜信息”中的“音量”需根据台词内容和情感氛围确定,可选值范围为:-50到100之间的整数,100代表2.0倍音量,-50代表0.5倍音量,负值表示降低音量,正值表示提高音量,0表示正常音量。
  3929. 4.13“分镜信息”中的“情感强度”需根据情感的强烈程度确定,可选值范围为:1到5之间的整数,默认情况下选择4。
  3930. 4.14“分镜信息”中的“音调”需根据台词内容和情感氛围确定,可选值范围为:-12到12之间的整数,负值表示降低音调,正值表示提高音调,0表示正常音调。\n
  3931. 示例如下:\n
  3932. ###剧本名:西昆仑
  3933. ###故事梗概
  3934. 内容梗概:古风厚涂的世界中,少女阿雪身中奇毒“问心刺”,气息奄奄。少年梁萧心急如焚,用奇珍“阴阳球”稳住阿雪性命,却发现必须解衣拔刺。阿雪羞涩难当,梁萧情急之下提出结拜兄妹,以化解尴尬。两人匆匆结拜后,梁萧小心翼翼地拔出“问心刺”,竟发现它是一根诡异的女子秀发。阿雪转危为安,两人之间,在生死与羞涩交织的瞬间,情愫暗生,却又被兄妹之名束缚,留下无尽遐想。
  3935. ###剧本亮点
  3936. 亮点1:绝境奇药,生死一线
  3937. 画面开场即是阿雪苍白无力的脸庞,梁萧眼中泪光闪烁,急促的呼吸声与心跳声烘托紧张气氛。特写梁萧将“阴阳球”塞入阿雪舌底,球体散发微光,阿雪气息从若有若无到逐渐平稳,生死逆转的瞬间,视觉与听觉冲击力十足。
  3938. 亮点2:结拜兄妹,化解尴尬
  3939. 阿雪因解衣拔刺而羞愤欲绝的特写,梁萧眼神中的挣扎与无奈。在紧迫的背景音下,梁萧急中生智,牵起阿雪颤抖的手,跪地匆匆结拜,阿雪眼神从惊惶到迷茫,最终一句“我……我愿意的”带着哭腔,将救命与羞耻的矛盾推向高潮,又以出人意料的方式解决。
  3940. 亮点3:纤发夺命,情愫暗涌
  3941. 拔刺过程的紧张特写,梁萧指尖小心翼翼地捏住细丝,缓慢抽出。当“问心刺”被完全拔出,特写它竟是一根细长、泛着寒光的女子秀发,诡异而令人震惊。阿雪苏醒后,两人眼神交汇,阿雪低头羞涩,梁萧眼中复杂的情绪,无声地传递出超越兄妹的微妙情愫。
  3942. ###人物关系
  3943. 阿雪与梁萧之间存在兄妹之情。
  3944. ###核心矛盾
  3945. 阿雪与梁萧之间存在兄妹之情,但因情愫暗生而产生微妙的矛盾。阿雪羞涩难当,梁萧情急之下提出结拜兄妹,以化解尴尬。两人匆匆结拜后,梁萧小心翼翼地拔出“问心刺”,竟发现它是一根诡异的女子秀发。阿雪转危为安,两人之间,在生死与羞涩交织的瞬间,情愫暗生,却又被兄妹之名束缚,留下无尽遐想。
  3946. ###主体列表
  3947. 阿雪:身中奇毒“问心刺”,气息奄奄,面色苍白,身体虚弱。{全景,正面拍摄,青年女性,亚洲人。
  3948. 黑色短发扎成利落的高马尾,五官精致且轮廓清晰。
  3949. 服饰穿搭:身着红白相间的专业田径运动背心与超短跑步裤,皮肤呈现健康的小麦色,四肢肌肉线条紧致有力。
  3950. 姿态:站立。}{{甜心小美}}
  3951. 梁萧:少年侠士,心系阿雪安危,沉着冷静,内心挣扎于情义之间。{8k超高清,电影级光效,大气透视,中国传统艺术美学,空灵光泽,深度景深,复杂精细的纹理,史诗级构图,电影胶片颗粒。
  3952. 全景,正面拍摄,青年女性,亚洲人。
  3953. 佩戴精致繁复的金丝凤冠,黑色长发整齐盘起。
  3954. 服饰穿搭:身着大红色正装嫁衣,上面绣有精细的金色凤凰纹样,质感厚重华贵。
  3955. 姿态:站立。}{{阳光青年}}
  3956. ###美术风格
  3957. 基础画风风格词:厚涂古风
  3958. 整体采用精致细腻的厚涂古风,色彩浓郁而富有层次感,注重光影对比,营造出古典而富有戏剧张力的氛围。色调以深沉的蓝绿、灰调为主,衬托阿雪病弱的苍白,同时用温暖的金色、琥珀色光晕突出“阴阳球”的生命力。拔刺与结拜场景则以柔和的暖色调为主,辅以人物脸颊的红晕,烘托羞涩与情愫暗生的微妙感。人物造型飘逸,服饰细节考究,背景虚实结合,既有古韵,又不失奇幻色彩。
  3959. ###场景列表
  3960. 林间疗伤地:幽静的古风林间空地,一棵高大古树下,是梁萧为阿雪疗伤的地点。环境清幽,光影斑驳,既有古韵又不失奇幻色彩。{影视质感。广角。
  3961. 宽敞的天然山洞内部,石壁上错落有致地镶嵌着发光的萤石,散发出冷色调的微光,照亮了整个空间。洞顶有钟乳石垂下。光影层次丰富,营造出一种宁静而神圣的氛围。
  3962. 无人物。}
  3963. ###分镜剧本
  3964. ##第1幕:徐家老旧厨房 白天 室内
  3965. 分镜1
  3966. 画面描述:许芸-校服装站在厨房门口,书包背在肩上,徐母手持扫帚,正对着许芸的背部猛烈挥打。卞大伟站在锅炉后方,双手抱胸,阴沉的目光落在许芸身上。
  3967. 场景:徐家老旧厨房
  3968. 构图设计:全景,低角度仰视
  3969. 运镜调度:手持拍摄
  3970. 配音角色:旁白
  3971. 出镜角色:许芸-校服装、徐母
  3972. 台词内容:我一到家,徐母的扫帚就落在我身上,嘴里一个劲地骂我,
  3973. 画面类型:普通画面
  3974. 尾帧描述:手持拍摄微晃镜头,中年妇女猛力挥动扫帚击打少女背部,少女身体因冲击微颤,阴影中的肥胖男子目光冷冷跟随。
  3975. 分镜2
  3976. 画面描述:徐母面容扭曲,嘴唇快速开合,扫帚挥舞在空中。
  3977. 场景:徐家老旧厨房
  3978. 构图设计:近景,徐母面部特写
  3979. 运镜调度:固定镜头
  3980. 配音角色:徐母
  3981. 出镜角色:徐母
  3982. 台词内容:问我夜不归宿死哪儿去了。
  3983. 画面类型:对口型
  3984. 尾帧描述:固定镜头,中年妇女面部肌肉剧烈抖动,嘴唇快速开合进行辱骂,背景中的扫帚在空中上下挥动。
  3985. ##第2幕:徐家简陋客厅 黄昏 室内
  3986. 分镜3
  3987. 画面描述:客厅窗户透出昏黄的夕阳余晖,空荡荡的客厅中央只有几把木凳和一张方桌,显得异常安静。
  3988. 场景:徐家简陋客厅
  3989. 构图设计:全景,景深虚化
  3990. 运镜调度:固定镜头
  3991. 配音角色:旁白
  3992. 出镜角色:无
  3993. 台词内容:可我没想到,半个月后,徐母居然再一次外出,
  3994. 画面类型:普通画面
  3995. 尾帧描述:固定镜头。昏黄的夕阳余晖在地面上缓慢移动,空气中的浮尘在光束中跳动,室内维持着死寂的安静。
  3996. 分镜4
  3997. 画面描述:一张泛黄的火车票特写,上面显示着隔壁市的站名和日期。
  3998. 场景:徐家简陋客厅
  3999. 构图设计:特写,火车票
  4000. 运镜调度:固定镜头
  4001. 配音角色:旁白
  4002. 出镜角色:无
  4003. 台词内容:这次是去隔壁市看望她生病的好姐妹。
  4004. 画面类型:普通画面
  4005. 尾帧描述:固定镜头。光影在火车票表面微微晃动,强调其陈旧的纸张纹理与关键的行程信息。\n
  4006. "
  4007. : "你是一个专业的文档分析助手及资深编剧,请根据用户提供的文档内容及下面提供的示例格式完成剧本大纲(需通过以下几个板块进行回复: <故事梗概><剧本亮点><人物关系><核心矛盾><主体列表><美术风格><场景列表>,每个板块之间必须使用###分隔,#不能多也不能少;同时板块之间需满足以下要求:\n
  4008. 强制要求:\n
  4009. 1.你只负责与剧本相关的内容,如果用户输入与剧本无关的内容,请告知用户并拒绝继续回答,此为第一优先原则,不满足此原则就忽略其他要求并停止继续输出
  4010. 2.如果用户输入修改美术风格、修改画风这类内容,请告知用户不可修改美术风格,并拒绝继续回答
  4011. 3.以上两条要求对修改主体角色或场景不生效,允许用户调整主体角色说明和场景描述等
  4012. 4.用户如果要求你帮忙写一个故事或者剧本,此类要求不属于与剧本无关,请帮助用户完成请求{$content_generation_requirement}
  4013. 5.强制按照示例格式返回内容,不要更改格式或随意增加符号\n
  4014. 普通要求:\n
  4015. 1.剧本名(必须生成)必须与文档内容高度相关
  4016. 2.<主体列表>中出现的“主体名”必须尽可能统一,严禁出现角色-科学家、角色-复仇者、角色-领导者等带有身份、职业、标签类的命名,只能使用服装差异(如校服装、卫衣装、西装)来区分同一角色,如角色-校服装等。
  4017. 2.1<主体列表>分以下三部分:主体描述、人物提示词和音色,格式必须为:主体名:主体描述{人物提示词}{{音色}}
  4018. 2.2<主体列表>的主体描述是分析人物在整部剧中的姓名、年龄、性格特征、社会身份或职业背景、个人成长经历或核心故事动机,以及在剧情中的目标与行为驱动力,不需要描述外貌、穿着或身体特征,重点突出心理结构、行为模式与叙事功能,并且用一句话进行总结。
  4019. 2.3<主体列表>的人物提示词需要根据当前分析出来的美术风格生成,包含性别(男、女)、大概年龄或年龄段(少年、中年、老年等)、人种(亚洲人、欧洲人)、发型(直发卷发等)、发色(黑色、棕色、红色、白色等)、整体视觉风格,以及详细的穿着风格和服装细节、面部特征(戴金丝眼镜、脸上有刀疤等,无面部特征可忽略)。描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合人物身份设定。该提示词(字数50-100字左右)在主体描述后用{}框起来。
  4020. 2.4<主体列表>的音色必须在以下音色中($timbres_content)选择,需要根据角色描述的年纪和性别选择,比如角色是青年男性就只能在北京小爷、阳光青年、傲娇霸总、优柔公子、解说小明、青涩小生、低音沉郁、亲切青年中选择,选好的音色放在描述的最后用{{}}框起来。
  4021. 3.<场景列表>与主体列表同理,需尽可能的对场景环境进行详细描述,必须严格注意不要出现任何角色和人物。
  4022. 3.1<场景列表>分以下两部分:场景描述和场景提示词,格式必须为:场景描述{场景提示词}
  4023. 3.2<场景列表>的场景描述是分析在整部剧中出现过的场景,场景描述尽量简洁贴合原文。
  4024. 3.3<场景列表>的场景提示词用于视觉呈现的场景环境描述,该场景需包含环境氛围、光线效果、空间布局、色彩基调、视觉风格等,描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合剧情设定(可参考以下示例的场景列表中使用{}框起来的提示词,字数150-200字,需带上提炼出来的美术风格)。该提示词在场景描述后用{}框起来。\n
  4025. 4.$mappedArtStyle_prompt\n\n
  4026. 示例如下:\n
  4027. ###剧本名:西昆仑
  4028. ###故事梗概
  4029. 内容梗概:古风厚涂的世界中,少女阿雪身中奇毒“问心刺”,气息奄奄。少年梁萧心急如焚,用奇珍“阴阳球”稳住阿雪性命,却发现必须解衣拔刺。阿雪羞涩难当,梁萧情急之下提出结拜兄妹,以化解尴尬。两人匆匆结拜后,梁萧小心翼翼地拔出“问心刺”,竟发现它是一根诡异的女子秀发。阿雪转危为安,两人之间,在生死与羞涩交织的瞬间,情愫暗生,却又被兄妹之名束缚,留下无尽遐想。
  4030. ###剧本亮点
  4031. 亮点1:绝境奇药,生死一线
  4032. 画面开场即是阿雪苍白无力的脸庞,梁萧眼中泪光闪烁,急促的呼吸声与心跳声烘托紧张气氛。特写梁萧将“阴阳球”塞入阿雪舌底,球体散发微光,阿雪气息从若有若无到逐渐平稳,生死逆转的瞬间,视觉与听觉冲击力十足。
  4033. 亮点2:结拜兄妹,化解尴尬
  4034. 阿雪因解衣拔刺而羞愤欲绝的特写,梁萧眼神中的挣扎与无奈。在紧迫的背景音下,梁萧急中生智,牵起阿雪颤抖的手,跪地匆匆结拜,阿雪眼神从惊惶到迷茫,最终一句“我……我愿意的”带着哭腔,将救命与羞耻的矛盾推向高潮,又以出人意料的方式解决。
  4035. 亮点3:纤发夺命,情愫暗涌
  4036. 拔刺过程的紧张特写,梁萧指尖小心翼翼地捏住细丝,缓慢抽出。当“问心刺”被完全拔出,特写它竟是一根细长、泛着寒光的女子秀发,诡异而令人震惊。阿雪苏醒后,两人眼神交汇,阿雪低头羞涩,梁萧眼中复杂的情绪,无声地传递出超越兄妹的微妙情愫。
  4037. ###人物关系
  4038. 阿雪与梁萧之间存在兄妹之情。
  4039. ###核心矛盾
  4040. 阿雪与梁萧之间存在兄妹之情,但因情愫暗生而产生微妙的矛盾。阿雪羞涩难当,梁萧情急之下提出结拜兄妹,以化解尴尬。两人匆匆结拜后,梁萧小心翼翼地拔出“问心刺”,竟发现它是一根诡异的女子秀发。阿雪转危为安,两人之间,在生死与羞涩交织的瞬间,情愫暗生,却又被兄妹之名束缚,留下无尽遐想。
  4041. ###主体列表
  4042. 阿雪:身中奇毒“问心刺”,气息奄奄,面色苍白,身体虚弱。{全景,正面拍摄,青年女性,亚洲人。
  4043. 黑色短发扎成利落的高马尾,五官精致且轮廓清晰。
  4044. 服饰穿搭:身着红白相间的专业田径运动背心与超短跑步裤,皮肤呈现健康的小麦色,四肢肌肉线条紧致有力。
  4045. 姿态:站立。}{{甜心小美}}
  4046. 梁萧:少年侠士,心系阿雪安危,沉着冷静,内心挣扎于情义之间。{8k超高清,电影级光效,大气透视,中国传统艺术美学,空灵光泽,深度景深,复杂精细的纹理,史诗级构图,电影胶片颗粒。
  4047. 全景,正面拍摄,青年女性,亚洲人。
  4048. 佩戴精致繁复的金丝凤冠,黑色长发整齐盘起。
  4049. 服饰穿搭:身着大红色正装嫁衣,上面绣有精细的金色凤凰纹样,质感厚重华贵。
  4050. 姿态:站立。}{{阳光青年}}
  4051. ###美术风格
  4052. 基础画风风格词:厚涂古风
  4053. 整体采用精致细腻的厚涂古风,色彩浓郁而富有层次感,注重光影对比,营造出古典而富有戏剧张力的氛围。色调以深沉的蓝绿、灰调为主,衬托阿雪病弱的苍白,同时用温暖的金色、琥珀色光晕突出“阴阳球”的生命力。拔刺与结拜场景则以柔和的暖色调为主,辅以人物脸颊的红晕,烘托羞涩与情愫暗生的微妙感。人物造型飘逸,服饰细节考究,背景虚实结合,既有古韵,又不失奇幻色彩。
  4054. ###场景列表
  4055. 林间疗伤地:幽静的古风林间空地,一棵高大古树下,是梁萧为阿雪疗伤的地点。环境清幽,光影斑驳,既有古韵又不失奇幻色彩。{影视质感。广角。
  4056. 宽敞的天然山洞内部,石壁上错落有致地镶嵌着发光的萤石,散发出冷色调的微光,照亮了整个空间。洞顶有钟乳石垂下。光影层次丰富,营造出一种宁静而神圣的氛围。
  4057. 无人物。}"];
  4058. // 如果提示词$prompt有类似于改成N集或调整成N集这类的文字信息,则提取出N这个集数
  4059. $episode_count = $this->extractEpisodeNumber($prompt);
  4060. if ((int)getProp($anime, 'is_multi') !== 1) {
  4061. yield [
  4062. 'type' => 'done',
  4063. 'answer' => '当前剧本是单剧集,不支持修改剧集数',
  4064. 'reasoning' => '',
  4065. 'usage' => []
  4066. ];
  4067. return;
  4068. }
  4069. if ($episode_count) {
  4070. $question = "请根据实例格式完成{$episode_count}集剧本大纲";
  4071. $system_message = [
  4072. 'role' => 'system',
  4073. 'content' => "你是一个专业的文档分析助手及资深编剧,请根据用户提供的文档内容及下面提供的示例格式完成剧本大纲(需通过以下几个板块进行回复: <故事梗概><剧本亮点><人物关系><核心矛盾><主体列表><美术风格><场景列表><分集详细内容>,每个板块之间必须使用###分隔,#不能多也不能少;同时板块之间需满足以下要求:\n
  4074. 强制要求:\n
  4075. 1.你只负责与剧本相关的内容,如果用户输入与剧本无关的内容,请告知用户并拒绝继续回答,此为第一优先原则,不满足此原则就忽略其他要求并停止继续输出
  4076. 2.如果用户输入修改美术风格、修改画风这类内容,请告知用户不可修改美术风格,并拒绝继续回答
  4077. 3.以上两条要求对修改主体角色或场景不生效,允许用户调整主体角色说明和场景描述等
  4078. 4.用户如果要求你帮忙写一个故事或者剧本,此类要求不属于与剧本无关,请帮助用户完成请求{$content_generation_requirement}
  4079. 5.强制按照示例格式返回内容,不要更改格式或随意增加符号\n
  4080. 普通要求:\n
  4081. 2.<主体列表>中出现的“主体名”必须尽可能统一,严禁出现角色-科学家、角色-复仇者、角色-领导者等带有身份、职业、标签类的命名,只能使用服装差异(如校服装、卫衣装、西装)来区分同一角色,如角色-校服装等。
  4082. 2.1<主体列表>分以下三部分:主体描述、人物提示词和音色,格式必须为:主体名:主体描述{人物提示词}{{音色}}
  4083. 2.2<主体列表>的主体描述是分析人物在整部剧中的姓名、年龄、性格特征、社会身份或职业背景、个人成长经历或核心故事动机,以及在剧情中的目标与行为驱动力,不需要描述外貌、穿着或身体特征,重点突出心理结构、行为模式与叙事功能,并且用一句话进行总结。
  4084. 2.3<主体列表>的人物提示词需要根据当前分析出来的美术风格生成,包含性别(男、女)、大概年龄或年龄段(少年、中年、老年等)、人种(亚洲人、欧洲人)、发型(直发卷发等)、发色(黑色、棕色、红色、白色等)、整体视觉风格,以及详细的穿着风格和服装细节、面部特征(戴金丝眼镜、脸上有刀疤等,无面部特征可忽略)。描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合人物身份设定。该提示词(字数50-100字左右)在主体描述后用{}框起来。
  4085. 2.4<主体列表>的音色必须在以下音色中($timbres_content)选择,需要根据角色描述的年纪和性别选择,比如角色是青年男性就只能在北京小爷、阳光青年、傲娇霸总、优柔公子、解说小明、青涩小生、低音沉郁、亲切青年中选择,选好的音色放在描述的最后用{{}}框起来。
  4086. 3.<场景列表>与主体列表同理,需尽可能的对场景环境进行详细描述,必须严格注意不要出现任何角色和人物。
  4087. 3.1<场景列表>分以下两部分:场景描述和场景提示词,格式必须为:场景描述{场景提示词},注意不要出现{{}}这样的双大括号。
  4088. 3.2<场景列表>的场景描述是分析在整部剧中出现过的场景,场景描述尽量简洁贴合原文。
  4089. 3.3<场景列表>的场景提示词用于视觉呈现的场景环境描述,该场景需包含环境氛围、光线效果、空间布局、色彩基调、视觉风格等,描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合剧情设定(可参考以下示例的场景列表中使用{}框起来的提示词,字数150-200字,需带上提炼出来的美术风格)。该提示词在场景描述后用{}框起来。\n
  4090. 4.<分集详细内容>中每一集的内容需尽可能的详细,保留对话内容(使用原文内容,不得自己生成),一般每集字数尽量不低于500字,每集结尾需以##第N章 作为章节标题\n\n
  4091. 示例如下:\n
  4092. ###剧本名:西昆仑
  4093. ###故事梗概
  4094. 内容梗概:古风厚涂的世界中,少女阿雪身中奇毒“问心刺”,气息奄奄。少年梁萧心急如焚,用奇珍“阴阳球”稳住阿雪性命,却发现必须解衣拔刺。阿雪羞涩难当,梁萧情急之下提出结拜兄妹,以化解尴尬。两人匆匆结拜后,梁萧小心翼翼地拔出“问心刺”,竟发现它是一根诡异的女子秀发。阿雪转危为安,两人之间,在生死与羞涩交织的瞬间,情愫暗生,却又被兄妹之名束缚,留下无尽遐想。
  4095. ###剧本亮点
  4096. 亮点1:绝境奇药,生死一线
  4097. 画面开场即是阿雪苍白无力的脸庞,梁萧眼中泪光闪烁,急促的呼吸声与心跳声烘托紧张气氛。特写梁萧将“阴阳球”塞入阿雪舌底,球体散发微光,阿雪气息从若有若无到逐渐平稳,生死逆转的瞬间,视觉与听觉冲击力十足。
  4098. 亮点2:结拜兄妹,化解尴尬
  4099. 阿雪因解衣拔刺而羞愤欲绝的特写,梁萧眼神中的挣扎与无奈。在紧迫的背景音下,梁萧急中生智,牵起阿雪颤抖的手,跪地匆匆结拜,阿雪眼神从惊惶到迷茫,最终一句“我……我愿意的”带着哭腔,将救命与羞耻的矛盾推向高潮,又以出人意料的方式解决。
  4100. 亮点3:纤发夺命,情愫暗涌
  4101. 拔刺过程的紧张特写,梁萧指尖小心翼翼地捏住细丝,缓慢抽出。当“问心刺”被完全拔出,特写它竟是一根细长、泛着寒光的女子秀发,诡异而令人震惊。阿雪苏醒后,两人眼神交汇,阿雪低头羞涩,梁萧眼中复杂的情绪,无声地传递出超越兄妹的微妙情愫。
  4102. ###人物关系
  4103. 阿雪与梁萧之间存在兄妹之情。
  4104. ###核心矛盾
  4105. 阿雪与梁萧之间存在兄妹之情,但因情愫暗生而产生微妙的矛盾。阿雪羞涩难当,梁萧情急之下提出结拜兄妹,以化解尴尬。两人匆匆结拜后,梁萧小心翼翼地拔出“问心刺”,竟发现它是一根诡异的女子秀发。阿雪转危为安,两人之间,在生死与羞涩交织的瞬间,情愫暗生,却又被兄妹之名束缚,留下无尽遐想。
  4106. ###主体列表
  4107. 阿雪:身中奇毒“问心刺”,气息奄奄,面色苍白,身体虚弱。{全景,正面拍摄,青年女性,亚洲人。
  4108. 黑色短发扎成利落的高马尾,五官精致且轮廓清晰。
  4109. 服饰穿搭:身着红白相间的专业田径运动背心与超短跑步裤,皮肤呈现健康的小麦色,四肢肌肉线条紧致有力。
  4110. 姿态:站立。}{{甜心小美}}
  4111. 梁萧:少年侠士,心系阿雪安危,沉着冷静,内心挣扎于情义之间。{8k超高清,电影级光效,大气透视,中国传统艺术美学,空灵光泽,深度景深,复杂精细的纹理,史诗级构图,电影胶片颗粒。
  4112. 全景,正面拍摄,青年女性,亚洲人。
  4113. 佩戴精致繁复的金丝凤冠,黑色长发整齐盘起。
  4114. 服饰穿搭:身着大红色正装嫁衣,上面绣有精细的金色凤凰纹样,质感厚重华贵。
  4115. 姿态:站立。}{{阳光青年}}
  4116. ###美术风格
  4117. 基础画风风格词:厚涂古风
  4118. 整体采用精致细腻的厚涂古风,色彩浓郁而富有层次感,注重光影对比,营造出古典而富有戏剧张力的氛围。色调以深沉的蓝绿、灰调为主,衬托阿雪病弱的苍白,同时用温暖的金色、琥珀色光晕突出“阴阳球”的生命力。拔刺与结拜场景则以柔和的暖色调为主,辅以人物脸颊的红晕,烘托羞涩与情愫暗生的微妙感。人物造型飘逸,服饰细节考究,背景虚实结合,既有古韵,又不失奇幻色彩。
  4119. ###场景列表
  4120. 林间疗伤地:幽静的古风林间空地,一棵高大古树下,是梁萧为阿雪疗伤的地点。环境清幽,光影斑驳,既有古韵又不失奇幻色彩。{影视质感。广角。
  4121. 宽敞的天然山洞内部,石壁上错落有致地镶嵌着发光的萤石,散发出冷色调的微光,照亮了整个空间。洞顶有钟乳石垂下。光影层次丰富,营造出一种宁静而神圣的氛围。
  4122. 无人物。}
  4123. ###分集详细内容
  4124. ##第1章 重生
  4125. 我重生了。
  4126. 源于一个男人偏执的暗恋。
  4127. ##第2章 相救
  4128. 我一到家,徐母的扫帚就落在我身上,嘴里一个劲地骂我,问我夜不归宿死哪儿去了。
  4129. 我眼睛扫向站在锅炉后的卞大伟。"
  4130. ];
  4131. // 构建消息
  4132. $messages = [
  4133. $system_message,
  4134. [
  4135. 'role' => 'user',
  4136. 'content' => "以下是文档内容:
  4137. {$content}
  4138. 用户问题:
  4139. {$question}"
  4140. ]
  4141. ];
  4142. }else {
  4143. if (strstr($prompt, '重新生成') || strstr($prompt, '再生成')) {
  4144. // 构建消息
  4145. $messages = [
  4146. $system_message,
  4147. [
  4148. 'role' => 'user',
  4149. 'content' => "以下是文档内容:
  4150. {$content}
  4151. 用户问题:
  4152. {$question}"
  4153. ]
  4154. ];
  4155. }else {
  4156. $messages = DB::table('mp_anime_records')->where('anime_id', $anime_id)->where('sequence', 0)
  4157. ->orderBy('id')->select('role', 'content')->get()->map(function ($value) {
  4158. return (array)$value;
  4159. })->toArray();
  4160. array_unshift($messages, $system_message);
  4161. $messages[] = [
  4162. 'role' => 'user',
  4163. 'content' => $prompt
  4164. ];
  4165. }
  4166. }
  4167. // 模型兼容性处理:deepseek-chat 和 deepseek-reasoner 映射到 deepseek-v4-flash
  4168. $thinkingMode = getProp($data, 'thinking', 'disabled'); // 默认非思考模式
  4169. $reasoningEffort = getProp($data, 'reasoning_effort', 'high'); // 默认思考强度
  4170. if ($model === 'deepseek-chat') {
  4171. // deepseek-chat 映射到 deepseek-v4-flash,非思考模式
  4172. $model = 'deepseek-v4-flash';
  4173. $thinkingMode = 'disabled';
  4174. } elseif ($model === 'deepseek-reasoner') {
  4175. // deepseek-reasoner 映射到 deepseek-v4-flash,思考模式
  4176. $model = 'deepseek-v4-flash';
  4177. $thinkingMode = 'enabled';
  4178. }
  4179. $post_data = [
  4180. 'model' => $model,
  4181. 'messages' => $messages,
  4182. // 'max_tokens' => 8192,
  4183. 'temperature' => 0.7,
  4184. 'frequency_penalty' => 0,
  4185. 'presence_penalty' => 0,
  4186. 'response_format' => ['type' => 'text'],
  4187. 'thinking' => ['type' => $thinkingMode],
  4188. 'stream' => true // 启用流式输出
  4189. ];
  4190. if ($thinkingMode == 'enabled') $post_data['reasoning_effort'] = $reasoningEffort;
  4191. // 根据模型类型选择调用方法
  4192. $fullContent = '';
  4193. $fullReasoningContent = '';
  4194. $usage = [];
  4195. // dd($post_data);
  4196. // dLog('deepseek')->info('请求参数: '.json_encode($post_data, 256));
  4197. try {
  4198. if (in_array($model, ['deepseek-reasoner', 'deepseek-chat', 'deepseek-v4-flash', 'deepseek-v4-pro'])) {
  4199. $streamGenerator = $this->deepSeekStreamResponse($post_data);
  4200. } else if (in_array($model, $this->gpt_text_models)) {
  4201. // GPT-5.4 模型使用 TokenRouter API
  4202. $streamGenerator = $this->gpt54StreamResponse($post_data);
  4203. } else {
  4204. $streamGenerator = $this->volcEngineChatCompletion($post_data);
  4205. }
  4206. // 验证返回值类型
  4207. if (!is_iterable($streamGenerator)) {
  4208. $errorType = is_object($streamGenerator) ? get_class($streamGenerator) : gettype($streamGenerator);
  4209. dLog('deepseek')->error('方法名流式生成器无效', [
  4210. 'generator_type' => $errorType,
  4211. 'model' => $model,
  4212. // 添加其他上下文信息
  4213. ]);
  4214. logDB('deepseek', 'error', '方法名流式生成器类型错误', [
  4215. 'type' => $errorType,
  4216. 'model' => $model
  4217. ]);
  4218. yield [
  4219. 'type' => 'error',
  4220. // 根据方法返回相应的空数据结构
  4221. 'answer' => '',
  4222. 'reasoning' => '',
  4223. 'usage' => [],
  4224. 'error' => '接口返回数据异常,请重新请求'
  4225. ];
  4226. return;
  4227. }
  4228. // 处理流式输出
  4229. foreach ($streamGenerator as $chunk) {
  4230. if (isset($chunk['type'])) {
  4231. if ($chunk['type'] === 'done') {
  4232. $fullContent = $chunk['full_content'];
  4233. $fullReasoningContent = $chunk['full_reasoning'];
  4234. $usage = isset($chunk['usage']) ? $chunk['usage'] : [];
  4235. } else {
  4236. yield $chunk;
  4237. }
  4238. }
  4239. }
  4240. } catch (\Exception $e) {
  4241. dLog('deepseek')->error('方法名流式处理异常: ' . $e->getMessage(), [
  4242. 'model' => $model,
  4243. 'trace' => $e->getTraceAsString()
  4244. ]);
  4245. logDB('deepseek', 'error', '方法名流式处理失败', [
  4246. 'error' => $e->getMessage(),
  4247. 'model' => $model
  4248. ]);
  4249. yield [
  4250. 'type' => 'error',
  4251. // 根据方法返回相应的空数据结构
  4252. 'answer' => '',
  4253. 'reasoning' => '',
  4254. 'usage' => [],
  4255. 'error' => '处理请求时发生异常: ' . $e->getMessage()
  4256. ];
  4257. return;
  4258. }
  4259. dLog('deepseek')->info('完整内容: '.$fullContent);
  4260. logDB('deepseek', 'info', '完整内容', ['content' => $fullContent, 'anime_id' => $anime_id]);
  4261. // 处理完整内容并返回最终结果
  4262. $script_arr = [];
  4263. if ($fullContent) {
  4264. $script_arr = handleScriptContent($fullContent);
  4265. dLog('deepseek')->info('解析内容', $script_arr);
  4266. logDB('deepseek', 'info', '解析内容', $script_arr);
  4267. }
  4268. if (empty($script_arr['roles'])) {
  4269. // 未生成剧本相关内容,保存对话记录并返回提示
  4270. dLog('deepseek')->info('未生成剧本相关的内容');
  4271. try {
  4272. $now = date('Y-m-d H:i:s');
  4273. // 保存对话记录
  4274. $records = [
  4275. [
  4276. 'uid' => $uid,
  4277. 'anime_id' => $anime_id,
  4278. 'sequence' => 0,
  4279. 'role' => 'user',
  4280. 'content' => $prompt,
  4281. 'created_at' => $now,
  4282. 'updated_at' => $now
  4283. ],
  4284. [
  4285. 'uid' => $uid,
  4286. 'anime_id' => $anime_id,
  4287. 'sequence' => 0,
  4288. 'role' => 'assistant',
  4289. 'content' => '未生成剧本相关的内容,请调整提示词后重试',
  4290. 'created_at' => $now,
  4291. 'updated_at' => $now
  4292. ]
  4293. ];
  4294. DB::table('mp_anime_records')->insert($records);
  4295. } catch (\Exception $e) {
  4296. dLog('deepseek')->error('保存对话记录失败', ['error' => $e->getMessage()]);
  4297. }
  4298. yield [
  4299. 'type' => 'done',
  4300. 'script' => [],
  4301. 'episode' => [],
  4302. 'answer' => $fullContent,
  4303. 'reasoning' => $fullReasoningContent,
  4304. 'usage' => $usage,
  4305. 'error' => '未生成剧本相关的内容,请调整提示词再试'
  4306. ];
  4307. return;
  4308. }
  4309. // 替换美术风格
  4310. if ($mappedArtStyle !== '') {
  4311. $script_arr['art_style'] = $mappedArtStyle;
  4312. $fullContent = $this->replaceArtStyleSection($fullContent, $mappedArtStyle);
  4313. }
  4314. if ($user_anime_name != '新剧本策划') {
  4315. $anime_name = isset($script_arr['script_name']) ? $script_arr['script_name'] : '';
  4316. // 确认对话名称唯一性
  4317. if ($anime_name && $id = DB::table('mp_animes')->where('anime_name', $anime_name)->value('id')) {
  4318. if ($id != $anime_id) $anime_name = $anime_name . '_' . date('YmdHis');
  4319. }
  4320. }else {
  4321. $anime_name = $user_anime_name;
  4322. }
  4323. $table_data = [
  4324. 'user_id' => $uid,
  4325. 'model' => $model,
  4326. 'intro' => isset($script_arr['intro']) ? $script_arr['intro'] : '',
  4327. 'highlights' => isset($script_arr['highlights']) ? $script_arr['highlights'] : '',
  4328. 'role_relationship' => isset($script_arr['role_relationship']) ? $script_arr['role_relationship'] : '',
  4329. 'core_contradiction' => isset($script_arr['core_contradiction']) ? $script_arr['core_contradiction'] : '',
  4330. 'roles' => isset($script_arr['roles']) && is_array($script_arr['roles']) ? json_encode($script_arr['roles'], 256) : '[]',
  4331. 'art_style' => isset($script_arr['art_style']) ? $script_arr['art_style'] : '',
  4332. 'art_style_type' => $input_art_style,
  4333. 'scenes' => isset($script_arr['scenes']) && is_array($script_arr['scenes']) ? json_encode($script_arr['scenes'], 256) : '[]',
  4334. 'status' => '解析完成',
  4335. 'generate_status' => '待执行',
  4336. 'updated_at' => date('Y-m-d H:i:s')
  4337. ];
  4338. if ($anime_name) $table_data['anime_name'] = $anime_name;
  4339. // 如果是修改集数,则将content内容更新(会改变原文)
  4340. if (isset($episode_count) && $episode_count > 0) {
  4341. if (isset($script_arr['content']) && $script_arr['content']) $table_data['content'] = $script_arr['content'];
  4342. $table_data['start_episode_sequence'] = 1;
  4343. $table_data['end_episode_sequence'] = $table_data['episode_num'] = $episode_count;
  4344. }else {
  4345. // 如果需要生成原文内容,从script_arr中提取
  4346. if ($need_generate_content && isset($script_arr['content']) && $script_arr['content']) {
  4347. $table_data['content'] = $script_arr['content'];
  4348. dLog('deepseek')->info('从AI生成结果中提取原文内容', ['content_length' => strlen($script_arr['content'])]);
  4349. if (!$table_data['content']) {
  4350. yield [
  4351. 'type' => 'done',
  4352. 'script' => [],
  4353. 'episode' => [],
  4354. 'answer' => $fullContent,
  4355. 'reasoning' => $fullReasoningContent,
  4356. 'usage' => $usage,
  4357. 'error' => '未生成剧本内容,请调整提示词再试'
  4358. ];
  4359. return;
  4360. }
  4361. }
  4362. if (isset($table_data['content']) && $table_data['content']) {
  4363. $chapters = $this->splitContent($table_data['content']);
  4364. $chapter_count = count($chapters);
  4365. if ($chapter_count > 0) {
  4366. $table_data['start_episode_sequence'] = 1;
  4367. $table_data['end_episode_sequence'] = $table_data['episode_num'] = $chapter_count;
  4368. }
  4369. }
  4370. }
  4371. $single_episode = [];
  4372. try {
  4373. DB::beginTransaction();
  4374. $now = date('Y-m-d H:i:s');
  4375. // 更新动漫大纲
  4376. $boolen = DB::table('mp_animes')->where('id', $anime_id)->update($table_data);
  4377. if (!$boolen) {
  4378. dLog('deepseek')->info('对话修改失败', $table_data);
  4379. logDB('deepseek', 'error', '对话修改失败', ['anime_id' => $anime_id]);
  4380. Utils::throwError('20003:对话修改失败');
  4381. }
  4382. // 保存对话记录
  4383. $records = [
  4384. [
  4385. 'uid' => $uid,
  4386. 'anime_id' => $anime_id,
  4387. 'sequence' => 0,
  4388. 'role' => 'user',
  4389. 'content' => $prompt,
  4390. 'created_at' => $now,
  4391. 'updated_at' => $now
  4392. ],
  4393. [
  4394. 'uid' => $uid,
  4395. 'anime_id' => $anime_id,
  4396. 'sequence' => 0,
  4397. 'role' => 'assistant',
  4398. // 'content' => '我已按照您的要求完成策划并将内容更新到您右侧的策划文档。',
  4399. 'content' => $fullContent,
  4400. 'created_at' => $now,
  4401. 'updated_at' => $now
  4402. ]
  4403. ];
  4404. // 保存对话记录
  4405. $boolen3 = DB::table('mp_anime_records')->insert($records);
  4406. if (!$boolen3) {
  4407. Utils::throwError('20003:对话记录保存失败');
  4408. }
  4409. // 单剧集模式:生成并保存剧集信息
  4410. if ($is_single) {
  4411. $anime_name = getProp($anime, 'anime_name', '未命名');
  4412. $episode_arr = $this->buildSingleEpisodeFromScript($script_arr, $fullContent, $anime_name);
  4413. if (empty($episode_arr['acts'])) {
  4414. Utils::throwError('20003:单剧集分镜内容解析失败,请确认输出格式');
  4415. }
  4416. $saveResult = $this->saveEpisodeVersionData(
  4417. $anime_id,
  4418. 1,
  4419. $episode_arr,
  4420. is_array(getProp($script_arr, 'roles')) ? getProp($script_arr, 'roles') : [],
  4421. is_array(getProp($script_arr, 'scenes')) ? getProp($script_arr, 'scenes') : [],
  4422. is_array(getProp($script_arr, 'props')) ? getProp($script_arr, 'props') : [],
  4423. null,
  4424. null,
  4425. false,
  4426. $content,
  4427. $now
  4428. );
  4429. $single_episode = $saveResult['episode'];
  4430. $episode_id = $saveResult['episode_id'];
  4431. $boolen5 = DB::table('mp_animes')->where('id', $anime_id)->update([
  4432. 'roles' => json_encode($saveResult['merged_roles'], 256),
  4433. 'scenes' => json_encode($saveResult['merged_scenes'], 256),
  4434. 'props' => json_encode($saveResult['merged_props'], 256),
  4435. 'updated_at' => $now
  4436. ]);
  4437. if ($boolen5 === false) {
  4438. Utils::throwError('20003:单剧集主表资源同步失败');
  4439. }
  4440. $table_data['roles'] = json_encode($saveResult['merged_roles'], 256);
  4441. $table_data['scenes'] = json_encode($saveResult['merged_scenes'], 256);
  4442. $table_data['props'] = json_encode($saveResult['merged_props'], 256);
  4443. $episode_record_content = '确认分镜大纲';
  4444. if ($content !== '') {
  4445. // $episode_record_content .= "\n\n###上传内容\n" . $content;
  4446. }
  4447. $episode_records = [
  4448. [
  4449. 'uid' => $uid,
  4450. 'anime_id' => $anime_id,
  4451. 'role' => 'user',
  4452. 'content' => $episode_record_content,
  4453. 'sequence' => 1,
  4454. 'episode_id' => $episode_id,
  4455. 'created_at' => $now,
  4456. 'updated_at' => $now
  4457. ],
  4458. [
  4459. 'uid' => $uid,
  4460. 'anime_id' => $anime_id,
  4461. 'role' => 'assistant',
  4462. 'content' => $fullContent,
  4463. // 'content' => '我已按照您的要求完成策划并将内容更新到您右侧的策划文档。',
  4464. 'sequence' => 1,
  4465. 'episode_id' => $episode_id,
  4466. 'created_at' => $now,
  4467. 'updated_at' => $now
  4468. ]
  4469. ];
  4470. $boolen4 = DB::table('mp_anime_records')->insert($episode_records);
  4471. if (!$boolen4) {
  4472. Utils::throwError('20003:单剧集分镜记录保存失败');
  4473. }
  4474. }
  4475. }catch (\Exception $e) {
  4476. DB::rollBack();
  4477. dLog('deepseek')->info('修改对话错误信息: '.$e->getMessage());
  4478. logDB('deepseek', 'error', '修改对话失败', ['error' => $e->getMessage(), 'anime_id' => $anime_id]);
  4479. yield [
  4480. 'type' => 'done',
  4481. 'answer' => $fullContent,
  4482. 'reasoning' => $fullReasoningContent,
  4483. 'usage' => $usage,
  4484. 'error' => $e->getMessage(),
  4485. ];
  4486. return;
  4487. }
  4488. DB::commit();
  4489. dLog('deepseek')->info('保存完毕');
  4490. if ($is_single && !empty($single_episode)) {
  4491. // $this->batchSetGlobalRoleImg([
  4492. // 'anime_id' => $anime_id,
  4493. // ]);
  4494. // $this->batchSetGlobalSceneImg([
  4495. // 'anime_id' => $anime_id,
  4496. // ]);
  4497. // // 将全局角色和场景的task_id、task_status和url继承给当前分集
  4498. // $this->inheritGlobalImagesToEpisodes($anime_id, $episode_id);
  4499. }
  4500. $table_data['anime_id'] = $anime_id;
  4501. $table_data['roles'] = json_decode($table_data['roles'], true);
  4502. $table_data['scenes'] = json_decode($table_data['scenes'], true);
  4503. unset($table_data['updated_at']);
  4504. unset($table_data['status']);
  4505. yield [
  4506. 'type' => 'done',
  4507. 'script' => $table_data,
  4508. 'episode' => $single_episode,
  4509. 'answer' => $fullContent,
  4510. 'reasoning' => $fullReasoningContent,
  4511. 'usage' => $usage
  4512. ];
  4513. }
  4514. public function chat($data) {
  4515. $uid = Site::getUid();
  4516. $anime_id = getProp($data, 'anime_id');
  4517. $file = getProp($data, 'file');
  4518. $content = getProp($data, 'content', '');
  4519. $bid = getProp($data, 'bid', 0);
  4520. $script_id = getProp($data, 'script_id', 0);
  4521. $episode_number = getProp($data, 'episode_number', 1);
  4522. $prompt = trim((string)getProp($data, 'prompt', '确认分镜大纲'));
  4523. $anime = DB::table('mp_animes')->where('id', $anime_id)->first();
  4524. if (!$anime) Utils::throwError('20003:该对话不存在!');
  4525. if ((int)getProp($anime, 'is_multi') !== 1 && (int)$episode_number !== 1) Utils::throwError('20003:单剧集不支持其他剧集序号');
  4526. $current_episode = DB::table('mp_anime_episodes')->where('anime_id', $anime_id)->where('episode_number', $episode_number)->where('is_default', 1)->first();
  4527. $base_episode = $current_episode ?: DB::table('mp_anime_episodes')->where('anime_id', $anime_id)->where('episode_number', $episode_number)->orderByDesc('id')->first();
  4528. $roles = $base_episode ? json_decode((string)getProp($base_episode, 'roles'), true) : json_decode((string)getProp($anime, 'roles'), true);
  4529. $scenes = $base_episode ? json_decode((string)getProp($base_episode, 'scenes'), true) : json_decode((string)getProp($anime, 'scenes'), true);
  4530. $roles = is_array($roles) ? $roles : [];
  4531. $scenes = is_array($scenes) ? $scenes : [];
  4532. // 获取最后一集序号
  4533. $end_episode_sequence = getProp($anime, 'end_episode_sequence');
  4534. if ($end_episode_sequence && $episode_number > $end_episode_sequence) Utils::throwError("20003:总集数只有{$end_episode_sequence}集,不允许继续策划下一集!");
  4535. // 转换主体列表和场景列表的格式
  4536. // 获取参考主体或场景
  4537. if ((int)$episode_number === 1) {
  4538. // $ref_data = DB::table('mp_animes')->where('id', $anime_id)->first();
  4539. $ref_data = []; // 第一集让AI自由发挥不再使用全局主体(会影响后续分集的继承问题)
  4540. }else {
  4541. $prev_episode_number = $episode_number - 1;
  4542. $ref_data = DB::table('mp_anime_episodes')->where('anime_id', $anime_id)->where('episode_number', $prev_episode_number)->where('is_default', 1)->first();
  4543. }
  4544. $ref_roles = getProp($ref_data, 'roles') ? json_decode(getProp($ref_data, 'roles'), true) : [];
  4545. $ref_scenes = getProp($ref_data, 'scenes') ? json_decode(getProp($ref_data, 'scenes'), true) : [];
  4546. $roles_content = $this->buildEpisodeItemContent($ref_roles, 'role');
  4547. $scenes_content = $this->buildEpisodeItemContent($ref_scenes, 'scene');
  4548. $timbres_content = "青年男:北京小爷、阳光青年、傲娇霸总、优柔公子、解说小明、青涩小生、低音沉郁、亲切青年
  4549. 青年女:柔美女友、魅力女友、爽快思思、高冷御姐、知性温婉、甜心小美、清新女声、甜美桃子、温柔文雅、甜美小源、贴心闺蜜
  4550. 中年男:胡子叔叔、油腻大叔、神秘法师、优柔帮主
  4551. 中年女:邻居阿姨
  4552. 老年男:幽默大爷
  4553. 老年女:婆婆
  4554. 萌娃:奶气萌娃";
  4555. if ($roles_content) {
  4556. $role_demo = "2.<主体列表>可在以下主体中选择多个(根据拆分出的分镜剧本来确认具体几个,不要出现旁白角色),不得自定义内容(必需带上冒号后的具体描述以及{}和{{}}中的内容,原样输出)。
  4557. 主体范围:\n {$roles_content}\n
  4558. 2.1如果本集剧情内容中出现了和给出的<主体列表>中不存在的新主体(或原主体发生了比较大的年龄、服装或身体变化),则不用在上述主体中选择,而是按照相同的格式新增一个主体显示在<主体列表>中。(以下是格式要求和生成要求,仅适用新出现的主体):
  4559. → 新主体分以下三部分:主体描述、人物提示词和音色,格式必须为:主体名:主体描述{人物提示词}{{音色}}
  4560. → 新主体的主体描述是分析人物在整部剧中的姓名、年龄、性格特征、社会身份或职业背景、个人成长经历或核心故事动机,以及在剧情中的目标与行为驱动力,不需要描述外貌、穿着或身体特征,重点突出心理结构、行为模式与叙事功能(缩略成一句话讲述完毕)。
  4561. → 新主体的人物提示词需要根据当前分析出来的美术风格生成(每个人物都只允许一套服装,如果多套服装则需区分主体名),包含性别(男、女)、大概年龄或年龄段(少年、中年、老年等)、人种(亚洲人、欧洲人)、发型(直发卷发等)、发色(黑色、棕色、红色、白色等)、整体视觉风格,以及详细的穿着风格和服装细节、面部特征(戴金丝眼镜、脸上有刀疤等,无面部特征可忽略)。描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合人物身份设定。该提示词(字数50-100字左右)在主体描述后用{}框起来。
  4562. → 新主体的音色必须在以下音色中($timbres_content)选择,需要根据角色描述的年纪和性别选择,比如角色是青年男性就只能在北京小爷、阳光青年、傲娇霸总、优柔公子、解说小明、青涩小生、低音沉郁、亲切青年中选择,选好的音色放在描述的最后用{{}}框起来。";
  4563. }else {
  4564. $role_demo = "2.<主体列表>中出现的“主体名”必须尽可能统一,严禁出现角色-科学家、角色-复仇者、角色-领导者等带有身份、职业、标签类的命名,只能使用服装差异(如校服装、卫衣装、西装)来区分同一角色,如角色-校服装等。
  4565. 2.1<主体列表>分以下三部分:主体描述、人物提示词和音色,格式必须为:主体名:主体描述{人物提示词}{{音色}}
  4566. 2.2<主体列表>的主体描述是分析人物在整部剧中的姓名、年龄、性格特征、社会身份或职业背景、个人成长经历或核心故事动机,以及在剧情中的目标与行为驱动力,不需要描述外貌、穿着或身体特征,重点突出心理结构、行为模式与叙事功能(缩略成一句话讲述完毕)。
  4567. 2.3<主体列表>的人物提示词需要根据当前分析出来的美术风格生成(每个人物都只允许一套服装,如果多套服装则需区分主体名),包含性别(男、女)、大概年龄或年龄段(少年、中年、老年等)、人种(亚洲人、欧洲人)、发型(直发卷发等)、发色(黑色、棕色、红色、白色等)、整体视觉风格,以及详细的穿着风格和服装细节、面部特征(戴金丝眼镜、脸上有刀疤等,无面部特征可忽略)。描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合人物身份设定。该提示词(字数50-100字左右)在主体描述后用{}框起来。
  4568. 2.4<主体列表>的音色必须在以下音色中($timbres_content)选择,需要根据角色描述的年纪和性别选择,比如角色是青年男性就只能在北京小爷、阳光青年、傲娇霸总、优柔公子、解说小明、青涩小生、低音沉郁、亲切青年中选择,选好的音色放在描述的最后用{{}}框起来。";
  4569. }
  4570. if ($scenes_content) {
  4571. $scene_demo = "3.<场景列表>可在以下场景中选择多个(需带上冒号后的具体描述),根据拆分出的分镜剧本来确认具体几个,不得自定义(必需带上冒号后的具体描述以及{}中的内容,原样输出)。
  4572. 场景范围:\n {$scenes_content}\n
  4573. 3.1如果本集剧情内容中出现了和给出的<场景列表>中不存在的新场景,则不用在上述场景中选择,而是按照相同的格式新增一个场景显示在<场景列表>中,必须严格注意不要出现任何角色和人物。(以下是格式要求和生成要求,仅适用新出现的场景):。
  4574. → 新场景分以下两部分:场景描述和场景提示词,格式必须为:场景描述{场景提示词}
  4575. → 新场景的场景描述是分析在整部剧中出现过的场景,场景描述尽量简洁贴合原文。
  4576. → 新场景的场景提示词用于视觉呈现的场景环境描述,该场景需包含环境氛围、光线效果、空间布局、色彩基调、视觉风格等,描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合剧情设定(可参考以下示例的场景列表中使用{}框起来的提示词,字数150-200字,需带上提炼出来的美术风格)。该提示词在场景描述后用{}框起来。\n";
  4577. }else {
  4578. $scene_demo = "3.<场景列表>与主体列表同理,需尽可能的对场景环境进行详细描述,必须严格注意不要出现任何角色和人物。
  4579. 3.1<场景列表>分以下两部分:场景描述和场景提示词,格式必须为:场景描述{场景提示词},注意不要出现{{}}这样的双大括号。
  4580. 3.2<场景列表>的场景描述是分析在整部剧中出现过的场景,场景描述尽量简洁贴合原文。
  4581. 3.3<场景列表>的场景提示词用于视觉呈现的场景环境描述,该场景需包含环境氛围、光线效果、空间布局、色彩基调、视觉风格等,描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合剧情设定(可参考以下示例的场景列表中使用{}框起来的提示词,字数150-200字,需带上提炼出来的美术风格)。该提示词在场景描述后用{}框起来。\n";
  4582. }
  4583. // 提取文件内容
  4584. if ($file) {
  4585. $uploaded_content = $this->extractFileContent($file);
  4586. if (!$uploaded_content) {
  4587. Utils::throwError('20003:无法提取文件内容,请检查文件格式');
  4588. }
  4589. } elseif ($script_id) {
  4590. $uploaded_content = $this->getContentByScriptId($script_id);
  4591. if (!$uploaded_content) {
  4592. Utils::throwError('20003:无法获取剧本内容,请检查剧本');
  4593. }
  4594. } elseif ($bid) {
  4595. $uploaded_content = $this->getContentByBid($bid);
  4596. if (!$uploaded_content) {
  4597. Utils::throwError('20003:无法获取书籍内容,请检查书籍');
  4598. }
  4599. } elseif ($content) {
  4600. $uploaded_content = filterContent((string)getProp($data, 'content', ''));
  4601. }
  4602. // elseif ($prompt) {
  4603. // $uploaded_content = filterContent($prompt);
  4604. // }
  4605. else {
  4606. $uploaded_content = '';
  4607. }
  4608. $input_art_style = getProp($anime, 'art_style');
  4609. $mappedArtStyle = $this->getArtStylePromptByInput($input_art_style);
  4610. // 美术风格
  4611. if (!$mappedArtStyle) {
  4612. $mappedArtStyle_prompt = '美术风格:根据剧本内容自定义美术风格';
  4613. }else {
  4614. if (strstr($mappedArtStyle, '基础画风风格词')) {
  4615. $mappedArtStyle_prompt = "美术风格强制设置如下(原样输出,不得更改任何内容):\n{$mappedArtStyle}\n";
  4616. }else {
  4617. $art_style_type = $input_art_style ? $input_art_style : getProp($anime, 'art_style_type');
  4618. $mappedArtStyle_prompt = "美术风格强制设置如下(原样输出,不得更改任何内容):\n基础画风风格词: {$art_style_type}\n{$mappedArtStyle}\n";
  4619. }
  4620. }
  4621. $prompt = trim((string)getProp($data, 'prompt', '确认分镜大纲'));
  4622. // $demo = "你是一个专业的文档分析助手及资深编剧,请根据用户提供的文档内容完成示例格式的内容输出(需包含以下几个板块: <剧集标题><故事梗概><美术风格><主体列表><场景列表><分镜剧本>,每个板块之间必须使用###分隔,#不能多也不能少;同时板块之间需满足以下要求:\n
  4623. // 强制要求:
  4624. // - 你只负责与剧本相关的内容,如果用户输入与剧本无关的内容,请告知用户并拒绝继续回答,此为第一优先原则,不满足此原则就忽略其他要求并停止继续输出
  4625. // - 如果用户输入修改美术风格、修改画风这类内容,请告知用户不可修改美术风格,并拒绝继续回答
  4626. // - 以上两条要求对修改主体角色或场景不生效,允许用户调整主体角色说明和场景描述等
  4627. // - 用户如果要求你帮忙写一个故事或者剧本,此类要求不属于与剧本无关,请帮助用户完成请求
  4628. // - 强制按照示例格式返回内容,不要更改格式或随意增加符号
  4629. // - 分镜剧本必须严格按照给出的“本集剧情内容”进行拆分,一个字都不要漏掉,同时也不要新增原文没有的内容
  4630. // - 如果给出的本集剧情内容是剧本格式,则不要改动剧本内容(必须强制使用原文内容),严格按照剧本的旁白和台词进行分镜的策划,不要跳过任何剧情
  4631. // - 分镜不是二次创作,必须严格“原文拆句”。优先把原文中可直接成镜头的句子拆成分镜,禁止把两句合并后再脑补过渡。
  4632. // - 画面描述必须直接使用,禁止新增任何原文没有写出的动作、人物、道具、地点、气氛、结果或因果。
  4633. // - 不要把“打开衣柜”扩写成“伸手轻抚嫁衣”,不要把“管家拎着两个箱子跟在后面”改成“沈耀和管家一起拎箱子”。主语、动作、数量、先后顺序必须完全按原文。
  4634. // - 分镜要和场景列表、人物主体保持一致\n
  4635. // 普通要求:
  4636. // 1.剧集标题必须与文档内容高度相关,剧集序号是{$episode_number}。
  4637. // {$role_demo}
  4638. // {$scene_demo}
  4639. // 4.<分镜剧本>要求包含“分幕标题”以及“分镜信息”:
  4640. // 4.1“分幕标题”是根据章节中的场景进行拆分,类似于“第1幕 场景、时间及室内或室外”的信息,其中场景只能在所给出的场景列表中选(不带场景详细描述)。
  4641. // 4.2“分镜信息”需同一场景内的对话内容完成分镜(每一幕分镜尽量不得低于5个),需包含“画面描述”、“构图设计”、“运镜调度”、“配音角色”、“台词内容”、“画面类型”、“尾帧描述”、“情感”、“性别”、“语速”、“音量”、“情感强度”、“音调”这几个部分,其中“画面描述”如果出现主体,只能选择上述主体列表中的一个,主体的名称也需要保持一致(不带主体具体描述)。
  4642. // 4.3分镜序号用1、2、3显示,如分镜1、分镜2,并且分镜序号需连续(跨分幕中的分镜序号需连续,但跨剧集中的不需要连续,如第2集的第1幕分镜序号从1开始)。
  4643. // 4.4“分镜信息”中的“台词内容”需满足以下要求: (1. 台词来源规则:- 台词内容必须逐字摘录自小说原文中的人物直接对话(带引号的内容)。- 不允许改写、总结、压缩、扩写或拼接原文对白。- 不允许生成原文中不存在的对话。2. 非对话过滤:- 旁白、心理描写、环境描写、动作描写等内容,禁止出现在台词内容中。3. 空台词规则:- 如果该分镜中没有人物对话,则“台词内容”必须返回“无”。- 严禁输出“无台词”、“没有”等任何替代文本。4. 禁止推测:- 不得根据语境补全人物可能说的话。5.如果没有找到原文内容,直接返回无)
  4644. // 4.5“分镜信息”中的“配音角色”需对应本集中的<主体列表>中的一个(非对话内容则统一固定为“旁白”角色),不得自己生成。
  4645. // 4.6“分镜信息”中的“场景”必须选择本集中的<场景列表>中的一个(不带详细表述)。
  4646. // 4.7“分镜信息”中的“出镜角色”需对应本集中的<主体列表>中的一个或多个(不带详细描述),如果没有出镜角色则填无。
  4647. // 4.8“分镜信息”中的“尾帧描述”需根据本镜和下一镜的剧本信息进行过渡生成,用于后续转视频进行衔接上下镜头的画面。
  4648. // 4.9“分镜信息”中的“情感”需根据台词内容和剧情氛围分析得出,可选值为:开心、悲伤、生气、惊讶、恐惧、厌恶、激动、冷漠、中性、沮丧、撒娇、害羞、安慰鼓励、咆哮、温柔、自然讲述、情感电台、磁性、广告营销、气泡音、新闻播报、娱乐八卦,不要在情感可选词后加任何解释性的词语,比如(对自己)、(对别人)等,不要自定义情感,必须在可选值中选择。
  4649. // 4.10“分镜信息”中的“性别”需根据配音角色的性别确定,可选值为:1(男)、2(女)、0(中性)。
  4650. // 4.11“分镜信息”中的“语速”需根据台词内容和情感氛围确定,可选值范围为:-50到100之间的整数,100代表2.0倍速,-50代表0.5倍数,负值表示减慢,正值表示加快,0表示正常语速。
  4651. // 4.12“分镜信息”中的“音量”需根据台词内容和情感氛围确定,可选值范围为:-50到100之间的整数,100代表2.0倍音量,-50代表0.5倍音量,负值表示降低音量,正值表示提高音量,0表示正常音量。
  4652. // 4.13“分镜信息”中的“情感强度”需根据情感的强烈程度确定,可选值范围为:1到5之间的整数,默认情况下选择4。
  4653. // 4.14“分镜信息”中的“音调”需根据台词内容和情感氛围确定,可选值范围为:-12到12之间的整数,负值表示降低音调,正值表示提高音调,0表示正常音调。
  4654. // 5.$mappedArtStyle_prompt\n\n
  4655. // 示例格式:\n
  4656. // 第1集:隐形的守护者
  4657. // ###故事梗概
  4658. // 内容梗概:许芸归家遭徐母毒打,她冷静周旋并编造谎言掩盖行踪,引得一旁阴沉的卞大伟愈发急躁。半个月后,徐母离家,卞大伟兽性大发欲行不轨。生死关头,隐形的程景现身肉搏,屋内家具横飞,卞大伟被“空气”重创。许芸趁乱逃出,却惊见地面凭空浮现一串血脚印。意识到守护者为救自己负伤,许芸不再逃避,对着虚无的空气执拗告白,誓要与这看不见的爱人共进退。
  4659. // ###美术风格
  4660. // 基础画风风格词:韩漫二次元
  4661. // 视觉风格描述:整体采用精致的韩漫厚涂风格,以浅灰冷调为基调,营造出重生复仇的清冷与压抑感。画面强调高对比度的光影处理,网吧场景通过低饱和度的杂乱色调表现颓废感,而清晨车祸瞬间则利用强烈的动态模糊与局部高光,突显“透明守护者”存在的神秘感与宿命感。人物形象细腻,眼神充满坚毅与破碎交织的故事感,完美契合偏执救赎的主题。
  4662. // ###主体列表
  4663. // 许芸-校服装:重生后的“假千金”,清醒果敢,不再任人唯亲。她是一名高中生,身材纤细,留着乌黑齐肩短发,眼神坚定。{18岁年轻女生,黑色长发,红色眼睛,五官精致,皮肤白皙,身材纤细,穿着日系校园制服,白色衬衫,黑色百褶裙,红色领带,表情悲伤,眼含泪水}{{甜心小美}}
  4664. // 许芸-卫衣装:半个月后居家或外出时的装扮,在清冷的环境中透着一丝坚毅与破碎感。{绝美年轻女子,白色长发,金色眼睛,五官精致,皮肤白皙,身材纤细,穿着淡蓝色古风汉服,气质清冷,神情平静,高细节人物刻画}{{爽快思思}}
  4665. // 程景:偏执深情的学霸,为救许芸变得“不可见”,是始终默默守护的隐形爱人。他身材高大挺拔,面容英俊。{年轻男性反派,银白色短发,红色眼睛,面容冷峻,皮肤苍白,身穿黑色长袍,嘴角冷笑,气质危险,高细节人物刻画}{{阳光青年}{{阳光青年}}
  4666. // ###场景列表
  4667. // 徐家老旧厨房:这是徐母劳作和惩罚许芸的地方。空间狭窄阴暗,充满了生活琐碎的压抑感。室内有一个旧式锅炉,案板上散落着面粉和包好的馄饨,光影对比强烈,凸显出家庭环境的冷酷。{影视质感。广角。
  4668. // 宽敞的天然山洞内部,石壁上错落有致地镶嵌着发光的萤石,散发出冷色调的微光,照亮了整个空间。洞顶有钟乳石垂下。光影层次丰富,营造出一种宁静而神圣的氛围。
  4669. // 无人物。}
  4670. // 徐家简陋客厅:半个月后冲突爆发的中心。陈设简单且陈旧,只有几把木凳和一张方桌。窗帘半掩,透进微弱的冷光,营造出一种暴风雨前的宁静与不安感。{影视质感。冷青色调,浓雾弥漫。
  4671. // 一条通往西北方向的崎岖山间土路,两旁是枯萎的灌木和怪石嶙峋。地面湿滑,反光强烈。远方山峦在阴雨中若隐若现,营造出一种穷追不舍的紧张感。
  4672. // 无人物。}
  4673. // 老旧公寓门口:许芸逃出家门后回望的视点。门框斑驳,透出屋内昏暗压抑的光线,与室外略显空旷的走廊形成对比,象征着逃离与未知的守护。{
  4674. // 豪华游轮内部宽敞的大堂或公共休息区。设计奢华,天花板高挑,有精致的装饰和柔和的暖色灯光。地面铺设着光洁的大理石,远处有巨大的落地窗,窗外是蓝色的海洋。自然光线充足,色彩明亮且饱和度适中,展现豪华游轮的质感。
  4675. // 无人物。}
  4676. // 破败巷弄与臭水沟:家门外的外部环境。一条狭窄、潮湿的巷弄,旁边是散发着冷气的臭水沟,水面偶尔泛起涟漪。这里是“透明守护者”受伤后经过的地方,氛围凄清。{
  4677. // 宽敞明亮的现代风格客厅,家具摆放整齐,色调偏向冷灰与深蓝,营造一种沉静而略带压抑的氛围。窗外光线柔和,室内光影对比不强烈,整体干净整洁。
  4678. // 无人物。}
  4679. // 潮湿的巷弄地面:特写视角。灰色的水泥地面湿迹斑斑,这是许芸发现血脚印并进行最后告白的地方。强调地面的质感和清冷的基调。{
  4680. // 一个现代居民小区的入口,有门卫岗亭和自动栏杆。道路宽阔,两旁绿化整齐,阳光明媚,整体色调略显冷峻,但光线清晰。
  4681. // 无人物。}
  4682. // ###分镜剧本
  4683. // ##第1幕:徐家老旧厨房 白天 室内
  4684. // 分镜1
  4685. // 画面描述:许芸-校服装站在厨房门口,书包背在肩上,徐母手持扫帚,正对着许芸的背部猛烈挥打。卞大伟站在锅炉后方,双手抱胸,阴沉的目光落在许芸身上。
  4686. // 场景:徐家老旧厨房
  4687. // 构图设计:全景,低角度仰视
  4688. // 运镜调度:手持拍摄
  4689. // 配音角色:旁白
  4690. // 出镜角色:许芸-校服装、徐母
  4691. // 台词内容:我一到家,徐母的扫帚就落在我身上,嘴里一个劲地骂我,
  4692. // 画面类型:普通画面
  4693. // 尾帧描述:手持拍摄微晃镜头,中年妇女猛力挥动扫帚击打少女背部,少女身体因冲击微颤,阴影中的肥胖男子目光冷冷跟随。
  4694. // 分镜2
  4695. // 画面描述:徐母面容扭曲,嘴唇快速开合,扫帚挥舞在空中。
  4696. // 场景:徐家老旧厨房
  4697. // 构图设计:近景,徐母面部特写
  4698. // 运镜调度:固定镜头
  4699. // 配音角色:徐母
  4700. // 出镜角色:徐母
  4701. // 台词内容:问我夜不归宿死哪儿去了。
  4702. // 画面类型:对口型
  4703. // 尾帧描述:固定镜头,中年妇女面部肌肉剧烈抖动,嘴唇快速开合进行辱骂,背景中的扫帚在空中上下挥动。
  4704. // ##第2幕:徐家简陋客厅 黄昏 室内
  4705. // 分镜3
  4706. // 画面描述:客厅窗户透出昏黄的夕阳余晖,空荡荡的客厅中央只有几把木凳和一张方桌,显得异常安静。
  4707. // 场景:徐家简陋客厅
  4708. // 构图设计:全景,景深虚化
  4709. // 运镜调度:固定镜头
  4710. // 配音角色:旁白
  4711. // 出镜角色:无
  4712. // 台词内容:可我没想到,半个月后,徐母居然再一次外出,
  4713. // 画面类型:普通画面
  4714. // 尾帧描述:固定镜头。昏黄的夕阳余晖在地面上缓慢移动,空气中的浮尘在光束中跳动,室内维持着死寂的安静。
  4715. // 分镜4
  4716. // 画面描述:一张泛黄的火车票特写,上面显示着隔壁市的站名和日期。
  4717. // 场景:徐家简陋客厅
  4718. // 构图设计:特写,火车票
  4719. // 运镜调度:固定镜头
  4720. // 配音角色:旁白
  4721. // 出镜角色:无
  4722. // 台词内容:这次是去隔壁市看望她生病的好姐妹。
  4723. // 画面类型:普通画面
  4724. // 尾帧描述:固定镜头。光影在火车票表面微微晃动,强调其陈旧的纸张纹理与关键的行程信息。
  4725. // \n\n";
  4726. $demo = "你是一个国际顶尖的执行导演/编剧,熟悉各种风格的影视化落地,我需要你将这个剧本调整一下,将其变成一个高质量的影视分镜剧本(需包含以下几个板块: <剧集标题><故事梗概><美术风格><主体列表><场景列表><分镜剧本>,每个板块之间必须使用###分隔,#不能多也不能少),要求画面和构图等进行高质量影视化改编,快节奏,改编的格式为先建立主体角色库、场景建库(其中场景建库的时候请分析故事发生的时候场景为白天还是晚上,建库的时候一起标注出来)以及关键的道具库。
  4727. 与此同时请将这个文本的情绪做一个分析,并且根据我下面提供的《情绪-视听语言映射表》进行一个文本情绪与视听语言的内容匹配。然后将每一个分镜按照「画面描述」「场景/场景时间(白天/晚上/傍晚等)」「构图设计(全景/近景/中景/远景,仰视/平视/俯视等)」「运镜调度」「配音角色」「出境角色」「台词内容」「画面类型」「尾帧描述」的格式来划分,请按照每分钟25个左右的分镜的节奏帮我生成(这是一个整体性的标准,如果文本的情绪分析需要更多或者更少的镜头数量,请根据具体情况调整),分镜头请细腻的拆分文本中的内容,将动作、场景转换、事件的逻辑关系等内容清晰的交代出来,分镜之间需要呈现场景之间的转换逻辑,不要突然跳到下一个场景之中。于此同时保持镜头剪辑的顿挫感,节奏不要太平均。
  4728. 每一个分镜的「画面描述」请参考上一个分镜中人物的状态/动作和位置来写,保持人物状态以及在场景中的位置的一致性,不要断裂感,同时也不要直接在内容里写“参考上个分镜”这种话,因为视频模型无法识别,使用模型可以直接识别的文本内容。每一个分镜中需要出现的内容都请标出,例如出场角色,出场道具等等。请尽可能详细的描述画面内容。\n
  4729. 普通要求:
  4730. 1.剧集标题必须与文档内容高度相关,剧集序号是{$episode_number}。
  4731. {$role_demo}
  4732. {$scene_demo}
  4733. 4.<分镜剧本>要求包含“分幕标题”以及“分镜信息”:
  4734. 4.1“分幕标题”是根据章节中的场景进行拆分,类似于“第1幕 场景、时间及室内或室外”的信息,其中场景只能在所给出的场景列表中选(不带场景详细描述)。
  4735. 4.2“分镜信息”需同一场景内的对话内容完成分镜(每一幕分镜尽量不得低于5个),需包含“画面描述”、“构图设计”、“运镜调度”、“配音角色”、“台词内容”、“画面类型”、“尾帧描述”、“情感”、“性别”、“语速”、“音量”、“情感强度”、“音调”这几个部分,其中“画面描述”如果出现主体,只能选择上述主体列表中的一个,主体的名称也需要保持一致(不带主体具体描述)。
  4736. 4.3分镜序号用1、2、3显示,如分镜1、分镜2,并且分镜序号需连续(跨分幕中的分镜序号需连续,但跨剧集中的不需要连续,如第2集的第1幕分镜序号从1开始),分镜序号前后不要带任何其他符号,特别是*符号。
  4737. 4.4“分镜信息”中的“台词内容”需满足以下要求: (1. 台词来源规则:- 台词内容必须逐字摘录自小说原文中的人物直接对话(带引号的内容)。- 不允许改写、总结、压缩、扩写或拼接原文对白。- 不允许生成原文中不存在的对话。2. 非对话过滤:- 旁白、心理描写、环境描写、动作描写等内容,禁止出现在台词内容中。3. 空台词规则:- 如果该分镜中没有人物对话,则“台词内容”必须返回“无”。- 严禁输出“无台词”、“没有”等任何替代文本。4. 禁止推测:- 不得根据语境补全人物可能说的话。5.如果没有找到原文内容,直接返回无)
  4738. 4.5“分镜信息”中的“配音角色”需对应本集中的<主体列表>中的一个(非对话内容则统一固定为“旁白”角色),不得自己生成。
  4739. 4.6“分镜信息”中的“场景”必须选择本集中的<场景列表>中的一个(不带详细表述)。
  4740. 4.7“分镜信息”中的“出镜角色”需对应本集中的<主体列表>中的一个或多个(不带详细描述),如果没有出镜角色则填无。
  4741. 4.8“分镜信息”中的“尾帧描述”需根据本镜和下一镜的剧本信息进行过渡生成,用于后续转视频进行衔接上下镜头的画面。
  4742. 4.9“分镜信息”中的“情感”需根据台词内容和剧情氛围分析得出,可选值为:开心、悲伤、生气、惊讶、恐惧、厌恶、激动、冷漠、中性、沮丧、撒娇、害羞、安慰鼓励、咆哮、温柔、自然讲述、情感电台、磁性、广告营销、气泡音、新闻播报、娱乐八卦,不要在情感可选词后加任何解释性的词语,比如(对自己)、(对别人)等,不要自定义情感,必须在可选值中选择。
  4743. 4.10“分镜信息”中的“性别”需根据配音角色的性别确定,可选值为:1(男)、2(女)、0(中性)。
  4744. 4.11“分镜信息”中的“语速”需根据台词内容和情感氛围确定,可选值范围为:-50到100之间的整数,100代表2.0倍速,-50代表0.5倍数,负值表示减慢,正值表示加快,0表示正常语速。
  4745. 4.12“分镜信息”中的“音量”需根据台词内容和情感氛围确定,可选值范围为:-50到100之间的整数,100代表2.0倍音量,-50代表0.5倍音量,负值表示降低音量,正值表示提高音量,0表示正常音量。
  4746. 4.13“分镜信息”中的“情感强度”需根据情感的强烈程度确定,可选值范围为:1到5之间的整数,默认情况下选择4。
  4747. 4.14“分镜信息”中的“音调”需根据台词内容和情感氛围确定,可选值范围为:-12到12之间的整数,负值表示降低音调,正值表示提高音调,0表示正常音调。
  4748. 5.$mappedArtStyle_prompt
  4749. 6.《情绪-视听语言映射表》:
  4750. 6.1 场景空间与时间心理学 (Scene & Temporality)
  4751. 维度 | 视听特征 | 对应核心情绪 | 心理学/叙事原理
  4752. -----|---------|------------|----------------
  4753. 场景空间 | 封闭狭小 (Corridors/Closets) | 压抑、幽闭恐惧 | 物理限制映射心理自由的剥夺。
  4754. 场景空间 | 极度开阔 (Empty Plains/Desert) | 孤独、渺小、疏离 | 剥离社会性支撑,强化个体的虚无感。
  4755. 时间选择 | 黄金时刻 (Golden Hour) | 怀旧、温情、希望 | 柔和漫反射光线符合大脑对“美好记忆”的视觉加工。
  4756. 时间选择 | 蓝调时刻/深夜 (Blue/Night) | 冷漠、忧郁、未知危险 | 冷色调抑制血清素,增强感官的不安全感。
  4757. 6.2 剪辑节奏与感知阈值 (Shot Duration & Rhythm)
  4758. 维度 | 视听特征 | 对应核心情绪 | 心理学/叙事原理
  4759. -----|---------|------------|----------------
  4760. 长镜头 | >15秒的静止或缓慢移动 | 沉思、肃穆、煎熬 | 强迫观众进入实时流逝,产生生理性的存在感。
  4761. 快切剪辑 | <2秒的频繁切换 | 混乱、亢奋、惊恐 | 模拟高肾上腺素下的视觉捕捉,剥夺深度思考时间。
  4762. 节奏错位 | 非同步切分 (L-cut/J-cut) | 扭曲、不协调 | 破坏连续性假定,产生心理上的不适与悬疑感。
  4763. 6.3 运镜调度与情绪调动 (Cinematic Movement & Affect)
  4764. 运镜方式 | 物理特征 | 心理/情绪功能 | 专业应用场景
  4765. --------|---------|--------------|-------------
  4766. 推镜头 (Dolly In) | 摄影机物理接近主体 | 专注、发现、心理逼近。通过压缩背景空间,强制观众忽略环境,进入主体的内心世界。 | 角色顿悟、秘密揭晓、情感升温。
  4767. 拉镜头 (Dolly Out) | 摄影机物理远离主体 | 疏离、解脱、宿命感。将主体置于广阔背景中,显示主体的渺小或强调事情已成定局。 | 悲剧结尾、孤独的告别、审视全局。
  4768. 摇镜头 (Pan/Tilt) | 轴心转动,视点改变 | 观察、审读、惊奇感。模拟人眼转头,用于揭示空间关系或进行逻辑上的“连接”。 | 发现潜伏的危机、环视战场、建立空间地理志。
  4769. 跟拍 (Tracking Shot) | 保持与主体同速移动 | 强烈的参与感、伴随性焦虑。让观众感觉自己是角色的共谋者或影子,产生“实时感”。 | 逃亡、快步穿行于走廊、展示角色的行动力。
  4770. 横移 (Truck/Side Track) | 与主体平行横向移动 | 客观审视、叙事流逝。像卷轴画一样展开,产生一种冷静、观察者的旁白视角。 | 展示环境细节、两个角色在边走边谈时的心理博弈。
  4771. 手持/肩扛 (Handheld) | 生理性不规则晃动 | 不安、真实感、心理动荡。模拟肉眼观察,通过不稳定性唤起观众的本能警觉。 | 冲突现场、纪录片式纪实感、角色精神崩溃。
  4772. 斯坦尼康 (Steadicam) | 流畅、近乎滑行的移动 | 全知感、梦幻、无缝观察。消除物理重力感,产生一种“上帝视角”或“游魂般”的观察。 | 漫长而复杂的转场、舞会等优雅场景的穿梭。
  4773. 希区柯克变焦 (Dolly Zoom) | 推进同时反向变焦 | 眩晕、世界崩塌、极度惊骇。空间透视的物理扭曲直接引发生理性失衡。 | 角色得知真相的瞬间、高度恐惧。
  4774. 升降/摇臂 (Crane Shot) | 垂直或斜向空间位移 | 释放、崇高、超越性。打破二维平面的限制,将情绪从微观拉向宏观。 | 史诗感的结局、战场全景、情绪的爆发式升华。
  4775. 6.4 构图、色彩与符号学 (Composition & Aesthetics)
  4776. 维度 | 视听特征 | 对应核心情绪 | 心理学原理
  4777. -----|---------|------------|------------
  4778. 角度 | 仰角 (Low Angle) | 威严、恐惧、崇高 | 模拟幼年视角,确立权力层级。
  4779. 角度 | 俯角 (High Angle) | 脆弱、无助、卑微 | 模拟审视感,削弱主体力量。
  4780. 构图 | 荷兰坡 (Dutch Tilt) | 疯狂、崩溃、失衡 | 重力轴偏离导致前庭系统产生失衡反应。
  4781. 色彩 | 极高饱和度/冷暖撞色 | 狂喜、冲突、超现实 | 强烈的视觉刺激诱发情绪阈值波动。
  4782. 6.5 进阶视听修辞与认知干预 (Advanced AV Rhetoric)
  4783. 修辞策略 | 技术实现 | 心理/情绪效应 | 理论基石
  4784. --------|---------|--------------|----------
  4785. 声画对位 (Contrapuntal AV) | 在极端残酷/激烈的画面中配以极其舒缓优雅的音乐(如古典乐)。 | 荒诞恐怖感、神圣的残酷、间离效果。通过感官冲突迫使观众从原始情绪中抽离,进入形而上的思考。 | 安德烈·塔可夫斯基《雕刻时光》、库布里克式美学。
  4786. 非平衡构图 (Compositional Tension) | 突破平衡法则,将视觉重点(Eye-tracking)置于画面边缘或极端角落。 | 本能警觉、不安全感、期待感。利用大脑对“视觉缺失”的补充本能,暗示画面外存在不可见的威胁。 | 格式塔心理学、眼动追踪实验。
  4787. 认知负荷调控 (Neurocinematics) | 利用快速剪辑抑制前额叶皮层(理性区),激活杏仁核(情绪区)。 | 纯粹感官化、生存模式切换。若要观众思考,则延长镜头以激活认知分析;若要观众受惊,则通过视觉过载切断其逻辑判断。 | 神经电影学 (Neurocinematics)。
  4788. 6.6 声音设计与潜意识影响 (Soundscape)
  4789. 维度 | 视听特征 | 对应核心情绪 | 心理学/生理学原理
  4790. -----|---------|------------|------------------
  4791. 音色 | 极低频音 (Infra-sound) | 潜伏恐惧、生理不安 | 模仿自然灾害频率,唤醒生物避害本能。
  4792. 环境声 | 极度死寂 (Silence) | 震惊、神圣、虚无 | 听觉剥夺放大视觉信号,产生心理张力。
  4793. 非现场音 | 不和谐音/尖锐音效 | 精神紧绷、躁郁 | 高频声音直接刺激杏仁核,引发防御机制。
  4794. 理论基石:
  4795. - The Five C's of Cinematography by Joseph V. Mascelli
  4796. - Film Art: An Introduction by David Bordwell
  4797. - Sight, Sound, Motion by Herbert Zettl
  4798. - Notes on the Cinematograph by Robert Bresson
  4799. \n\n
  4800. 7.必须严格按照以下示例格式返回剧本,不得添加其他符号
  4801. 示例格式:\n
  4802. 第1集:隐形的守护者
  4803. ###故事梗概
  4804. 内容梗概:许芸归家遭徐母毒打,她冷静周旋并编造谎言掩盖行踪,引得一旁阴沉的卞大伟愈发急躁。半个月后,徐母离家,卞大伟兽性大发欲行不轨。生死关头,隐形的程景现身肉搏,屋内家具横飞,卞大伟被“空气”重创。许芸趁乱逃出,却惊见地面凭空浮现一串血脚印。意识到守护者为救自己负伤,许芸不再逃避,对着虚无的空气执拗告白,誓要与这看不见的爱人共进退。
  4805. ###美术风格
  4806. 基础画风风格词:韩漫二次元
  4807. 视觉风格描述:整体采用精致的韩漫厚涂风格,以浅灰冷调为基调,营造出重生复仇的清冷与压抑感。画面强调高对比度的光影处理,网吧场景通过低饱和度的杂乱色调表现颓废感,而清晨车祸瞬间则利用强烈的动态模糊与局部高光,突显“透明守护者”存在的神秘感与宿命感。人物形象细腻,眼神充满坚毅与破碎交织的故事感,完美契合偏执救赎的主题。
  4808. ###主体列表
  4809. 许芸-校服装:重生后的“假千金”,清醒果敢,不再任人唯亲。她是一名高中生,身材纤细,留着乌黑齐肩短发,眼神坚定。{18岁年轻女生,黑色长发,红色眼睛,五官精致,皮肤白皙,身材纤细,穿着日系校园制服,白色衬衫,黑色百褶裙,红色领带,表情悲伤,眼含泪水}{{甜心小美}}
  4810. 许芸-卫衣装:半个月后居家或外出时的装扮,在清冷的环境中透着一丝坚毅与破碎感。{绝美年轻女子,白色长发,金色眼睛,五官精致,皮肤白皙,身材纤细,穿着淡蓝色古风汉服,气质清冷,神情平静,高细节人物刻画}{{爽快思思}}
  4811. 程景:偏执深情的学霸,为救许芸变得“不可见”,是始终默默守护的隐形爱人。他身材高大挺拔,面容英俊。{年轻男性反派,银白色短发,红色眼睛,面容冷峻,皮肤苍白,身穿黑色长袍,嘴角冷笑,气质危险,高细节人物刻画}{{阳光青年}{{阳光青年}}
  4812. ###场景列表
  4813. 徐家老旧厨房:这是徐母劳作和惩罚许芸的地方。空间狭窄阴暗,充满了生活琐碎的压抑感。室内有一个旧式锅炉,案板上散落着面粉和包好的馄饨,光影对比强烈,凸显出家庭环境的冷酷。{影视质感。广角。
  4814. 宽敞的天然山洞内部,石壁上错落有致地镶嵌着发光的萤石,散发出冷色调的微光,照亮了整个空间。洞顶有钟乳石垂下。光影层次丰富,营造出一种宁静而神圣的氛围。
  4815. 无人物。}
  4816. 徐家简陋客厅:半个月后冲突爆发的中心。陈设简单且陈旧,只有几把木凳和一张方桌。窗帘半掩,透进微弱的冷光,营造出一种暴风雨前的宁静与不安感。{影视质感。冷青色调,浓雾弥漫。
  4817. 一条通往西北方向的崎岖山间土路,两旁是枯萎的灌木和怪石嶙峋。地面湿滑,反光强烈。远方山峦在阴雨中若隐若现,营造出一种穷追不舍的紧张感。
  4818. 无人物。}
  4819. 老旧公寓门口:许芸逃出家门后回望的视点。门框斑驳,透出屋内昏暗压抑的光线,与室外略显空旷的走廊形成对比,象征着逃离与未知的守护。{
  4820. 豪华游轮内部宽敞的大堂或公共休息区。设计奢华,天花板高挑,有精致的装饰和柔和的暖色灯光。地面铺设着光洁的大理石,远处有巨大的落地窗,窗外是蓝色的海洋。自然光线充足,色彩明亮且饱和度适中,展现豪华游轮的质感。
  4821. 无人物。}
  4822. 破败巷弄与臭水沟:家门外的外部环境。一条狭窄、潮湿的巷弄,旁边是散发着冷气的臭水沟,水面偶尔泛起涟漪。这里是“透明守护者”受伤后经过的地方,氛围凄清。{
  4823. 宽敞明亮的现代风格客厅,家具摆放整齐,色调偏向冷灰与深蓝,营造一种沉静而略带压抑的氛围。窗外光线柔和,室内光影对比不强烈,整体干净整洁。
  4824. 无人物。}
  4825. 潮湿的巷弄地面:特写视角。灰色的水泥地面湿迹斑斑,这是许芸发现血脚印并进行最后告白的地方。强调地面的质感和清冷的基调。{
  4826. 一个现代居民小区的入口,有门卫岗亭和自动栏杆。道路宽阔,两旁绿化整齐,阳光明媚,整体色调略显冷峻,但光线清晰。
  4827. 无人物。}
  4828. ###分镜剧本
  4829. ##第1幕:徐家老旧厨房 白天 室内
  4830. 分镜1
  4831. 画面描述:许芸-校服装站在厨房门口,书包背在肩上,徐母手持扫帚,正对着许芸的背部猛烈挥打。卞大伟站在锅炉后方,双手抱胸,阴沉的目光落在许芸身上。
  4832. 场景:徐家老旧厨房
  4833. 构图设计:全景,低角度仰视
  4834. 运镜调度:手持拍摄
  4835. 配音角色:旁白
  4836. 出镜角色:许芸-校服装、徐母
  4837. 台词内容:我一到家,徐母的扫帚就落在我身上,嘴里一个劲地骂我,
  4838. 画面类型:普通画面
  4839. 尾帧描述:手持拍摄微晃镜头,中年妇女猛力挥动扫帚击打少女背部,少女身体因冲击微颤,阴影中的肥胖男子目光冷冷跟随。
  4840. 分镜2
  4841. 画面描述:徐母面容扭曲,嘴唇快速开合,扫帚挥舞在空中。
  4842. 场景:徐家老旧厨房
  4843. 构图设计:近景,徐母面部特写
  4844. 运镜调度:固定镜头
  4845. 配音角色:徐母
  4846. 出镜角色:徐母
  4847. 台词内容:问我夜不归宿死哪儿去了。
  4848. 画面类型:对口型
  4849. 尾帧描述:固定镜头,中年妇女面部肌肉剧烈抖动,嘴唇快速开合进行辱骂,背景中的扫帚在空中上下挥动。
  4850. ##第2幕:徐家简陋客厅 黄昏 室内
  4851. 分镜3
  4852. 画面描述:客厅窗户透出昏黄的夕阳余晖,空荡荡的客厅中央只有几把木凳和一张方桌,显得异常安静。
  4853. 场景:徐家简陋客厅
  4854. 构图设计:全景,景深虚化
  4855. 运镜调度:固定镜头
  4856. 配音角色:旁白
  4857. 出镜角色:无
  4858. 台词内容:可我没想到,半个月后,徐母居然再一次外出,
  4859. 画面类型:普通画面
  4860. 尾帧描述:固定镜头。昏黄的夕阳余晖在地面上缓慢移动,空气中的浮尘在光束中跳动,室内维持着死寂的安静。
  4861. 分镜4
  4862. 画面描述:一张泛黄的火车票特写,上面显示着隔壁市的站名和日期。
  4863. 场景:徐家简陋客厅
  4864. 构图设计:特写,火车票
  4865. 运镜调度:固定镜头
  4866. 配音角色:旁白
  4867. 出镜角色:无
  4868. 台词内容:这次是去隔壁市看望她生病的好姐妹。
  4869. 画面类型:普通画面
  4870. 尾帧描述:固定镜头。光影在火车票表面微微晃动,强调其陈旧的纸张纹理与关键的行程信息。
  4871. \n\n";
  4872. $origin_prompt = trim((string)getProp($data, 'prompt', '确认分镜大纲'));
  4873. $is_regenerate_version = !in_array($origin_prompt, ['确认分镜大纲', '继续策划下一集'], true);
  4874. $is_global_generate_pics = $origin_prompt == '确认分镜大纲' ? 1 : 0;
  4875. $prompt = $origin_prompt;
  4876. $is_single = (int)getProp($anime, 'is_multi') !== 1;
  4877. // 获取章节内容
  4878. $full_content = getProp($anime, 'content');
  4879. if ($uploaded_content) {
  4880. $full_content = $uploaded_content;
  4881. }
  4882. // 根据章节内容拆分章节
  4883. $chapters = $this->splitContent($full_content);
  4884. $chapter_content = isset($chapters[($episode_number-1)]['content']) ? $chapters[($episode_number-1)]['content'] : '';
  4885. if (!$chapter_content) $chapter_content = $full_content; // 如果无法拆分章节则直接用原文
  4886. $messages = [];
  4887. if ($prompt == '确认分镜大纲') {
  4888. // 暂时保留
  4889. $content = $chapter_content;
  4890. if ($is_single) $content = $full_content; // 单剧集使用全文
  4891. if (!$content) {
  4892. Utils::throwError('20003:章节内容无法获取,请确认');
  4893. }
  4894. $question = $is_single
  4895. ? $demo . "请根据以上要求分析完整剧情,直接输出第1集完整单剧集分镜剧本,需一次性生成并可直接保存,剧情内容如下: \n\n$content"
  4896. : $demo . "请根据以上要求分析本章剧情,完成分镜剧本,本集剧情内容如下: \n\n$content";
  4897. // 构建消息
  4898. $messages[] =
  4899. [
  4900. 'role' => 'user',
  4901. 'content' => $question
  4902. ];
  4903. }else if ($prompt == '继续策划下一集') {
  4904. if ($is_single) {
  4905. Utils::throwError('20003:单剧集不支持继续策划下一集');
  4906. }
  4907. $content = $chapter_content;
  4908. if (!$content) {
  4909. Utils::throwError('20003:章节内容无法获取,请确认');
  4910. }
  4911. $question = $demo."请根据以上要求分析本集剧情内容,完成本集分镜剧本(需衔接上一集内容)";
  4912. // 获取上一集最后记录
  4913. $prev_sequence = $episode_number - 1;
  4914. $messages = $this->getEpisodeChatContent($anime_id, $prev_sequence, $content);
  4915. // 构建消息
  4916. $messages[] =
  4917. [
  4918. 'role' => 'user',
  4919. 'content' => $question
  4920. ];
  4921. }else {
  4922. $content = $uploaded_content ? $chapter_content : trim((string)getProp($base_episode, 'content'));
  4923. if (!$content) {
  4924. $content = $is_single ? $full_content : ($chapter_content ?: $full_content);
  4925. }
  4926. if (!$content) {
  4927. Utils::throwError('20003:章节内容无法获取,请确认');
  4928. }
  4929. $messages = $this->getEpisodeChatMessages($anime_id, $episode_number);
  4930. $targetEpisodeText = $is_single ? '第1集完整单剧集版本' : "第{$episode_number}集完整的新版本剧集";
  4931. $question = $demo . "\n请基于以上历史对话记录,重新生成{$targetEpisodeText},并严格按示例格式输出完整内容。\n";
  4932. if ($origin_prompt) {
  4933. $question .= "\n本次修改要求如下:\n{$origin_prompt}";
  4934. }
  4935. $messages[] =
  4936. [
  4937. 'role' => 'user',
  4938. 'content' => $question
  4939. ];
  4940. }
  4941. // 处理文本模型
  4942. $model = getProp($data, 'model');
  4943. if (!$model) {
  4944. // 用户没有输入,从anime表获取
  4945. $anime = DB::table('mp_animes')->where('id', $anime_id)->first();
  4946. $model = getProp($anime, 'model');
  4947. if (!$model) {
  4948. // anime表也没有,使用默认值
  4949. $model = 'doubao-seed-2-0-mini-260215';
  4950. }
  4951. }
  4952. // 验证模型是否在可用模型表中
  4953. if (!DB::table('mp_text_models')->where('model', $model)->where('is_enabled', 1)->exists()) {
  4954. $model = 'doubao-seed-2-0-mini-260215';
  4955. }
  4956. // 模型兼容性处理:deepseek-chat 和 deepseek-reasoner 映射到 deepseek-v4-flash
  4957. $thinkingMode = getProp($data, 'thinking', 'disabled'); // 默认非思考模式
  4958. $reasoningEffort = getProp($data, 'reasoning_effort', 'high'); // 默认思考强度
  4959. if ($model === 'deepseek-chat') {
  4960. // deepseek-chat 映射到 deepseek-v4-flash,非思考模式
  4961. $model = 'deepseek-v4-flash';
  4962. $thinkingMode = 'disabled';
  4963. } elseif ($model === 'deepseek-reasoner') {
  4964. // deepseek-reasoner 映射到 deepseek-v4-flash,思考模式
  4965. $model = 'deepseek-v4-flash';
  4966. $thinkingMode = 'enabled';
  4967. }
  4968. $post_data = [
  4969. 'model' => $model,
  4970. 'messages' => $messages,
  4971. // 'max_tokens' => 8192,
  4972. 'temperature' => 0.2,
  4973. 'frequency_penalty' => 0,
  4974. 'presence_penalty' => 0,
  4975. 'response_format' => ['type' => 'text'],
  4976. 'thinking' => ['type' => $thinkingMode],
  4977. 'stream' => true // 启用流式输出
  4978. ];
  4979. if ($thinkingMode == 'enabled') $post_data['reasoning_effort'] = $reasoningEffort;
  4980. // 根据模型类型选择调用方法
  4981. $fullContent = '';
  4982. $fullReasoningContent = '';
  4983. $usage = [];
  4984. try {
  4985. if (in_array($model, ['deepseek-reasoner', 'deepseek-chat', 'deepseek-v4-flash', 'deepseek-v4-pro'])) {
  4986. // DeepSeek 官方模型使用 DeepSeek API
  4987. $streamGenerator = $this->deepSeekStreamResponse($post_data);
  4988. } else if (in_array($model, $this->gpt_text_models)) {
  4989. // GPT-5.4 模型使用 TokenRouter API
  4990. $streamGenerator = $this->gpt54StreamResponse($post_data);
  4991. } else {
  4992. // 其他模型使用火山引擎API
  4993. $streamGenerator = $this->volcEngineChatCompletion($post_data);
  4994. }
  4995. // 验证返回值类型
  4996. if (!is_iterable($streamGenerator)) {
  4997. $errorType = is_object($streamGenerator) ? get_class($streamGenerator) : gettype($streamGenerator);
  4998. dLog('deepseek')->error('chat流式生成器无效', [
  4999. 'generator_type' => $errorType,
  5000. 'model' => $model,
  5001. 'anime_id' => $anime_id,
  5002. 'episode_number' => $episode_number
  5003. ]);
  5004. logDB('deepseek', 'error', 'chat流式生成器类型错误', [
  5005. 'type' => $errorType,
  5006. 'model' => $model
  5007. ]);
  5008. yield [
  5009. 'type' => 'error',
  5010. 'episode' => [],
  5011. 'answer' => '',
  5012. 'reasoning' => '',
  5013. 'usage' => [],
  5014. 'error' => '接口返回数据异常,请重新请求'
  5015. ];
  5016. return;
  5017. }
  5018. // 处理流式输出
  5019. foreach ($streamGenerator as $chunk) {
  5020. if (isset($chunk['type'])) {
  5021. if ($chunk['type'] === 'done') {
  5022. // 最终结果
  5023. $fullContent = $chunk['full_content'];
  5024. $fullReasoningContent = $chunk['full_reasoning'];
  5025. $usage = isset($chunk['usage']) ? $chunk['usage'] : [];
  5026. } else {
  5027. // 逐块yield数据
  5028. yield $chunk;
  5029. }
  5030. }
  5031. }
  5032. } catch (\Exception $e) {
  5033. dLog('deepseek')->error('chat流式处理异常: ' . $e->getMessage(), [
  5034. 'model' => $model,
  5035. 'anime_id' => $anime_id,
  5036. 'episode_number' => $episode_number,
  5037. 'trace' => $e->getTraceAsString()
  5038. ]);
  5039. logDB('deepseek', 'error', 'chat流式处理失败', [
  5040. 'error' => $e->getMessage(),
  5041. 'model' => $model
  5042. ]);
  5043. yield [
  5044. 'type' => 'error',
  5045. 'episode' => [],
  5046. 'answer' => '',
  5047. 'reasoning' => '',
  5048. 'usage' => [],
  5049. 'error' => '处理请求时发生异常: ' . $e->getMessage()
  5050. ];
  5051. return;
  5052. }
  5053. dLog('deepseek')->info('完整内容: '.$fullContent);
  5054. logDB('deepseek', 'info', '完整内容', ['content' => $fullContent, 'anime_id' => $anime_id]);
  5055. // 处理完整内容并返回最终结果
  5056. $episode_arr = handleEpisodeContent($fullContent);
  5057. logDB('deepseek', 'info', '解析内容', $episode_arr);
  5058. if (empty($episode_arr['acts'])) {
  5059. // 未生成剧本相关内容,保存对话记录并返回提示
  5060. dLog('deepseek')->info('未生成分镜剧本相关的内容');
  5061. try {
  5062. $now = date('Y-m-d H:i:s');
  5063. // 保存对话记录
  5064. $records = [
  5065. [
  5066. 'uid' => $uid,
  5067. 'anime_id' => $anime_id,
  5068. 'sequence' => $episode_number,
  5069. 'role' => 'user',
  5070. 'content' => $prompt,
  5071. 'created_at' => $now,
  5072. 'updated_at' => $now
  5073. ],
  5074. [
  5075. 'uid' => $uid,
  5076. 'anime_id' => $anime_id,
  5077. 'sequence' => $episode_number,
  5078. 'role' => 'assistant',
  5079. 'content' => $fullContent,
  5080. // 'content' => '我已按照您的要求完成策划并将内容更新到您右侧的策划文档。',
  5081. 'created_at' => $now,
  5082. 'updated_at' => $now
  5083. ]
  5084. ];
  5085. DB::table('mp_anime_records')->insert($records);
  5086. } catch (\Exception $e) {
  5087. dLog('deepseek')->error('保存对话记录失败', ['error' => $e->getMessage()]);
  5088. }
  5089. yield [
  5090. 'type' => 'done',
  5091. 'episode' => [],
  5092. 'answer' => $fullContent,
  5093. 'reasoning' => $fullReasoningContent,
  5094. 'usage' => $usage,
  5095. 'error' => '未生成分镜剧本相关的内容,请调整提示词再试'
  5096. ];
  5097. return;
  5098. }
  5099. $existing_roles = $base_episode ? json_decode((string)getProp($base_episode, 'roles'), true) : [];
  5100. $existing_scenes = $base_episode ? json_decode((string)getProp($base_episode, 'scenes'), true) : [];
  5101. $existing_props = $base_episode ? json_decode((string)getProp($base_episode, 'props'), true) : [];
  5102. $existing_roles = is_array($existing_roles) ? $existing_roles : [];
  5103. $existing_scenes = is_array($existing_scenes) ? $existing_scenes : [];
  5104. $existing_props = is_array($existing_props) ? $existing_props : [];
  5105. $now = date('Y-m-d H:i:s');
  5106. try {
  5107. DB::beginTransaction();
  5108. $saveResult = $this->saveEpisodeVersionData(
  5109. $anime_id,
  5110. $episode_number,
  5111. $episode_arr,
  5112. $existing_roles,
  5113. $existing_scenes,
  5114. $existing_props,
  5115. $current_episode,
  5116. $base_episode,
  5117. $is_regenerate_version,
  5118. $content,
  5119. $now
  5120. );
  5121. $episode = $saveResult['episode'];
  5122. $episode_id = $saveResult['episode_id'];
  5123. $merged_roles = $saveResult['merged_roles'];
  5124. $merged_scenes = $saveResult['merged_scenes'];
  5125. $merged_props = $saveResult['merged_props'];
  5126. $record_content = $origin_prompt;
  5127. if ($uploaded_content !== '') {
  5128. // $record_content .= "\n\n###上传内容\n" . $uploaded_content;
  5129. }
  5130. $records = [
  5131. [
  5132. 'uid' => $uid,
  5133. 'anime_id' => $anime_id,
  5134. 'role' => 'user',
  5135. 'content' => $record_content,
  5136. 'sequence' => $episode_number,
  5137. 'episode_id' => $episode_id,
  5138. 'created_at' => $now,
  5139. 'updated_at' => $now
  5140. ],
  5141. [
  5142. 'uid' => $uid,
  5143. 'anime_id' => $anime_id,
  5144. 'role' => 'assistant',
  5145. 'content' => $fullContent,
  5146. // 'content' => '我已按照您的要求完成策划并将内容更新到您右侧的策划文档。',
  5147. 'sequence' => $episode_number,
  5148. 'episode_id' => $episode_id,
  5149. 'created_at' => $now,
  5150. 'updated_at' => $now
  5151. ]
  5152. ];
  5153. $boolen4 = DB::table('mp_anime_records')->insert($records);
  5154. if (!$boolen4) {
  5155. Utils::throwError('20003:对话记录保存失败');
  5156. }
  5157. // 保存模型和内容到anime表
  5158. $update_anime_data = [
  5159. 'model' => $model,
  5160. 'updated_at' => $now
  5161. ];
  5162. if ($uploaded_content) {
  5163. $update_anime_data['content'] = $uploaded_content;
  5164. }
  5165. DB::table('mp_animes')->where('id', $anime_id)->update($update_anime_data);
  5166. }catch (\Exception $e) {
  5167. DB::rollBack();
  5168. dLog('deepseek')->info('新建对话错误信息: '.$e->getMessage());
  5169. yield [
  5170. 'type' => 'done',
  5171. 'answer' => $fullContent,
  5172. 'reasoning' => $fullReasoningContent,
  5173. 'usage' => $usage,
  5174. 'error' => $e->getMessage(),
  5175. ];
  5176. return;
  5177. }
  5178. DB::commit();
  5179. if ($is_global_generate_pics) {
  5180. // // 批量生成全局角色图片
  5181. // $this->batchSetGlobalRoleImg([
  5182. // 'anime_id' => $anime_id,
  5183. // ], true);
  5184. // // 批量生成全局场景图片
  5185. // $this->batchSetGlobalSceneImg([
  5186. // 'anime_id' => $anime_id,
  5187. // ], true);
  5188. // // 将全局角色和场景的task_id、task_status和url继承给当前分集
  5189. // $this->inheritGlobalImagesToEpisodes($anime_id, $episode_id);
  5190. }
  5191. $episode['episode_id'] = $episode_id;
  5192. $episode['roles'] = $merged_roles;
  5193. $episode['scenes'] = $merged_scenes;
  5194. $episode['acts'] = isset($episode_arr['acts']) && is_array($episode_arr['acts']) ? $episode_arr['acts'] : [];
  5195. yield [
  5196. 'type' => 'done',
  5197. 'episode' => $episode,
  5198. 'answer' => $fullContent,
  5199. 'reasoning' => $fullReasoningContent,
  5200. 'usage' => $usage
  5201. ];
  5202. }
  5203. public function addChatForAce($data) {
  5204. $uid = Site::getUid();
  5205. $anime_id = getProp($data, 'anime_id');
  5206. if (!$anime_id) Utils::throwError('20003:请选择对话');
  5207. $anime = DB::table('mp_animes')->where('id', $anime_id)->where('is_deleted', 0)->first();
  5208. if (!$anime) Utils::throwError('20003:对话不存在');
  5209. $file = getProp($data, 'file');
  5210. $content = getProp($data, 'content', '');
  5211. $bid = getProp($data, 'bid', 0);
  5212. $script_id = getProp($data, 'script_id', 0);
  5213. $ace_mode = getProp($data, 'ace_mode', 0) ? getProp($data, 'ace_mode', 0) : getProp($anime, 'ace_mode', 0);
  5214. $prompt = getProp($data, 'prompt');
  5215. $is_multi = getProp($anime, 'is_multi');
  5216. $is_single = (int)$is_multi !== 1;
  5217. $extra_products = getProp($data, 'products', []);
  5218. if (!$extra_products) {
  5219. $extra_products = getProp($anime, 'extra_products');
  5220. if ($extra_products) {
  5221. $extra_products = json_decode($extra_products, true);
  5222. }else {
  5223. $extra_products = [];
  5224. }
  5225. }else {
  5226. if (is_string($extra_products)) $extra_products = json_decode($extra_products, true);
  5227. }
  5228. // 如果anime有关联的script_id,则从mp_script_product_mappings中查询资产并合并到extra_products
  5229. $anime_script_id = getProp($anime, 'script_id');
  5230. if ($anime_script_id && $is_single) { // 单剧集模式下才使用全部资产库
  5231. $cpid = Site::getCpid();
  5232. $script_products_mappings = DB::table('mp_script_product_mappings as mapping')
  5233. ->join('mp_products as product', 'mapping.product_id', '=', 'product.id')
  5234. ->where('mapping.script_id', $anime_script_id)
  5235. ->where('product.cpid', $cpid)
  5236. ->where('product.is_deleted', 0);
  5237. $mappings = $script_products_mappings->select(
  5238. 'mapping.product_id',
  5239. 'mapping.episode_number',
  5240. 'product.product_name',
  5241. 'product.type',
  5242. 'product.product',
  5243. 'product.pic_prompt',
  5244. 'product.url',
  5245. 'product.pic_task_id',
  5246. 'product.pic_task_status',
  5247. 'product.three_view_image_url'
  5248. )
  5249. ->get();
  5250. // 按 product_id 分组,合并 episode_number
  5251. $productMap = [];
  5252. foreach ($mappings as $item) {
  5253. $product_id = $item->product_id;
  5254. $product_name = $item->product_name;
  5255. // 处理product_name两边的符号
  5256. $product_name = preg_replace('/^[\s*\[\]【】[]{}{}\x{3000}]+|[\s*\[\]【】[]{}{}\x{3000}]+$/u', '', $product_name);
  5257. if (!isset($productMap[$product_id])) {
  5258. $productMap[$product_id] = [
  5259. 'product_id' => $product_id,
  5260. 'product_name' => $product_name,
  5261. 'type' => (int)$item->type,
  5262. 'product' => (int)$item->product,
  5263. 'pic_prompt' => $item->pic_prompt,
  5264. 'url' => $item->url,
  5265. 'pic_task_id' => $item->pic_task_id,
  5266. 'pic_task_status' => $item->pic_task_status,
  5267. 'episode_numbers' => [],
  5268. ];
  5269. }
  5270. // 添加 episode_number(去重)
  5271. if (!in_array($item->episode_number, $productMap[$product_id]['episode_numbers'])) {
  5272. $productMap[$product_id]['episode_numbers'][] = $item->episode_number;
  5273. }
  5274. }
  5275. // 将查询到的剧本资产合并到extra_products中
  5276. // 先构建extra_products的索引(以product_id为key,用于去重)
  5277. $extraProductsMap = [];
  5278. foreach ($extra_products as $item) {
  5279. $key = getProp($item, 'product_id');
  5280. if ($key) {
  5281. $extraProductsMap[$key] = $item;
  5282. }
  5283. }
  5284. // 合并剧本资产(去重:仅按product_id去重,不考虑episode_number)
  5285. foreach ($productMap as $product) {
  5286. $product_id = $product['product_id'];
  5287. // 如果不存在则添加(不带episode_number信息)
  5288. if (!isset($extraProductsMap[$product_id])) {
  5289. $extraProductsMap[$product_id] = [
  5290. 'product_id' => $product['product_id'],
  5291. 'product_name' => $product['product_name'],
  5292. 'type' => $product['type'],
  5293. 'product' => $product['product'],
  5294. 'pic_prompt' => $product['pic_prompt'],
  5295. 'url' => $product['url'],
  5296. 'pic_task_id' => $product['pic_task_id'],
  5297. 'pic_task_status' => $product['pic_task_status'],
  5298. ];
  5299. }
  5300. }
  5301. // 重新赋值给extra_products
  5302. $extra_products = array_values($extraProductsMap);
  5303. }
  5304. $user_anime_name = getProp($anime, 'anime_name');
  5305. // 处理提示词
  5306. if ($prompt && $extra_products) {
  5307. // 遍历extra_products,将prompt中的{product_name}替换为实际的product_name
  5308. foreach($extra_products as $item) {
  5309. $product_name = getProp($item, 'product_name');
  5310. if ($product_name) {
  5311. // 替换 {product_name} 为 product_name
  5312. $prompt = str_replace('{' . $product_name . '}', $product_name, $prompt);
  5313. }
  5314. }
  5315. }
  5316. // 处理文本模型
  5317. $model = getProp($data, 'model');
  5318. if (!$model) {
  5319. // 用户没有输入,从anime表获取
  5320. $model = getProp($anime, 'model');
  5321. if (!$model) {
  5322. // anime表也没有,使用默认值
  5323. $model = 'deepseek-v4-pro';
  5324. }
  5325. }
  5326. // 验证模型是否在可用模型表中
  5327. if (!DB::table('mp_text_models')->where('model', $model)->where('is_enabled', 1)->exists()) {
  5328. $model = 'deepseek-v4-pro';
  5329. }
  5330. $question = getProp($data, 'question', $is_single ? "请根据实例格式完成单剧集剧本及分段\n" : "请根据实例格式完成剧本大纲\n");
  5331. if ($prompt) {
  5332. $question .= "本次修改要求如下:\n{$prompt}";
  5333. }
  5334. // if (!$file && !$content && !$bid) {
  5335. // Utils::throwError('20003:请上传文件或选择书籍或输入内容');
  5336. // }
  5337. // 提取文件内容
  5338. if ($file) {
  5339. $content = $this->extractFileContent($file);
  5340. if (!$content) {
  5341. Utils::throwError('20003:无法提取文件内容,请检查文件格式');
  5342. }
  5343. } elseif ($script_id) {
  5344. $content = $this->getContentByScriptId($script_id);
  5345. if (!$content) {
  5346. Utils::throwError('20003:无法获取剧本内容,请检查剧本');
  5347. }
  5348. } elseif ($bid) {
  5349. $content = $this->getContentByBid($bid);
  5350. if (!$content) {
  5351. Utils::throwError('20003:无法获取书籍内容,请检查书籍');
  5352. }
  5353. } elseif ($content) {
  5354. $content = filterContent($content);
  5355. } else {
  5356. $content = getProp($anime, 'content');
  5357. if (!$content && $prompt) { // 如果动漫记录中没有内容则根据提示词生成
  5358. $need_generate_content = true;
  5359. }
  5360. }
  5361. // 美术风格
  5362. $input_art_style = getProp($data, 'art_style');
  5363. if (!$input_art_style) {
  5364. $mappedArtStyle = getProp($anime, 'art_style');
  5365. }else {
  5366. $mappedArtStyle = $this->getArtStylePromptByInput($input_art_style);
  5367. }
  5368. // 判断是否需要生成原文内容
  5369. $need_generate_content = isset($need_generate_content) && $need_generate_content;
  5370. // 如果需要生成原文内容,添加额外的要求
  5371. $content_generation_requirement = $need_generate_content
  5372. ? "\n5.如果用户只提供了创作要求而没有提供具体文档内容,你需要根据要求创作完整的原文内容(小说形式,包含对话和叙述),并将原文内容放在最后一个板块###原文内容中输出。原文内容应该详细、生动,如果用户没有明确生成的字数,则字数不少于1000字。"
  5373. : "";
  5374. // 美术风格
  5375. if (!$mappedArtStyle) {
  5376. $mappedArtStyle_prompt = '美术风格:根据剧本内容自定义美术风格';
  5377. }else {
  5378. if (strstr($mappedArtStyle, '基础画风风格词')) {
  5379. $mappedArtStyle_prompt = "美术风格强制设置如下(原样输出,不得更改任何内容):\n{$mappedArtStyle}\n";
  5380. }else {
  5381. $art_style_type = $input_art_style ? $input_art_style : getProp($anime, 'art_style_type');
  5382. $mappedArtStyle_prompt = "美术风格强制设置如下(原样输出,不得更改任何内容):\n基础画风风格词: {$art_style_type}\n{$mappedArtStyle}\n";
  5383. }
  5384. }
  5385. // 构建主体、场景和道具提示词
  5386. $extra_role_prompt = "\n";
  5387. $extra_scene_prompt = "\n";
  5388. $extra_prop_prompt = "\n";
  5389. $ref_products = []; // 参考资产
  5390. if ($extra_products) {
  5391. foreach($extra_products as $item) {
  5392. $product = getProp($item, 'product');
  5393. $product_name = getProp($item, 'product_name');
  5394. if ($product_name == '旁白') continue;
  5395. $ref_products[$product_name] = $item;
  5396. $pic_prompt = getProp($item, 'pic_prompt');
  5397. if ((int)$product === 1) {
  5398. // 拼接角色信息
  5399. $extra_role_prompt .= $product_name . ":" . $pic_prompt . "\n";
  5400. } elseif ((int)$product === 2) {
  5401. // 拼接场景信息
  5402. $extra_scene_prompt .= $product_name . ":" . $pic_prompt . "\n";
  5403. } elseif ((int)$product === 3) {
  5404. // 拼接道具信息
  5405. $extra_prop_prompt .= $product_name . ":" . $pic_prompt . "\n";
  5406. }
  5407. }
  5408. // 优化提示词,融入剧本
  5409. if (!empty(trim($extra_role_prompt))) {
  5410. $extra_role_prompt = "\n2.5【强制主体设定-不可修改】\n以下主体必须全部出现在<主体列表>中,必须包含:\n" . $extra_role_prompt . "\n格式要求:\n1. 主体名称:必须保持不变\n2. 主体描述:你必须根据剧情为每个主体生成主体描述(分析人物在整部剧中的姓名、年龄、性格特征、社会身份或职业背景、个人成长经历或核心故事动机,以及在剧情中的目标与行为驱动力,用一句话进行总结)\n3. 人物提示词:{}中的内容必须完整保留,不得修改\n4. 音色提示词:你必须根据角色特征生成音色提示词(描述角色的整体音色特征,包含:性别、年龄、音质、表达形式、情绪氛围,字数控制在80字以内),用{{}}框起来\n\n最终格式必须为:主体名:[你生成的主体描述]{人物提示词-保持不变}{{你生成的音色提示词}}\n\n【重要】以上每个主体在<主体列表>中的完整格式必须是:主体名:主体描述{人物提示词}{{音色提示词}},其中主体描述和音色提示词是你必须生成的内容,{}中的人物提示词必须原样保留。";
  5411. }
  5412. if (!empty(trim($extra_scene_prompt))) {
  5413. $extra_scene_prompt = "\n3.4【强制场景设定-不可修改】\n以下场景必须全部出现在<场景列表>中,必须包含:\n" . $extra_scene_prompt . "\n格式要求:\n1. 场景名称:必须保持不变\n2. 场景描述:你必须根据剧情为每个场景生成符合<普通要求3.2>的场景描述(分析在整部剧中出现过的场景,场景描述尽量简洁贴合原文)\n3. 场景提示词:{}中的内容必须完整保留,不得修改\n\n最终格式必须为:场景名:[你生成的场景描述]{场景提示词-保持不变}\n\n必须包含的场景列表:\n" . $extra_scene_prompt . "\n【重要提示】以上每个场景在<场景列表>中的完整格式必须是:场景名:场景描述{场景提示词},其中场景描述是你必须生成的内容,{}中的场景提示词必须原样保留。";
  5414. }
  5415. if (!empty(trim($extra_prop_prompt))) {
  5416. $extra_prop_prompt = "\n4.4【强制道具设定-不可修改】\n以下道具必须全部出现在<道具列表>中,必须包含:\n" . $extra_prop_prompt . "\n格式要求:\n1. 道具名称:必须保持不变\n2. 道具描述:你必须根据剧情为每个道具生成道具描述(分析在整部剧中出现过的道具,道具描述尽量简洁贴合原文)\n3. 道具提示词:{}中的内容必须完整保留,不得修改\n\n最终格式必须为:道具名:[你生成的道具描述]{道具提示词-保持不变}\n\n必须包含的道具列表:\n" . $extra_prop_prompt . "\n【重要提示】以上每个道具在<道具列表>中的完整格式必须是:道具名:道具描述{道具提示词},其中道具描述是你必须生成的内容,{}中的道具提示词必须原样保留。";
  5417. }
  5418. }
  5419. $systemPrompt = $is_single
  5420. ? "你是一个专业的文档分析助手及资深编剧,请根据用户提供的文档内容输出单剧集完整策划稿(剧本大纲包括: <故事梗概><剧本亮点><人物关系><核心矛盾><主体列表><美术风格><场景列表><道具列表><分段剧本>,每个板块之间必须使用###分隔,#不能多也不能少)。单剧集模式下,必须一次性输出可直接保存的大纲与完整分镜,不允许只输出大纲。
  5421. 强制要求:
  5422. 1.你只负责与剧本相关的内容,如果用户输入与剧本无关的内容,请告知用户并拒绝继续回答,此为第一优先原则,不满足此原则就忽略其他要求并停止继续输出
  5423. 2.如果用户输入修改美术风格、修改画风这类内容,请告知用户不可修改美术风格,并拒绝继续回答
  5424. 3.以上两条要求对修改主体角色或场景不生效,允许用户调整主体角色说明和场景描述等
  5425. 4.用户如果要求你帮忙写一个故事或者剧本,此类要求不属于与剧本无关,请帮助用户完成请求{$content_generation_requirement}
  5426. 5.强制按照示例格式返回内容,不要更改格式或随意增加符号\n
  5427. 普通要求:
  5428. 1.剧本名(必须生成)和剧集标题必须与文档内容高度相关,(其中剧集序号是1)。
  5429. 2.<主体列表>中出现的“主体名”必须尽可能统一,严禁出现角色-科学家、角色-复仇者、角色-领导者等带有身份、职业、标签类的命名,只能使用服装差异(如校服装、卫衣装、西装)来区分同一角色,如角色-校服装等。
  5430. 2.1<主体列表>分以下三部分(强制格式):主体描述、人物提示词和音色提示词,格式必须为:主体名:主体描述{人物提示词}{{音色提示词}}
  5431. 2.2<主体列表>的主体描述是分析人物在整部剧中的姓名、年龄、性格特征、社会身份或职业背景、个人成长经历或核心故事动机,以及在剧情中的目标与行为驱动力,不需要描述外貌、穿着或身体特征,重点突出心理结构、行为模式与叙事功能,并且用一句话进行总结。
  5432. 2.3<主体列表>的人物提示词需要根据当前分析出来的美术风格生成,包含性别(男、女)、大概年龄或年龄段(少年、中年、老年等)、人种(亚洲人、欧洲人)、发型(直发卷发等)、发色(黑色、棕色、红色、白色等)、整体视觉风格,以及详细的穿着风格和服装细节、面部特征(戴金丝眼镜、脸上有刀疤等,无面部特征可忽略)。描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合人物身份设定。该提示词(字数50-100字左右)在主体描述后用{}框起来。
  5433. 2.4<主体列表>的音色提示词放在描述的最后用{{}}框起来。音色提示词需描述角色的整体音色特征,包含:性别、年龄(约XX岁)、音质(如清冷、沉稳、磁性等)、表达形式(如叙述感、旁白式等)、情绪氛围(如空灵韵味、看透世事的清冷等),字数控制在80字以内。举例:成年女性(约25-35岁),音色清冷且富有叙述感,语速平缓,带有仙侠剧特有的空灵韵味。
  5434. {$extra_role_prompt}
  5435. 3.<场景列表>与主体列表同理,需尽可能的对场景环境进行详细描述,必须严格注意不要出现任何角色和人物。
  5436. 3.1<场景列表>分以下两部分(强制格式):场景描述和场景提示词,格式必须为:场景描述{场景提示词}
  5437. 3.2<场景列表>的场景描述是分析在整部剧中出现过的场景,场景描述尽量简洁贴合原文。
  5438. 3.3<场景列表>的场景提示词用于视觉呈现的场景环境描述,该场景需包含环境氛围、光线效果、空间布局、色彩基调、视觉风格等,描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合剧情设定(可参考以下示例的场景列表中使用{}框起来的提示词,字数150-200字,需带上提炼出来的美术风格)。该提示词在场景描述后用{}框起来。\n
  5439. {$extra_scene_prompt}
  5440. 4<道具列表>与场景列表同理,需尽可能的对道具环境进行详细描述,必须严格注意不要出现任何角色和人物。
  5441. 4.1<道具列表>分以下两部分(强制格式):道具描述和道具提示词,格式必须为:道具描述{道具提示词}
  5442. 4.2<道具列表>的道具描述是分析在整部剧中出现过的道具,道具描述尽量简洁贴合原文。
  5443. 4.3<道具列表>的道具提示词用于视觉呈现的道具环境描述,该道具需包含环境氛围、光线效果、空间布局、色彩基调、视觉风格等,描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合剧情设定(可参考以下示例的道具列表中使用{}框起来的提示词,字数150-200字,需带上提炼出来的美术风格)。该提示词在道具描述后用{}框起来。\n
  5444. {$extra_scene_prompt}
  5445. 5.<分段剧本>按照“分段模式”进行拆分和组织,要求如下:
  5446. 5.1片段分割逻辑(优先级从高到低):
  5447. - 第一优先级:时长上限(单个片段不超过15秒,超过必须切为新片段)
  5448. - 第二优先级:叙事节拍Beat(一个完整的事件/动作/情绪起落单元,如一段对话的来回、一个动作序列:推门→看见→震惊、一次闪回/回忆、旁白+画面组合)
  5449. - 第三优先级:语义块(按“谁、在哪、做什么、情绪怎样”聚合,如同一段旁白/独白、同一个角色的连续动作、同一个视角的连续镜头)
  5450. - 只要事件没结束、情绪没断,哪怕换场景,也留在同一个片段里
  5451. 5.2片段结构:片段数量+旁白音色+多个片段(每个片段由多个分镜组合而成(片段序号格式:片段1、片段2...))
  5452. - 片段数量格式为: 片段数量:8
  5453. - 旁白音色需描述旁白的整体音色特征,包含:性别、年龄(约XX岁)、音质(如清冷、沉稳、磁性等)、表达形式(如叙述感、旁白式等)、情绪氛围(如空灵韵味、看透世事的清冷等),字数控制在80字以内。举例:成年女性(约25-35岁),音色清冷且富有叙述感,语速平缓,带有仙侠剧特有的空灵韵味。
  5454. - 片段序号格式必须如下: ##片段+片段序号\n时长:xxs,例如: ##片段1\n时长:12s
  5455. 5.3分镜结构:每个分镜包含以下字段:
  5456. - 分镜序号在片段内用1、2、3显示,如分镜1、分镜2,并且分镜序号需连续(跨片段中的分镜序号需连续,如片段2中的分镜序号从片段1的最大序号+1开始),分镜序号前后不要带任何其他符号,特别是*符号。
  5457. - 出场角色: 如果有主体出现在本镜,则必须选择本集中的<主体列表>中的一个(如必须使用“沈秋岚-程家居家围裙造型”,而不是“沈秋岚”,不得缺少“-”前后和“:”前面的任何内容,不能自创)
  5458. - 场景:必须选择本集中的<场景列表>中的一个(如需使用“酒店会客室-景池筹备会版”,而不是“酒店会客室”,不得缺少“-”前后和“:”前面的任何内容,不能自创)
  5459. - 出场道具:如果有道具出现在本镜,则必须选择本集中的<道具列表>中的一个(如需使用“酒杯-高脚杯”,而不是“酒杯”或“高脚杯”,不得缺少“-”前后和“:”前面的任何内容,不能自创)
  5460. - 画面:【构图/视角】+画面描述(越简单越好,对视觉场景/实景的描述,不描述氛围,比如紧张、压抑的氛围)。举例:[近景特写,浅景深]季潇握着手机贴在耳边,指关节因为用力而泛白。她的眼眶微红,睫毛在狂风中颤抖,嘴唇紧抿,眼神中闪烁着最后一点希冀的微光。
  5461. - 画面中如果出现主体,只能选择上述<主体列表>中的一个,主体的名称也必须保持一致(如必须使用“沈秋岚-程家居家围裙造型”,而不是“沈秋岚”,不得缺少“-”前后和“:”前面的任何内容,不能自创)
  5462. - 画面中如果出现道具,只能选择上述<道具列表>中的一个,道具的名称也必须保持一致(如必须使用“酒杯-高脚杯”,而不是“酒杯”或“高脚杯”,不得缺少“-”前后和“:”前面的任何内容,不能自创)
  5463. - 运镜:场景+画面描述+运镜
  5464. - 运镜描述中如果出现主体或道具,逻辑需同画面一直,必须选择<主体列表>或<道具列表>中的一个,名称也必须保持一致
  5465. - 配音台词:格式为“中文配音:[角色]台词”,角色配音台词和语言由模型匹配生成,角色必须对应本集中的<主体列表>中的一个(非对话内容则统一固定为“旁白”角色,[角色]的角色名必须完整使用主体列表的主体名,如必须使用“沈秋岚-程家居家围裙造型”,而不是“沈秋岚”,不得缺少“-”前后和“:”前面的任何内容,不能自创,不能自创)
  5466. - 台词内容需满足以下要求: (1. 台词来源规则:台词内容必须逐字摘录自小说原文中的人物直接对话(带引号的内容),不允许改写、总结、压缩、扩写或拼接原文对白,不允许生成原文中不存在的对话。2. 非对话过滤:旁白、心理描写、环境描写、动作描写等内容,禁止出现在台词内容中。3. 空台词规则:如果该分镜中没有人物对话,则“台词内容”必须返回“无”,严禁输出“无台词”、“没有”等任何替代文本。4. 禁止推测:不得根据语境补全人物可能说的话。5.如果没有原文,直接返回空)
  5467. - 背景音效:背景音效需根据内容和画面生成,贴合当前画面描述和动作,例如:背景音效:高跟鞋的脚本声\n
  5468. 6.{$mappedArtStyle_prompt}\n\n
  5469. 示例如下:\n
  5470. ###剧本名:西昆仑
  5471. ###故事梗概
  5472. 内容梗概:许芸归家遭徐母毒打,她冷静周旋并编造谎言掩盖行踪,引得一旁阴沉的卞大伟愈发急躁。半个月后,徐母离家,卞大伟兽性大发欲行不轨。生死关头,隐形的程景现身肉搏,屋内家具横飞,卞大伟被“空气”重创。许芸趁乱逃出,却惊见地面凭空浮现一串血脚印。意识到守护者为救自己负伤,许芸不再逃避,对着虚无的空气执拗告白,誓要与这看不见的爱人共进退。
  5473. ###剧本亮点
  5474. 亮点1:绝境奇药,生死一线
  5475. 画面开场即是阿雪苍白无力的脸庞,梁萧眼中泪光闪烁,急促的呼吸声与心跳声烘托紧张气氛。特写梁萧将“阴阳球”塞入阿雪舌底,球体散发微光,阿雪气息从若有若无到逐渐平稳,生死逆转的瞬间,视觉与听觉冲击力十足。
  5476. 亮点2:结拜兄妹,化解尴尬
  5477. 阿雪因解衣拔刺而羞愤欲绝的特写,梁萧眼神中的挣扎与无奈。在紧迫的背景音下,梁萧急中生智,牵起阿雪颤抖的手,跪地匆匆结拜,阿雪眼神从惊惶到迷茫,最终一句“我……我愿意的”带着哭腔,将救命与羞耻的矛盾推向高潮,又以出人意料的方式解决。
  5478. 亮点3:纤发夺命,情愫暗涌
  5479. 拔刺过程的紧张特写,梁萧指尖小心翼翼地捏住细丝,缓慢抽出。当“问心刺”被完全拔出,特写它竟是一根细长、泛着寒光的女子秀发,诡异而令人震惊。阿雪苏醒后,两人眼神交汇,阿雪低头羞涩,梁萧眼中复杂的情绪,无声地传递出超越兄妹的微妙情愫。
  5480. ###人物关系
  5481. 阿雪与梁萧之间存在兄妹之情。
  5482. ###核心矛盾
  5483. 阿雪与梁萧之间存在兄妹之情,但因情愫暗生而产生微妙的矛盾。阿雪羞涩难当,梁萧情急之下提出结拜兄妹,以化解尴尬。两人匆匆结拜后,梁萧小心翼翼地拔出“问心刺”,竟发现它是一根诡异的女子秀发。阿雪转危为安,两人之间,在生死与羞涩交织的瞬间,情愫暗生,却又被兄妹之名束缚,留下无尽遐想。
  5484. ###主体列表
  5485. 阿雪:身中奇毒“问心刺”,气息奄奄,面色苍白,身体虚弱。{全景,正面拍摄,青年女性,亚洲人。
  5486. 黑色短发扎成利落的高马尾,五官精致且轮廓清晰。
  5487. 服饰穿搭:身着红白相间的专业田径运动背心与超短跑步裤,皮肤呈现健康的小麦色,四肢肌肉线条紧致有力。
  5488. 姿态:站立。}{{青年女性(约25-30岁),音色亲切、温柔,语速平稳缓慢}}
  5489. 梁萧:少年侠士,心系阿雪安危,沉着冷静,内心挣扎于情义之间。{8k超高清,电影级光效,大气透视,中国传统艺术美学,空灵光泽,深度景深,复杂精细的纹理,史诗级构图,电影胶片颗粒。
  5490. 全景,正面拍摄,青年女性,亚洲人。
  5491. 佩戴精致繁复的金丝凤冠,黑色长发整齐盘起。
  5492. 服饰穿搭:身着大红色正装嫁衣,上面绣有精细的金色凤凰纹样,质感厚重华贵。
  5493. 姿态:站立。}{{青年男性(约25-30岁),音色冷峻、低沉,语速平稳缓慢,透着一股拒人于千里之外的孤傲与肃杀。}}
  5494. ###美术风格
  5495. 基础画风风格词:厚涂古风
  5496. 整体采用精致细腻的厚涂古风,色彩浓郁而富有层次感,注重光影对比,营造出古典而富有戏剧张力的氛围。色调以深沉的蓝绿、灰调为主,衬托阿雪病弱的苍白,同时用温暖的金色、琥珀色光晕突出“阴阳球”的生命力。拔刺与结拜场景则以柔和的暖色调为主,辅以人物脸颊的红晕,烘托羞涩与情愫暗生的微妙感。人物造型飘逸,服饰细节考究,背景虚实结合,既有古韵,又不失奇幻色彩。
  5497. ###场景列表
  5498. 林间疗伤地:幽静的古风林间空地,一棵高大古树下,是梁萧为阿雪疗伤的地点。环境清幽,光影斑驳,既有古韵又不失奇幻色彩。{影视质感。广角。
  5499. 宽敞的天然山洞内部,石壁上错落有致地镶嵌着发光的萤石,散发出冷色调的微光,照亮了整个空间。洞顶有钟乳石垂下。光影层次丰富,营造出一种宁静而神圣的氛围。
  5500. 无人物。}
  5501. ###分段剧本
  5502. 片段数量:8
  5503. 旁白音色:成年男性(约35-50岁),音色厚重且富有磁性,带有老电影叙事般的沧桑感,语调低沉平稳,富有江湖气息。
  5504. ##片段1
  5505. 时长:12s
  5506. 分镜1
  5507. 出场角色:刀疤脸
  5508. 场景:边境小镇街道
  5509. 出场道具:酒杯-高脚杯
  5510. 画面:[大远景,冷色调,高对比度] 漫天黄沙在狂风中如怒龙翻滚,遮蔽了微弱的星光。悦来酒馆残破的木门在风中剧烈摇晃,铁链撞击木板发出沉重的闷响,四周建筑尽显荒凉。
  5511. 运镜:大远景固定镜头,随后风沙席卷向镜头形成遮挡转场。
  5512. 配音台词:
  5513. 背景音效:
  5514. 分镜2
  5515. 出场角色:刀疤脸
  5516. 场景:悦来酒馆
  5517. 出场道具:酒杯-高脚杯
  5518. 画面:[全景,暖色调阴影构图] 酒馆内部昏暗,几盏油灯在风中疯狂摇曳,投射出扭曲的长影。角落里的食客低头缩颈,双手紧紧攥着粗瓷碗,空气中漂浮着细小的尘埃。
  5519. 运镜:缓慢横移,掠过阴影中瑟缩的食客。
  5520. 配音台词:
  5521. 背景音效:
  5522. 分镜3
  5523. 出场角色:刀疤脸
  5524. 场景:悦来酒馆
  5525. 出场道具:酒杯-高脚杯
  5526. 画面:[中景,三分法构图] 三名大汉围坐在正中央的圆桌旁,腰间挂着的“武林盟”金色令牌在灯火下闪烁。桌上残羹冷炙横陈,一名大汉正张狂地大笑,脚踩在长凳上。
  5527. 运镜:从中景推向令牌特写。
  5528. 配音台词:
  5529. 背景音效:
  5530. 分镜4
  5531. 出场角色:刀疤脸
  5532. 场景:悦来酒馆
  5533. 出场道具:酒杯-高脚杯
  5534. 画面:[特写,低角度仰拍] 刀疤脸横肉颤动,嘴角挂着轻蔑的笑。他随手倾覆手中的酒碗,琥珀色的液体在空中划过弧线,重重溅在老李打着补丁的布鞋和裤脚上。
  5535. 运镜:极速推向酒水溅起的瞬间。
  5536. 配音台词:
  5537. 背景音效:
  5538. 分镜5
  5539. 出场角色:刀疤脸
  5540. 场景:悦来酒馆
  5541. 出场道具:酒杯-高脚杯
  5542. 画面:[近景,荷兰角倾斜构图] 刀疤脸斜睨着眼,昏暗的灯光勾勒出他脸上狰狞的伤疤。他身体前倾,阴影完全笼罩了对面的老李,眼神如毒蛇般阴冷。
  5543. 运镜:固定机位,利用倾斜构图制造压迫感。
  5544. 配音台词:中文配音:[刀疤脸]老李头,这酒里掺水了吧?
  5545. 背景音效:
  5546. \n\n"
  5547. : "你是一个专业的文档分析助手及资深编剧,请根据用户提供的文档内容及下面提供的示例格式完成剧本大纲(需通过以下几个板块进行回复: <故事梗概><剧本亮点><人物关系><核心矛盾><主体列表><美术风格><场景列表><道具列表>,每个板块之间必须使用###分隔,#不能多也不能少;同时板块之间需满足以下要求:\n
  5548. 强制要求:\n
  5549. 1.你只负责与剧本相关的内容,如果用户输入与剧本无关的内容,请告知用户并拒绝继续回答,此为第一优先原则,不满足此原则就忽略其他要求并停止继续输出
  5550. 2.如果用户输入修改美术风格、修改画风这类内容,请告知用户不可修改美术风格,并拒绝继续回答
  5551. 3.以上两条要求对修改主体角色或场景不生效,允许用户调整主体角色说明和场景描述等
  5552. 4.用户如果要求你帮忙写一个故事或者剧本,此类要求不属于与剧本无关,请帮助用户完成请求{$content_generation_requirement}
  5553. 5.强制按照示例格式返回内容,不要更改格式或随意增加符号\n
  5554. 普通要求:\n
  5555. 1.剧本名(必须生成)必须与文档内容高度相关
  5556. 2.<主体列表>中出现的“主体名”必须尽可能统一,严禁出现角色-科学家、角色-复仇者、角色-领导者等带有身份、职业、标签类的命名,只能使用服装差异(如校服装、卫衣装、西装)来区分同一角色,如角色-校服装等。
  5557. 2.1<主体列表>分以下三部分(强制格式):主体描述、人物提示词和音色提示词,格式必须为:主体名:主体描述{人物提示词}{{音色提示词}}
  5558. 2.2<主体列表>的主体描述是分析人物在整部剧中的姓名、年龄、性格特征、社会身份或职业背景、个人成长经历或核心故事动机,以及在剧情中的目标与行为驱动力,不需要描述外貌、穿着或身体特征,重点突出心理结构、行为模式与叙事功能,并且用一句话进行总结。
  5559. 2.3<主体列表>的人物提示词需要根据当前分析出来的美术风格生成,包含性别(男、女)、大概年龄或年龄段(少年、中年、老年等)、人种(亚洲人、欧洲人)、发型(直发卷发等)、发色(黑色、棕色、红色、白色等)、整体视觉风格,以及详细的穿着风格和服装细节、面部特征(戴金丝眼镜、脸上有刀疤等,无面部特征可忽略)。描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合人物身份设定。该提示词(字数50-100字左右)在主体描述后用{}框起来。
  5560. 2.4<主体列表>的音色提示词放在描述的最后用{{}}框起来。音色提示词需描述角色的整体音色特征,包含:性别、年龄(约XX岁)、音质(如清冷、沉稳、磁性等)、表达形式(如叙述感、旁白式等)、情绪氛围(如空灵韵味、看透世事的清冷等),字数控制在80字以内。举例:成年女性(约25-35岁),音色清冷且富有叙述感,语速平缓,带有仙侠剧特有的空灵韵味。
  5561. {$extra_role_prompt}
  5562. 3.<场景列表>与主体列表同理,需尽可能的对场景环境进行详细描述,必须严格注意不要出现任何角色和人物。
  5563. 3.1<场景列表>分以下两部分(强制格式):场景描述和场景提示词,格式必须为:场景描述{场景提示词}
  5564. 3.2<场景列表>的场景描述是分析在整部剧中出现过的场景,场景描述尽量简洁贴合原文。
  5565. 3.3<场景列表>的场景提示词用于视觉呈现的场景环境描述,该场景需包含环境氛围、光线效果、空间布局、色彩基调、视觉风格等,描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合剧情设定(可参考以下示例的场景列表中使用{}框起来的提示词,字数150-200字,需带上提炼出来的美术风格)。该提示词在场景描述后用{}框起来。\n
  5566. {$extra_scene_prompt}
  5567. 4.<道具列表>与场景列表同理,需尽可能的对道具环境进行详细描述,必须严格注意不要出现任何角色和人物。
  5568. 3.1<道具列表>分以下两部分(强制格式):道具描述和道具提示词,格式必须为:道具描述{道具提示词}
  5569. 3.2<道具列表>的道具描述是分析在整部剧中出现过的道具,道具描述尽量简洁贴合原文。
  5570. 3.3<道具列表>的道具提示词用于视觉呈现的道具环境描述,该道具需包含环境氛围、光线效果、空间布局、色彩基调、视觉风格等,描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合剧情设定(可参考以下示例的道具列表中使用{}框起来的提示词,字数150-200字,需带上提炼出来的美术风格)。该提示词在道具描述后用{}框起来。\n
  5571. {$extra_prop_prompt}
  5572. 5.{$mappedArtStyle_prompt}\n\n
  5573. 示例如下:\n
  5574. ###剧本名:西昆仑
  5575. ###故事梗概
  5576. 内容梗概:许芸归家遭徐母毒打,她冷静周旋并编造谎言掩盖行踪,引得一旁阴沉的卞大伟愈发急躁。半个月后,徐母离家,卞大伟兽性大发欲行不轨。生死关头,隐形的程景现身肉搏,屋内家具横飞,卞大伟被“空气”重创。许芸趁乱逃出,却惊见地面凭空浮现一串血脚印。意识到守护者为救自己负伤,许芸不再逃避,对着虚无的空气执拗告白,誓要与这看不见的爱人共进退。
  5577. ###剧本亮点
  5578. 亮点1:绝境奇药,生死一线
  5579. 画面开场即是阿雪苍白无力的脸庞,梁萧眼中泪光闪烁,急促的呼吸声与心跳声烘托紧张气氛。特写梁萧将“阴阳球”塞入阿雪舌底,球体散发微光,阿雪气息从若有若无到逐渐平稳,生死逆转的瞬间,视觉与听觉冲击力十足。
  5580. 亮点2:结拜兄妹,化解尴尬
  5581. 阿雪因解衣拔刺而羞愤欲绝的特写,梁萧眼神中的挣扎与无奈。在紧迫的背景音下,梁萧急中生智,牵起阿雪颤抖的手,跪地匆匆结拜,阿雪眼神从惊惶到迷茫,最终一句“我……我愿意的”带着哭腔,将救命与羞耻的矛盾推向高潮,又以出人意料的方式解决。
  5582. 亮点3:纤发夺命,情愫暗涌
  5583. 拔刺过程的紧张特写,梁萧指尖小心翼翼地捏住细丝,缓慢抽出。当“问心刺”被完全拔出,特写它竟是一根细长、泛着寒光的女子秀发,诡异而令人震惊。阿雪苏醒后,两人眼神交汇,阿雪低头羞涩,梁萧眼中复杂的情绪,无声地传递出超越兄妹的微妙情愫。
  5584. ###人物关系
  5585. 阿雪与梁萧之间存在兄妹之情。
  5586. ###核心矛盾
  5587. 阿雪与梁萧之间存在兄妹之情,但因情愫暗生而产生微妙的矛盾。阿雪羞涩难当,梁萧情急之下提出结拜兄妹,以化解尴尬。两人匆匆结拜后,梁萧小心翼翼地拔出“问心刺”,竟发现它是一根诡异的女子秀发。阿雪转危为安,两人之间,在生死与羞涩交织的瞬间,情愫暗生,却又被兄妹之名束缚,留下无尽遐想。
  5588. ###主体列表
  5589. 阿雪:身中奇毒“问心刺”,气息奄奄,面色苍白,身体虚弱。{全景,正面拍摄,青年女性,亚洲人。
  5590. 黑色短发扎成利落的高马尾,五官精致且轮廓清晰。
  5591. 服饰穿搭:身着红白相间的专业田径运动背心与超短跑步裤,皮肤呈现健康的小麦色,四肢肌肉线条紧致有力。
  5592. 姿态:站立。}{{青年女性(约25-30岁),音色亲切、温柔,语速平稳缓慢}}
  5593. 梁萧:少年侠士,心系阿雪安危,沉着冷静,内心挣扎于情义之间。{8k超高清,电影级光效,大气透视,中国传统艺术美学,空灵光泽,深度景深,复杂精细的纹理,史诗级构图,电影胶片颗粒。
  5594. 全景,正面拍摄,青年女性,亚洲人。
  5595. 佩戴精致繁复的金丝凤冠,黑色长发整齐盘起。
  5596. 服饰穿搭:身着大红色正装嫁衣,上面绣有精细的金色凤凰纹样,质感厚重华贵。
  5597. 姿态:站立。}{{青年男性(约25-30岁),音色冷峻、低沉,语速平稳缓慢,透着一股拒人于千里之外的孤傲与肃杀。}}
  5598. ###美术风格
  5599. 基础画风风格词:厚涂古风
  5600. 整体采用精致细腻的厚涂古风,色彩浓郁而富有层次感,注重光影对比,营造出古典而富有戏剧张力的氛围。色调以深沉的蓝绿、灰调为主,衬托阿雪病弱的苍白,同时用温暖的金色、琥珀色光晕突出“阴阳球”的生命力。拔刺与结拜场景则以柔和的暖色调为主,辅以人物脸颊的红晕,烘托羞涩与情愫暗生的微妙感。人物造型飘逸,服饰细节考究,背景虚实结合,既有古韵,又不失奇幻色彩。
  5601. ###场景列表
  5602. 林间疗伤地:幽静的古风林间空地,一棵高大古树下,是梁萧为阿雪疗伤的地点。环境清幽,光影斑驳,既有古韵又不失奇幻色彩。{影视质感。广角。
  5603. 宽敞的天然山洞内部,石壁上错落有致地镶嵌着发光的萤石,散发出冷色调的微光,照亮了整个空间。洞顶有钟乳石垂下。光影层次丰富,营造出一种宁静而神圣的氛围。
  5604. 无人物。}
  5605. ###道具列表
  5606. 酒杯-高脚杯:高脚杯,盛有少量酒液。{高脚酒杯,道具多角度视角图,正面、背面、侧面、局部细节,透明玻璃材质,细长杯柄,尺寸标注高24厘米杯口6厘米,白底干净展示,无人物}";
  5607. // 构建消息
  5608. $messages = [
  5609. [
  5610. 'role' => 'system',
  5611. 'content' => $systemPrompt
  5612. ],
  5613. [
  5614. 'role' => 'user',
  5615. 'content' => "以下是文档内容:
  5616. {$content}
  5617. 用户问题:
  5618. {$question}"
  5619. ]
  5620. ];
  5621. // 模型兼容性处理:deepseek-chat 和 deepseek-reasoner 映射到 deepseek-v4-flash
  5622. $thinkingMode = getProp($data, 'thinking', 'disabled'); // 默认非思考模式
  5623. $reasoningEffort = getProp($data, 'reasoning_effort', 'high'); // 默认思考强度
  5624. if ($model === 'deepseek-chat') {
  5625. // deepseek-chat 映射到 deepseek-v4-flash,非思考模式
  5626. $model = 'deepseek-v4-flash';
  5627. $thinkingMode = 'disabled';
  5628. } elseif ($model === 'deepseek-reasoner') {
  5629. // deepseek-reasoner 映射到 deepseek-v4-flash,思考模式
  5630. $model = 'deepseek-v4-flash';
  5631. $thinkingMode = 'enabled';
  5632. }
  5633. $post_data = [
  5634. 'model' => $model,
  5635. 'messages' => $messages,
  5636. // 'max_tokens' => 8192,
  5637. 'temperature' => 0.7,
  5638. 'frequency_penalty' => 0,
  5639. 'presence_penalty' => 0,
  5640. 'response_format' => ['type' => 'text'],
  5641. 'thinking' => ['type'=>$thinkingMode],
  5642. 'stream' => true // 启用流式输出
  5643. ];
  5644. if ($thinkingMode == 'enabled') $post_data['reasoning_effort'] = $reasoningEffort;
  5645. // 根据模型类型选择调用方法
  5646. $fullContent = '';
  5647. $fullReasoningContent = '';
  5648. $usage = [];
  5649. try {
  5650. if (in_array($model, ['deepseek-reasoner', 'deepseek-chat', 'deepseek-v4-flash', 'deepseek-v4-pro'])) {
  5651. // DeepSeek 官方模型使用 DeepSeek API
  5652. $streamGenerator = $this->deepSeekStreamResponse($post_data);
  5653. } else if (in_array($model, $this->gpt_text_models)) {
  5654. // GPT-5.4 模型使用 TokenRouter API
  5655. $streamGenerator = $this->gpt54StreamResponse($post_data);
  5656. } else {
  5657. // 其他模型使用火山引擎API
  5658. $streamGenerator = $this->volcEngineChatCompletion($post_data);
  5659. }
  5660. // 验证返回值类型
  5661. if (!is_iterable($streamGenerator)) {
  5662. $errorType = is_object($streamGenerator) ? get_class($streamGenerator) : gettype($streamGenerator);
  5663. dLog('deepseek')->error('addChat流式生成器无效', [
  5664. 'generator_type' => $errorType,
  5665. 'model' => $model,
  5666. 'anime_id' => $anime_id
  5667. ]);
  5668. logDB('deepseek', 'error', 'addChat流式生成器类型错误', [
  5669. 'type' => $errorType,
  5670. 'model' => $model
  5671. ]);
  5672. yield [
  5673. 'type' => 'error',
  5674. 'script' => [],
  5675. 'episode' => [],
  5676. 'answer' => '',
  5677. 'reasoning' => '',
  5678. 'usage' => [],
  5679. 'error' => '接口返回数据异常,请重新请求'
  5680. ];
  5681. return;
  5682. }
  5683. // 处理流式输出
  5684. foreach ($streamGenerator as $chunk) {
  5685. if (isset($chunk['type'])) {
  5686. if ($chunk['type'] === 'done') {
  5687. // 最终结果
  5688. $fullContent = $chunk['full_content'];
  5689. $fullReasoningContent = $chunk['full_reasoning'];
  5690. $usage = isset($chunk['usage']) ? $chunk['usage'] : [];
  5691. } else {
  5692. // 逐块yield数据
  5693. yield $chunk;
  5694. }
  5695. }
  5696. }
  5697. } catch (\Exception $e) {
  5698. dLog('deepseek')->error('addChat流式处理异常: ' . $e->getMessage(), [
  5699. 'model' => $model,
  5700. 'anime_id' => $anime_id,
  5701. 'trace' => $e->getTraceAsString()
  5702. ]);
  5703. logDB('deepseek', 'error', 'addChat流式处理失败', [
  5704. 'error' => $e->getMessage(),
  5705. 'model' => $model
  5706. ]);
  5707. yield [
  5708. 'type' => 'error',
  5709. 'script' => [],
  5710. 'episode' => [],
  5711. 'answer' => '',
  5712. 'reasoning' => '',
  5713. 'usage' => [],
  5714. 'error' => '处理请求时发生异常: ' . $e->getMessage()
  5715. ];
  5716. return;
  5717. }
  5718. dLog('deepseek')->info('完整内容: '.$fullContent);
  5719. logDB('deepseek', 'info', '完整内容', ['content' => $fullContent, 'anime_id' => $anime_id]);
  5720. // 处理完整内容并返回最终结果
  5721. $script_arr = [];
  5722. if ($fullContent) {
  5723. $script_arr = handleScriptContentForAce($fullContent);
  5724. dLog('deepseek')->info('解析内容', $script_arr);
  5725. logDB('deepseek', 'info', '解析内容', $script_arr);
  5726. }
  5727. if (empty($script_arr['roles'])) {
  5728. // 未生成剧本相关内容,保存对话记录并返回提示
  5729. dLog('deepseek')->info('未生成剧本相关的内容');
  5730. try {
  5731. DB::beginTransaction();
  5732. $now = date('Y-m-d H:i:s');
  5733. // 保存对话记录
  5734. $records = [
  5735. [
  5736. 'uid' => $uid,
  5737. 'anime_id' => $anime_id,
  5738. 'sequence' => 0,
  5739. 'role' => 'user',
  5740. 'content' => $prompt,
  5741. 'created_at' => $now,
  5742. 'updated_at' => $now
  5743. ],
  5744. [
  5745. 'uid' => $uid,
  5746. 'anime_id' => $anime_id,
  5747. 'sequence' => 0,
  5748. 'role' => 'assistant',
  5749. // 'content' => $fullContent,
  5750. 'content' => '未生成剧本相关的内容,请调整提示词再试',
  5751. 'created_at' => $now,
  5752. 'updated_at' => $now
  5753. ]
  5754. ];
  5755. DB::table('mp_anime_records')->insert($records);
  5756. DB::commit();
  5757. } catch (\Exception $e) {
  5758. DB::rollBack();
  5759. dLog('deepseek')->error('保存对话记录失败', ['error' => $e->getMessage()]);
  5760. }
  5761. yield [
  5762. 'type' => 'done',
  5763. 'script' => [],
  5764. 'episode' => [],
  5765. 'answer' => $fullContent,
  5766. 'reasoning' => $fullReasoningContent,
  5767. 'usage' => $usage,
  5768. 'error' => '未生成剧本相关的内容,请调整提示词再试'
  5769. ];
  5770. return;
  5771. }
  5772. // 如果需要生成原文内容,从script_arr中提取
  5773. if ($need_generate_content && isset($script_arr['content']) && $script_arr['content']) {
  5774. $content = $script_arr['content'];
  5775. dLog('deepseek')->info('从AI生成结果中提取原文内容', ['content_length' => strlen($content)]);
  5776. if (!$content) {
  5777. yield [
  5778. 'type' => 'done',
  5779. 'script' => [],
  5780. 'episode' => [],
  5781. 'answer' => $fullContent,
  5782. 'reasoning' => $fullReasoningContent,
  5783. 'usage' => $usage,
  5784. 'error' => '未生成剧本内容,请调整提示词再试'
  5785. ];
  5786. return;
  5787. }
  5788. }
  5789. // 替换美术风格
  5790. if ($mappedArtStyle !== '') {
  5791. $script_arr['art_style'] = $mappedArtStyle;
  5792. $fullContent = $this->replaceArtStyleSection($fullContent, $mappedArtStyle);
  5793. }
  5794. // 新建动漫
  5795. if ($user_anime_name == '新剧本策划') {
  5796. $anime_name = getProp($script_arr, 'script_name');
  5797. // if (!$anime_name) {
  5798. // // 未识别到剧本名,保存对话记录并返回提示
  5799. // dLog('deepseek')->info('未能识别到剧本名称');
  5800. // try {
  5801. // DB::beginTransaction();
  5802. // $now = date('Y-m-d H:i:s');
  5803. // // 保存对话记录
  5804. // $records = [
  5805. // [
  5806. // 'uid' => $uid,
  5807. // 'anime_id' => $anime_id,
  5808. // 'sequence' => 0,
  5809. // 'role' => 'user',
  5810. // 'content' => $prompt,
  5811. // 'created_at' => $now,
  5812. // 'updated_at' => $now
  5813. // ],
  5814. // [
  5815. // 'uid' => $uid,
  5816. // 'anime_id' => $anime_id,
  5817. // 'sequence' => 0,
  5818. // 'role' => 'assistant',
  5819. // 'content' => '未能生成剧本名称,请重试',
  5820. // 'created_at' => $now,
  5821. // 'updated_at' => $now
  5822. // ]
  5823. // ];
  5824. // DB::table('mp_anime_records')->insert($records);
  5825. // DB::commit();
  5826. // } catch (\Exception $e) {
  5827. // DB::rollBack();
  5828. // dLog('deepseek')->error('保存对话记录失败', ['error' => $e->getMessage()]);
  5829. // }
  5830. // yield [
  5831. // 'type' => 'done',
  5832. // 'script' => [],
  5833. // 'episode' => [],
  5834. // 'answer' => $fullContent,
  5835. // 'reasoning' => $fullReasoningContent,
  5836. // 'usage' => $usage,
  5837. // 'error' => '未能识别到剧本名称,请调整提示词再试'
  5838. // ];
  5839. // return;
  5840. // }
  5841. // 确认对话名称唯一性
  5842. if ($anime_name && DB::table('mp_animes')->where('anime_name', $anime_name)->value('id')) {
  5843. $anime_name = $anime_name . '_' . date('YmdHis');
  5844. }
  5845. }else {
  5846. $anime_name = $user_anime_name;
  5847. }
  5848. $table_data = [
  5849. 'user_id' => $uid,
  5850. 'anime_name' => $anime_name,
  5851. 'model' => $model,
  5852. 'intro' => isset($script_arr['intro']) ? $script_arr['intro'] : '',
  5853. 'highlights' => isset($script_arr['highlights']) ? $script_arr['highlights'] : '',
  5854. 'role_relationship' => isset($script_arr['role_relationship']) ? $script_arr['role_relationship'] : '',
  5855. 'core_contradiction' => isset($script_arr['core_contradiction']) ? $script_arr['core_contradiction'] : '',
  5856. 'roles' => isset($script_arr['roles']) && is_array($script_arr['roles']) ? json_encode($script_arr['roles'], 256) : '[]',
  5857. 'art_style' => isset($script_arr['art_style']) ? $script_arr['art_style'] : '',
  5858. 'art_style_type' => $input_art_style,
  5859. 'scenes' => isset($script_arr['scenes']) && is_array($script_arr['scenes']) ? json_encode($script_arr['scenes'], 256) : '[]',
  5860. 'props' => isset($script_arr['props']) && is_array($script_arr['props']) ? json_encode($script_arr['props'], 256) : '[]',
  5861. 'status' => '解析完成',
  5862. 'content' => $content,
  5863. 'is_multi' => $is_multi,
  5864. 'ace_mode' => $ace_mode,
  5865. 'generate_status' => '待执行',
  5866. 'updated_at' => date('Y-m-d H:i:s')
  5867. ];
  5868. if ($table_data['content']) {
  5869. $chapters = $this->splitContent($table_data['content']);
  5870. $chapter_count = count($chapters);
  5871. if ($chapter_count > 0) {
  5872. $table_data['start_episode_sequence'] = 1;
  5873. $table_data['end_episode_sequence'] = $table_data['episode_num'] = $chapter_count;
  5874. }
  5875. }
  5876. if ($extra_products) {
  5877. $table_data['extra_products'] = json_encode($extra_products, 256);
  5878. }
  5879. // 如果有剧本ID则进行绑定
  5880. if ($script_id) {
  5881. $table_data['script_id'] = $script_id;
  5882. }
  5883. $single_episode = [];
  5884. try {
  5885. DB::beginTransaction();
  5886. $now = date('Y-m-d H:i:s');
  5887. // 更新动漫大纲
  5888. $boolen = DB::table('mp_animes')->where('id', $anime_id)->update($table_data);
  5889. if (!$boolen) {
  5890. dLog('deepseek')->info('动漫保存失败', $table_data);
  5891. Utils::throwError('20003:动漫保存失败');
  5892. }
  5893. // 保存对话记录
  5894. $records = [
  5895. [
  5896. 'uid' => $uid,
  5897. 'anime_id' => $anime_id,
  5898. 'sequence' => 0,
  5899. 'role' => 'user',
  5900. 'content' => $prompt,
  5901. 'created_at' => date('Y-m-d H:i:s'),
  5902. 'updated_at' => date('Y-m-d H:i:s')
  5903. ],
  5904. [
  5905. 'uid' => $uid,
  5906. 'anime_id' => $anime_id,
  5907. 'sequence' => 0,
  5908. 'role' => 'assistant',
  5909. 'content' => $fullContent,
  5910. 'created_at' => date('Y-m-d H:i:s'),
  5911. 'updated_at' => date('Y-m-d H:i:s')
  5912. ]
  5913. ];
  5914. // 保存对话记录
  5915. $boolen3 = DB::table('mp_anime_records')->insert($records);
  5916. if (!$boolen3) {
  5917. Utils::throwError('20003:对话记录保存失败');
  5918. }
  5919. if ($is_single) {
  5920. $episode_arr = $this->buildSingleEpisodeFromScript($script_arr, $fullContent, $anime_name);
  5921. if (empty($episode_arr['acts'])) {
  5922. Utils::throwError('20003:单剧集分镜内容解析失败,请确认输出格式');
  5923. }
  5924. $saveResult = $this->saveEpisodeVersionData(
  5925. $anime_id,
  5926. 1,
  5927. $episode_arr,
  5928. is_array(getProp($script_arr, 'roles')) ? getProp($script_arr, 'roles') : [],
  5929. is_array(getProp($script_arr, 'scenes')) ? getProp($script_arr, 'scenes') : [],
  5930. is_array(getProp($script_arr, 'props')) ? getProp($script_arr, 'props') : [],
  5931. null,
  5932. null,
  5933. false,
  5934. $content,
  5935. $now,
  5936. $ref_products
  5937. );
  5938. $single_episode = $saveResult['episode'];
  5939. $episode_id = $saveResult['episode_id'];
  5940. $boolen5 = DB::table('mp_animes')->where('id', $anime_id)->update([
  5941. 'roles' => json_encode($saveResult['merged_roles'], 256),
  5942. 'scenes' => json_encode($saveResult['merged_scenes'], 256),
  5943. 'props' => json_encode($saveResult['merged_props'], 256),
  5944. 'updated_at' => $now
  5945. ]);
  5946. if ($boolen5 === false) {
  5947. Utils::throwError('20003:单剧集主表资源同步失败');
  5948. }
  5949. $table_data['roles'] = json_encode($saveResult['merged_roles'], 256);
  5950. $table_data['scenes'] = json_encode($saveResult['merged_scenes'], 256);
  5951. $table_data['props'] = json_encode($saveResult['merged_props'], 256);
  5952. $episode_record_content = '确认分镜大纲';
  5953. if ($content !== '') {
  5954. // $episode_record_content .= "\n\n###上传内容\n" . $content;
  5955. }
  5956. $episode_records = [
  5957. [
  5958. 'uid' => $uid,
  5959. 'anime_id' => $anime_id,
  5960. 'role' => 'user',
  5961. 'content' => $episode_record_content,
  5962. 'sequence' => 1,
  5963. 'episode_id' => $episode_id,
  5964. 'created_at' => $now,
  5965. 'updated_at' => $now
  5966. ],
  5967. [
  5968. 'uid' => $uid,
  5969. 'anime_id' => $anime_id,
  5970. 'role' => 'assistant',
  5971. 'content' => $fullContent,
  5972. // 'content' => '我已按照您的要求完成策划并将内容更新到您右侧的策划文档。',
  5973. 'sequence' => 1,
  5974. 'episode_id' => $episode_id,
  5975. 'created_at' => $now,
  5976. 'updated_at' => $now
  5977. ]
  5978. ];
  5979. $boolen4 = DB::table('mp_anime_records')->insert($episode_records);
  5980. if (!$boolen4) {
  5981. Utils::throwError('20003:单剧集分镜记录保存失败');
  5982. }
  5983. }
  5984. }catch (\Exception $e) {
  5985. DB::rollBack();
  5986. dLog('deepseek')->info('新建对话错误信息: '.$e->getMessage());
  5987. yield [
  5988. 'type' => 'done',
  5989. 'answer' => $fullContent,
  5990. 'reasoning' => $fullReasoningContent,
  5991. 'usage' => $usage,
  5992. 'error' => $e->getMessage(),
  5993. ];
  5994. return;
  5995. }
  5996. DB::commit();
  5997. dLog('deepseek')->info('保存完毕');
  5998. if ($is_single && !empty($single_episode)) {
  5999. // $this->batchSetGlobalRoleImg([
  6000. // 'anime_id' => $anime_id,
  6001. // ]);
  6002. // $this->batchSetGlobalSceneImg([
  6003. // 'anime_id' => $anime_id,
  6004. // ]);
  6005. // // 将全局角色和场景的task_id、task_status和url继承给当前分集
  6006. // $this->inheritGlobalImagesToEpisodes($anime_id, $episode_id);
  6007. }
  6008. $table_data['anime_id'] = $anime_id;
  6009. $table_data['roles'] = json_decode($table_data['roles'], true);
  6010. $table_data['scenes'] = json_decode($table_data['scenes'], true);
  6011. // $table_data['episodes'] = $script_arr['episodes'];
  6012. unset($table_data['created_at']);
  6013. unset($table_data['updated_at']);
  6014. unset($table_data['status']);
  6015. dLog('deepseek')->info('开始返回');
  6016. yield [
  6017. 'type' => 'done',
  6018. 'script' => $table_data,
  6019. 'episode' => $single_episode,
  6020. 'answer' => $fullContent,
  6021. 'reasoning' => $fullReasoningContent,
  6022. 'usage' => $usage
  6023. ];
  6024. }
  6025. public function reGenerateAnimeForAce($data) {
  6026. $uid = Site::getUid();
  6027. $file = getProp($data, 'file');
  6028. $content = getProp($data, 'content', '');
  6029. $bid = getProp($data, 'bid', 0);
  6030. $script_id = getProp($data, 'script_id', 0);
  6031. $anime_id = getProp($data, 'anime_id');
  6032. $anime = DB::table('mp_animes')->where('id', $anime_id)->first();
  6033. if (!$anime) {
  6034. Utils::throwError('20003:该对话不存在');
  6035. }
  6036. $is_multi = getProp($anime, 'is_multi', 1);
  6037. $is_single = (int)$is_multi !== 1;
  6038. $extra_products = getProp($data, 'products', []);
  6039. if (!$extra_products) {
  6040. $extra_products = getProp($anime, 'extra_products');
  6041. if ($extra_products) {
  6042. $extra_products = json_decode($extra_products, true);
  6043. }else {
  6044. $extra_products = [];
  6045. }
  6046. }else {
  6047. if (is_string($extra_products)) $extra_products = json_decode($extra_products, true);
  6048. }
  6049. // 如果anime有关联的script_id,则从mp_script_product_mappings中查询资产并合并到extra_products
  6050. $anime_script_id = getProp($anime, 'script_id');
  6051. if ($anime_script_id && $is_single) {
  6052. $cpid = Site::getCpid();
  6053. $script_products_mappings = DB::table('mp_script_product_mappings as mapping')
  6054. ->join('mp_products as product', 'mapping.product_id', '=', 'product.id')
  6055. ->where('mapping.script_id', $anime_script_id)
  6056. ->where('product.cpid', $cpid)
  6057. ->where('product.is_deleted', 0);
  6058. $mappings = $script_products_mappings->select(
  6059. 'mapping.product_id',
  6060. 'mapping.episode_number',
  6061. 'product.product_name',
  6062. 'product.type',
  6063. 'product.product',
  6064. 'product.pic_prompt',
  6065. 'product.url',
  6066. 'product.pic_task_id',
  6067. 'product.pic_task_status',
  6068. 'product.three_view_image_url'
  6069. )
  6070. ->get();
  6071. // 按 product_id 分组,合并 episode_number
  6072. $productMap = [];
  6073. foreach ($mappings as $item) {
  6074. $product_id = $item->product_id;
  6075. $product_name = $item->product_name;
  6076. // 处理product_name两边的符号
  6077. $product_name = preg_replace('/^[\s*\[\]【】[]{}{}\x{3000}]+|[\s*\[\]【】[]{}{}\x{3000}]+$/u', '', $product_name);
  6078. if (!isset($productMap[$product_id])) {
  6079. $productMap[$product_id] = [
  6080. 'product_id' => $product_id,
  6081. 'product_name' => $product_name,
  6082. 'type' => (int)$item->type,
  6083. 'product' => (int)$item->product,
  6084. 'pic_prompt' => $item->pic_prompt,
  6085. 'url' => $item->url,
  6086. 'pic_task_id' => $item->pic_task_id,
  6087. 'pic_task_status' => $item->pic_task_status,
  6088. 'episode_numbers' => [],
  6089. ];
  6090. }
  6091. // 添加 episode_number(去重)
  6092. if (!in_array($item->episode_number, $productMap[$product_id]['episode_numbers'])) {
  6093. $productMap[$product_id]['episode_numbers'][] = $item->episode_number;
  6094. }
  6095. }
  6096. // 将查询到的剧本资产合并到extra_products中
  6097. // 先构建extra_products的索引(以product_id为key,用于去重)
  6098. $extraProductsMap = [];
  6099. foreach ($extra_products as $item) {
  6100. $key = getProp($item, 'product_id');
  6101. if ($key) {
  6102. $extraProductsMap[$key] = $item;
  6103. }
  6104. }
  6105. // 合并剧本资产(去重:仅按product_id去重,不考虑episode_number)
  6106. foreach ($productMap as $product) {
  6107. $product_id = $product['product_id'];
  6108. // 如果不存在则添加(不带episode_number信息)
  6109. if (!isset($extraProductsMap[$product_id])) {
  6110. $extraProductsMap[$product_id] = [
  6111. 'product_id' => $product['product_id'],
  6112. 'product_name' => $product['product_name'],
  6113. 'type' => $product['type'],
  6114. 'product' => $product['product'],
  6115. 'pic_prompt' => $product['pic_prompt'],
  6116. 'url' => $product['url'],
  6117. 'pic_task_id' => $product['pic_task_id'],
  6118. 'pic_task_status' => $product['pic_task_status'],
  6119. ];
  6120. }
  6121. }
  6122. // 重新赋值给extra_products
  6123. $extra_products = array_values($extraProductsMap);
  6124. }
  6125. $user_anime_name = getProp($anime, 'anime_name');
  6126. $prompt = getProp($data, 'prompt');
  6127. // 处理提示词
  6128. if ($prompt && $extra_products) {
  6129. // 遍历extra_products,将prompt中的{product_name}替换为实际的product_name
  6130. foreach($extra_products as $item) {
  6131. $product_name = getProp($item, 'product_name');
  6132. if ($product_name) {
  6133. // 替换 {product_name} 为 product_name
  6134. $prompt = str_replace('{' . $product_name . '}', $product_name, $prompt);
  6135. }
  6136. }
  6137. }
  6138. // 处理文本模型
  6139. $model = getProp($data, 'model');
  6140. if (!$model) {
  6141. // 用户没有输入,从anime表获取
  6142. $model = getProp($anime, 'model');
  6143. if (!$model) {
  6144. // anime表也没有,使用默认值
  6145. $model = 'deepseek-v4-pro';
  6146. }
  6147. }
  6148. // 验证模型是否在可用模型表中
  6149. if (!DB::table('mp_text_models')->where('model', $model)->where('is_enabled', 1)->exists()) {
  6150. $model = 'deepseek-v4-pro';
  6151. }
  6152. // 单剧集模式下,如果已经生成了剧集,则不允许继续调整大纲
  6153. if ($is_single) {
  6154. $episode_exists = DB::table('mp_anime_episodes')
  6155. ->where('anime_id', $anime_id)
  6156. ->where('sequence', 1)
  6157. ->exists();
  6158. if ($episode_exists) {
  6159. Utils::throwError('20003:单剧集模式下已生成剧集,不可继续调整大纲,请去策划剧集');
  6160. }
  6161. }
  6162. $question = getProp($data, 'question', $is_single ? "请根据实例格式完成单剧集剧本及分段\n" : "请根据实例格式完成剧本大纲\n");
  6163. if ($prompt) {
  6164. $question .= "本次修改要求如下:\n{$prompt}";
  6165. }
  6166. // 提取文件内容
  6167. if ($file) {
  6168. $content = $this->extractFileContent($file);
  6169. if (!$content) {
  6170. Utils::throwError('20003:无法提取文件内容,请检查文件格式');
  6171. }
  6172. } elseif ($script_id) {
  6173. $content = $this->getContentByScriptId($script_id);
  6174. if (!$content) {
  6175. Utils::throwError('20003:无法获取剧本内容,请检查剧本');
  6176. }
  6177. } elseif ($bid) {
  6178. $content = $this->getContentByBid($bid);
  6179. if (!$content) {
  6180. Utils::throwError('20003:无法获取书籍内容,请检查书籍');
  6181. }
  6182. } elseif ($content) {
  6183. $content = filterContent($content);
  6184. }else {
  6185. $content = filterContent(getProp($anime, 'content'));
  6186. if (!$content && $prompt) { // 如果动漫记录中没有内容则根据提示词生成
  6187. $need_generate_content = true;
  6188. }
  6189. }
  6190. // 判断是否需要生成原文内容
  6191. $need_generate_content = isset($need_generate_content) && $need_generate_content;
  6192. // 如果需要生成原文内容,添加额外的要求
  6193. $content_generation_requirement = $need_generate_content
  6194. ? "\n5.如果用户只提供了创作要求而没有提供具体文档内容,你需要根据要求创作完整的原文内容(小说形式,包含对话和叙述),并将原文内容放在最后一个板块###原文内容中输出。原文内容应该详细、生动,如果用户没有明确生成的字数,则字数不少于1000字。"
  6195. : "";
  6196. // 美术风格
  6197. $input_art_style = getProp($data, 'art_style');
  6198. if (!$input_art_style) {
  6199. $mappedArtStyle = getProp($anime, 'art_style');
  6200. }else {
  6201. $mappedArtStyle = $this->getArtStylePromptByInput($input_art_style);
  6202. }
  6203. if (!$mappedArtStyle) {
  6204. $mappedArtStyle_prompt = '美术风格:根据剧本内容自定义美术风格';
  6205. }else {
  6206. if (strstr($mappedArtStyle, '基础画风风格词')) {
  6207. $mappedArtStyle_prompt = "美术风格强制设置如下(原样输出,不得更改任何内容):\n{$mappedArtStyle}\n";
  6208. }else {
  6209. $art_style_type = $input_art_style ? $input_art_style : getProp($anime, 'art_style_type');
  6210. $mappedArtStyle_prompt = "美术风格强制设置如下(原样输出,不得更改任何内容):\n基础画风风格词: {$art_style_type}\n{$mappedArtStyle}\n";
  6211. }
  6212. }
  6213. // 构建主体、场景和道具提示词
  6214. $extra_role_prompt = "\n";
  6215. $extra_scene_prompt = "\n";
  6216. $extra_prop_prompt = "\n";
  6217. $ref_products = []; // 参考资产
  6218. if ($extra_products) {
  6219. foreach($extra_products as $item) {
  6220. $product = getProp($item, 'product');
  6221. $product_name = getProp($item, 'product_name');
  6222. if ($product_name == '旁白') continue;
  6223. $ref_products[$product_name] = $item;
  6224. $pic_prompt = getProp($item, 'pic_prompt');
  6225. if ((int)$product === 1) {
  6226. // 拼接角色信息
  6227. $extra_role_prompt .= $product_name . ":" . $pic_prompt . "\n";
  6228. } elseif ((int)$product === 2) {
  6229. // 拼接场景信息
  6230. $extra_scene_prompt .= $product_name . ":" . $pic_prompt . "\n";
  6231. } elseif ((int)$product === 3) {
  6232. // 拼接道具信息
  6233. $extra_prop_prompt .= $product_name . ":" . $pic_prompt . "\n";
  6234. }
  6235. }
  6236. // 优化提示词,融入剧本
  6237. if (!empty(trim($extra_role_prompt))) {
  6238. $extra_role_prompt = "\n2.5【强制主体设定-不可修改】\n以下主体必须全部出现在<主体列表>中,必须包含:\n" . $extra_role_prompt . "\n格式要求:\n1. 主体名称:必须保持不变\n2. 主体描述:你必须根据剧情为每个主体生成主体描述(分析人物在整部剧中的姓名、年龄、性格特征、社会身份或职业背景、个人成长经历或核心故事动机,以及在剧情中的目标与行为驱动力,用一句话进行总结)\n3. 人物提示词:{}中的内容必须完整保留,不得修改\n4. 音色提示词:你必须根据角色特征生成音色提示词(描述角色的整体音色特征,包含:性别、年龄、音质、表达形式、情绪氛围,字数控制在80字以内),用{{}}框起来\n\n最终格式必须为:主体名:[你生成的主体描述]{人物提示词-保持不变}{{你生成的音色提示词}}\n\n【重要】以上每个主体在<主体列表>中的完整格式必须是:主体名:主体描述{人物提示词}{{音色提示词}},其中主体描述和音色提示词是你必须生成的内容,{}中的人物提示词必须原样保留。";
  6239. }
  6240. if (!empty(trim($extra_scene_prompt))) {
  6241. $extra_scene_prompt = "\n3.4【强制场景设定-不可修改】\n以下场景必须全部出现在<场景列表>中,必须包含:\n" . $extra_scene_prompt . "\n格式要求:\n1. 场景名称:必须保持不变\n2. 场景描述:你必须根据剧情为每个场景生成符合<普通要求3.2>的场景描述(分析在整部剧中出现过的场景,场景描述尽量简洁贴合原文)\n3. 场景提示词:{}中的内容必须完整保留,不得修改\n\n最终格式必须为:场景名:[你生成的场景描述]{场景提示词-保持不变}\n\n必须包含的场景列表:\n" . $extra_scene_prompt . "\n【重要提示】以上每个场景在<场景列表>中的完整格式必须是:场景名:场景描述{场景提示词},其中场景描述是你必须生成的内容,{}中的场景提示词必须原样保留。";
  6242. }
  6243. if (!empty(trim($extra_prop_prompt))) {
  6244. $extra_prop_prompt = "\n4.4【强制道具设定-不可修改】\n以下道具必须全部出现在<道具列表>中,必须包含:\n" . $extra_prop_prompt . "\n格式要求:\n1. 道具名称:必须保持不变\n2. 道具描述:你必须根据剧情为每个道具生成道具描述(分析在整部剧中出现过的道具,道具描述尽量简洁贴合原文)\n3. 道具提示词:{}中的内容必须完整保留,不得修改\n\n最终格式必须为:道具名:[你生成的道具描述]{道具提示词-保持不变}\n\n必须包含的道具列表:\n" . $extra_prop_prompt . "\n【重要提示】以上每个道具在<道具列表>中的完整格式必须是:道具名:道具描述{道具提示词},其中道具描述是你必须生成的内容,{}中的道具提示词必须原样保留。";
  6245. }
  6246. }
  6247. $system_message = ['role'=>'system', 'content'=>$is_single
  6248. ? "你是一个专业的文档分析助手及资深编剧,请根据用户提供的文档内容输出单剧集完整策划稿(剧本大纲包括: <故事梗概><剧本亮点><人物关系><核心矛盾><主体列表><美术风格><场景列表><道具列表><分段剧本>,每个板块之间必须使用###分隔,#不能多也不能少)。单剧集模式下,必须一次性输出可直接保存的大纲与完整分镜,不允许只输出大纲。
  6249. 强制要求:
  6250. 1.你只负责与剧本相关的内容,如果用户输入与剧本无关的内容,请告知用户并拒绝继续回答,此为第一优先原则,不满足此原则就忽略其他要求并停止继续输出
  6251. 2.如果用户输入修改美术风格、修改画风这类内容,请告知用户不可修改美术风格,并拒绝继续回答
  6252. 3.以上两条要求对修改主体角色或场景不生效,允许用户调整主体角色说明和场景描述等
  6253. 4.用户如果要求你帮忙写一个故事或者剧本,此类要求不属于与剧本无关,请帮助用户完成请求{$content_generation_requirement}
  6254. 5.强制按照示例格式返回内容,不要更改格式或随意增加符号\n
  6255. 普通要求:
  6256. 1.剧本名(必须生成)和剧集标题必须与文档内容高度相关,(其中剧集序号是1)。
  6257. 2.<主体列表>中出现的“主体名”必须尽可能统一,严禁出现角色-科学家、角色-复仇者、角色-领导者等带有身份、职业、标签类的命名,只能使用服装差异(如校服装、卫衣装、西装)来区分同一角色,如角色-校服装等。
  6258. 2.1<主体列表>分以下三部分(强制格式):主体描述、人物提示词和音色提示词,格式必须为:主体名:主体描述{人物提示词}{{音色提示词}}
  6259. 2.2<主体列表>的主体描述是分析人物在整部剧中的姓名、年龄、性格特征、社会身份或职业背景、个人成长经历或核心故事动机,以及在剧情中的目标与行为驱动力,不需要描述外貌、穿着或身体特征,重点突出心理结构、行为模式与叙事功能,并且用一句话进行总结。
  6260. 2.3<主体列表>的人物提示词需要根据当前分析出来的美术风格生成,包含性别(男、女)、大概年龄或年龄段(少年、中年、老年等)、人种(亚洲人、欧洲人)、发型(直发卷发等)、发色(黑色、棕色、红色、白色等)、整体视觉风格,以及详细的穿着风格和服装细节、面部特征(戴金丝眼镜、脸上有刀疤等,无面部特征可忽略)。描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合人物身份设定。该提示词(字数50-100字左右)在主体描述后用{}框起来。
  6261. 2.4<主体列表>的音色提示词放在描述的最后用{{}}框起来。音色提示词需描述角色的整体音色特征,包含:性别、年龄(约XX岁)、音质(如清冷、沉稳、磁性等)、表达形式(如叙述感、旁白式等)、情绪氛围(如空灵韵味、看透世事的清冷等),字数控制在80字以内。举例:成年女性(约25-35岁),音色清冷且富有叙述感,语速平缓,带有仙侠剧特有的空灵韵味。
  6262. {$extra_role_prompt}
  6263. 3.<场景列表>与主体列表同理,需尽可能的对场景环境进行详细描述,必须严格注意不要出现任何角色和人物。
  6264. 3.1<场景列表>分以下两部分(强制格式):场景描述和场景提示词,格式必须为:场景描述{场景提示词}
  6265. 3.2<场景列表>的场景描述是分析在整部剧中出现过的场景,场景描述尽量简洁贴合原文。
  6266. 3.3<场景列表>的场景提示词用于视觉呈现的场景环境描述,该场景需包含环境氛围、光线效果、空间布局、色彩基调、视觉风格等,描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合剧情设定(可参考以下示例的场景列表中使用{}框起来的提示词,字数150-200字,需带上提炼出来的美术风格)。该提示词在场景描述后用{}框起来。\n
  6267. {$extra_scene_prompt}
  6268. 4<道具列表>与场景列表同理,需尽可能的对道具环境进行详细描述,必须严格注意不要出现任何角色和人物。
  6269. 4.1<道具列表>分以下两部分(强制格式):道具描述和道具提示词,格式必须为:道具描述{道具提示词}
  6270. 4.2<道具列表>的道具描述是分析在整部剧中出现过的道具,道具描述尽量简洁贴合原文。
  6271. 4.3<道具列表>的道具提示词用于视觉呈现的道具环境描述,该道具需包含环境氛围、光线效果、空间布局、色彩基调、视觉风格等,描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合剧情设定(可参考以下示例的道具列表中使用{}框起来的提示词,字数150-200字,需带上提炼出来的美术风格)。该提示词在道具描述后用{}框起来。\n
  6272. {$extra_scene_prompt}
  6273. 5.<分段剧本>按照“分段模式”进行拆分和组织,要求如下:
  6274. 5.1片段分割逻辑(优先级从高到低):
  6275. - 第一优先级:时长上限(单个片段不超过15秒,超过必须切为新片段)
  6276. - 第二优先级:叙事节拍Beat(一个完整的事件/动作/情绪起落单元,如一段对话的来回、一个动作序列:推门→看见→震惊、一次闪回/回忆、旁白+画面组合)
  6277. - 第三优先级:语义块(按“谁、在哪、做什么、情绪怎样”聚合,如同一段旁白/独白、同一个角色的连续动作、同一个视角的连续镜头)
  6278. - 只要事件没结束、情绪没断,哪怕换场景,也留在同一个片段里
  6279. 5.2片段结构:片段数量+旁白音色+多个片段(每个片段由多个分镜组合而成(片段序号格式:片段1、片段2...))
  6280. - 片段数量格式为: 片段数量:8
  6281. - 旁白音色需描述旁白的整体音色特征,包含:性别、年龄(约XX岁)、音质(如清冷、沉稳、磁性等)、表达形式(如叙述感、旁白式等)、情绪氛围(如空灵韵味、看透世事的清冷等),字数控制在80字以内。举例:成年女性(约25-35岁),音色清冷且富有叙述感,语速平缓,带有仙侠剧特有的空灵韵味。
  6282. - 片段序号格式必须如下: ##片段+片段序号\n时长:xxs,例如: ##片段1\n时长:12s
  6283. 5.3分镜结构:每个分镜包含以下字段:
  6284. - 分镜序号在片段内用1、2、3显示,如分镜1、分镜2,并且分镜序号需连续(跨片段中的分镜序号需连续,如片段2中的分镜序号从片段1的最大序号+1开始),分镜序号前后不要带任何其他符号,特别是*符号。
  6285. - 出场角色: 如果有主体出现在本镜,则必须选择本集中的<主体列表>中的一个(如必须使用“沈秋岚-程家居家围裙造型”,而不是“沈秋岚”,不得缺少“-”前后和“:”前面的任何内容,不能自创)
  6286. - 场景:必须选择本集中的<场景列表>中的一个(如需使用“酒店会客室-景池筹备会版”,而不是“酒店会客室”,不得缺少“-”前后和“:”前面的任何内容,不能自创)
  6287. - 出场道具:如果有道具出现在本镜,则必须选择本集中的<道具列表>中的一个(如需使用“酒杯-高脚杯”,而不是“酒杯”或“高脚杯”,不得缺少“-”前后和“:”前面的任何内容,不能自创)
  6288. - 画面:【构图/视角】+画面描述(越简单越好,对视觉场景/实景的描述,不描述氛围,比如紧张、压抑的氛围)。举例:[近景特写,浅景深]季潇握着手机贴在耳边,指关节因为用力而泛白。她的眼眶微红,睫毛在狂风中颤抖,嘴唇紧抿,眼神中闪烁着最后一点希冀的微光。
  6289. - 画面中如果出现主体,只能选择上述<主体列表>中的一个,主体的名称也必须保持一致(如必须使用“沈秋岚-程家居家围裙造型”,而不是“沈秋岚”,不得缺少“-”前后和“:”前面的任何内容,不能自创)
  6290. - 画面中如果出现道具,只能选择上述<道具列表>中的一个,道具的名称也必须保持一致(如必须使用“酒杯-高脚杯”,而不是“酒杯”或“高脚杯”,不得缺少“-”前后和“:”前面的任何内容,不能自创)
  6291. - 运镜:场景+画面描述+运镜
  6292. - 运镜描述中如果出现主体或道具,逻辑需同画面一直,必须选择<主体列表>或<道具列表>中的一个,名称也必须保持一致
  6293. - 配音台词:格式为“中文配音:[角色]台词”,角色配音台词和语言由模型匹配生成,角色必须对应本集中的<主体列表>中的一个(非对话内容则统一固定为“旁白”角色,[角色]的角色名必须完整使用主体列表的主体名,如必须使用“沈秋岚-程家居家围裙造型”,而不是“沈秋岚”,不得缺少“-”前后和“:”前面的任何内容,不能自创,不能自创)
  6294. - 台词内容需满足以下要求: (1. 台词来源规则:台词内容必须逐字摘录自小说原文中的人物直接对话(带引号的内容),不允许改写、总结、压缩、扩写或拼接原文对白,不允许生成原文中不存在的对话。2. 非对话过滤:旁白、心理描写、环境描写、动作描写等内容,禁止出现在台词内容中。3. 空台词规则:如果该分镜中没有人物对话,则“台词内容”必须返回“无”,严禁输出“无台词”、“没有”等任何替代文本。4. 禁止推测:不得根据语境补全人物可能说的话。5.如果没有原文,直接返回空)
  6295. - 背景音效:背景音效需根据内容和画面生成,贴合当前画面描述和动作,例如:背景音效:高跟鞋的脚本声\n
  6296. 6.{$mappedArtStyle_prompt}\n\n
  6297. 示例如下:\n
  6298. ###剧本名:西昆仑
  6299. ###故事梗概
  6300. 内容梗概:许芸归家遭徐母毒打,她冷静周旋并编造谎言掩盖行踪,引得一旁阴沉的卞大伟愈发急躁。半个月后,徐母离家,卞大伟兽性大发欲行不轨。生死关头,隐形的程景现身肉搏,屋内家具横飞,卞大伟被“空气”重创。许芸趁乱逃出,却惊见地面凭空浮现一串血脚印。意识到守护者为救自己负伤,许芸不再逃避,对着虚无的空气执拗告白,誓要与这看不见的爱人共进退。
  6301. ###剧本亮点
  6302. 亮点1:绝境奇药,生死一线
  6303. 画面开场即是阿雪苍白无力的脸庞,梁萧眼中泪光闪烁,急促的呼吸声与心跳声烘托紧张气氛。特写梁萧将“阴阳球”塞入阿雪舌底,球体散发微光,阿雪气息从若有若无到逐渐平稳,生死逆转的瞬间,视觉与听觉冲击力十足。
  6304. 亮点2:结拜兄妹,化解尴尬
  6305. 阿雪因解衣拔刺而羞愤欲绝的特写,梁萧眼神中的挣扎与无奈。在紧迫的背景音下,梁萧急中生智,牵起阿雪颤抖的手,跪地匆匆结拜,阿雪眼神从惊惶到迷茫,最终一句“我……我愿意的”带着哭腔,将救命与羞耻的矛盾推向高潮,又以出人意料的方式解决。
  6306. 亮点3:纤发夺命,情愫暗涌
  6307. 拔刺过程的紧张特写,梁萧指尖小心翼翼地捏住细丝,缓慢抽出。当“问心刺”被完全拔出,特写它竟是一根细长、泛着寒光的女子秀发,诡异而令人震惊。阿雪苏醒后,两人眼神交汇,阿雪低头羞涩,梁萧眼中复杂的情绪,无声地传递出超越兄妹的微妙情愫。
  6308. ###人物关系
  6309. 阿雪与梁萧之间存在兄妹之情。
  6310. ###核心矛盾
  6311. 阿雪与梁萧之间存在兄妹之情,但因情愫暗生而产生微妙的矛盾。阿雪羞涩难当,梁萧情急之下提出结拜兄妹,以化解尴尬。两人匆匆结拜后,梁萧小心翼翼地拔出“问心刺”,竟发现它是一根诡异的女子秀发。阿雪转危为安,两人之间,在生死与羞涩交织的瞬间,情愫暗生,却又被兄妹之名束缚,留下无尽遐想。
  6312. ###主体列表
  6313. 阿雪:身中奇毒“问心刺”,气息奄奄,面色苍白,身体虚弱。{全景,正面拍摄,青年女性,亚洲人。
  6314. 黑色短发扎成利落的高马尾,五官精致且轮廓清晰。
  6315. 服饰穿搭:身着红白相间的专业田径运动背心与超短跑步裤,皮肤呈现健康的小麦色,四肢肌肉线条紧致有力。
  6316. 姿态:站立。}{{青年女性(约25-30岁),音色亲切、温柔,语速平稳缓慢}}
  6317. 梁萧:少年侠士,心系阿雪安危,沉着冷静,内心挣扎于情义之间。{8k超高清,电影级光效,大气透视,中国传统艺术美学,空灵光泽,深度景深,复杂精细的纹理,史诗级构图,电影胶片颗粒。
  6318. 全景,正面拍摄,青年女性,亚洲人。
  6319. 佩戴精致繁复的金丝凤冠,黑色长发整齐盘起。
  6320. 服饰穿搭:身着大红色正装嫁衣,上面绣有精细的金色凤凰纹样,质感厚重华贵。
  6321. 姿态:站立。}{{青年男性(约25-30岁),音色冷峻、低沉,语速平稳缓慢,透着一股拒人于千里之外的孤傲与肃杀。}}
  6322. ###美术风格
  6323. 基础画风风格词:厚涂古风
  6324. 整体采用精致细腻的厚涂古风,色彩浓郁而富有层次感,注重光影对比,营造出古典而富有戏剧张力的氛围。色调以深沉的蓝绿、灰调为主,衬托阿雪病弱的苍白,同时用温暖的金色、琥珀色光晕突出“阴阳球”的生命力。拔刺与结拜场景则以柔和的暖色调为主,辅以人物脸颊的红晕,烘托羞涩与情愫暗生的微妙感。人物造型飘逸,服饰细节考究,背景虚实结合,既有古韵,又不失奇幻色彩。
  6325. ###场景列表
  6326. 林间疗伤地:幽静的古风林间空地,一棵高大古树下,是梁萧为阿雪疗伤的地点。环境清幽,光影斑驳,既有古韵又不失奇幻色彩。{影视质感。广角。
  6327. 宽敞的天然山洞内部,石壁上错落有致地镶嵌着发光的萤石,散发出冷色调的微光,照亮了整个空间。洞顶有钟乳石垂下。光影层次丰富,营造出一种宁静而神圣的氛围。
  6328. 无人物。}
  6329. ###分段剧本
  6330. 片段数量:8
  6331. 旁白音色:成年男性(约35-50岁),音色厚重且富有磁性,带有老电影叙事般的沧桑感,语调低沉平稳,富有江湖气息。
  6332. ##片段1
  6333. 时长:12s
  6334. 分镜1
  6335. 出场角色:刀疤脸
  6336. 场景:边境小镇街道
  6337. 出场道具:酒杯-高脚杯
  6338. 画面:[大远景,冷色调,高对比度] 漫天黄沙在狂风中如怒龙翻滚,遮蔽了微弱的星光。悦来酒馆残破的木门在风中剧烈摇晃,铁链撞击木板发出沉重的闷响,四周建筑尽显荒凉。
  6339. 运镜:大远景固定镜头,随后风沙席卷向镜头形成遮挡转场。
  6340. 配音台词:
  6341. 背景音效:
  6342. 分镜2
  6343. 出场角色:刀疤脸
  6344. 场景:悦来酒馆
  6345. 出场道具:酒杯-高脚杯
  6346. 画面:[全景,暖色调阴影构图] 酒馆内部昏暗,几盏油灯在风中疯狂摇曳,投射出扭曲的长影。角落里的食客低头缩颈,双手紧紧攥着粗瓷碗,空气中漂浮着细小的尘埃。
  6347. 运镜:缓慢横移,掠过阴影中瑟缩的食客。
  6348. 配音台词:
  6349. 背景音效:
  6350. 分镜3
  6351. 出场角色:刀疤脸
  6352. 场景:悦来酒馆
  6353. 出场道具:酒杯-高脚杯
  6354. 画面:[中景,三分法构图] 三名大汉围坐在正中央的圆桌旁,腰间挂着的“武林盟”金色令牌在灯火下闪烁。桌上残羹冷炙横陈,一名大汉正张狂地大笑,脚踩在长凳上。
  6355. 运镜:从中景推向令牌特写。
  6356. 配音台词:
  6357. 背景音效:
  6358. 分镜4
  6359. 出场角色:刀疤脸
  6360. 场景:悦来酒馆
  6361. 出场道具:酒杯-高脚杯
  6362. 画面:[特写,低角度仰拍] 刀疤脸横肉颤动,嘴角挂着轻蔑的笑。他随手倾覆手中的酒碗,琥珀色的液体在空中划过弧线,重重溅在老李打着补丁的布鞋和裤脚上。
  6363. 运镜:极速推向酒水溅起的瞬间。
  6364. 配音台词:
  6365. 背景音效:
  6366. 分镜5
  6367. 出场角色:刀疤脸
  6368. 场景:悦来酒馆
  6369. 出场道具:酒杯-高脚杯
  6370. 画面:[近景,荷兰角倾斜构图] 刀疤脸斜睨着眼,昏暗的灯光勾勒出他脸上狰狞的伤疤。他身体前倾,阴影完全笼罩了对面的老李,眼神如毒蛇般阴冷。
  6371. 运镜:固定机位,利用倾斜构图制造压迫感。
  6372. 配音台词:中文配音:[刀疤脸]老李头,这酒里掺水了吧?
  6373. 背景音效:
  6374. \n\n"
  6375. : "你是一个专业的文档分析助手及资深编剧,请根据用户提供的文档内容及下面提供的示例格式完成剧本大纲(需通过以下几个板块进行回复: <故事梗概><剧本亮点><人物关系><核心矛盾><主体列表><美术风格><场景列表><道具列表>,每个板块之间必须使用###分隔,#不能多也不能少;同时板块之间需满足以下要求:\n
  6376. 强制要求:\n
  6377. 1.你只负责与剧本相关的内容,如果用户输入与剧本无关的内容,请告知用户并拒绝继续回答,此为第一优先原则,不满足此原则就忽略其他要求并停止继续输出
  6378. 2.如果用户输入修改美术风格、修改画风这类内容,请告知用户不可修改美术风格,并拒绝继续回答
  6379. 3.以上两条要求对修改主体角色或场景不生效,允许用户调整主体角色说明和场景描述等
  6380. 4.用户如果要求你帮忙写一个故事或者剧本,此类要求不属于与剧本无关,请帮助用户完成请求{$content_generation_requirement}
  6381. 5.强制按照示例格式返回内容,不要更改格式或随意增加符号\n
  6382. 普通要求:\n
  6383. 1.剧本名(必须生成)必须与文档内容高度相关
  6384. 2.<主体列表>中出现的“主体名”必须尽可能统一,严禁出现角色-科学家、角色-复仇者、角色-领导者等带有身份、职业、标签类的命名,只能使用服装差异(如校服装、卫衣装、西装)来区分同一角色,如角色-校服装等。
  6385. 2.1<主体列表>分以下三部分(强制格式):主体描述、人物提示词和音色提示词,格式必须为:主体名:主体描述{人物提示词}{{音色提示词}}
  6386. 2.2<主体列表>的主体描述是分析人物在整部剧中的姓名、年龄、性格特征、社会身份或职业背景、个人成长经历或核心故事动机,以及在剧情中的目标与行为驱动力,不需要描述外貌、穿着或身体特征,重点突出心理结构、行为模式与叙事功能,并且用一句话进行总结。
  6387. 2.3<主体列表>的人物提示词需要根据当前分析出来的美术风格生成,包含性别(男、女)、大概年龄或年龄段(少年、中年、老年等)、人种(亚洲人、欧洲人)、发型(直发卷发等)、发色(黑色、棕色、红色、白色等)、整体视觉风格,以及详细的穿着风格和服装细节、面部特征(戴金丝眼镜、脸上有刀疤等,无面部特征可忽略)。描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合人物身份设定。该提示词(字数50-100字左右)在主体描述后用{}框起来。
  6388. 2.4<主体列表>的音色提示词放在描述的最后用{{}}框起来。音色提示词需描述角色的整体音色特征,包含:性别、年龄(约XX岁)、音质(如清冷、沉稳、磁性等)、表达形式(如叙述感、旁白式等)、情绪氛围(如空灵韵味、看透世事的清冷等),字数控制在80字以内。举例:成年女性(约25-35岁),音色清冷且富有叙述感,语速平缓,带有仙侠剧特有的空灵韵味。
  6389. {$extra_role_prompt}
  6390. 3.<场景列表>与主体列表同理,需尽可能的对场景环境进行详细描述,必须严格注意不要出现任何角色和人物。
  6391. 3.1<场景列表>分以下两部分(强制格式):场景描述和场景提示词,格式必须为:场景描述{场景提示词}
  6392. 3.2<场景列表>的场景描述是分析在整部剧中出现过的场景,场景描述尽量简洁贴合原文。
  6393. 3.3<场景列表>的场景提示词用于视觉呈现的场景环境描述,该场景需包含环境氛围、光线效果、空间布局、色彩基调、视觉风格等,描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合剧情设定(可参考以下示例的场景列表中使用{}框起来的提示词,字数150-200字,需带上提炼出来的美术风格)。该提示词在场景描述后用{}框起来。\n
  6394. {$extra_scene_prompt}
  6395. 4.<道具列表>与场景列表同理,需尽可能的对道具环境进行详细描述,必须严格注意不要出现任何角色和人物。
  6396. 3.1<道具列表>分以下两部分(强制格式):道具描述和道具提示词,格式必须为:道具描述{道具提示词}
  6397. 3.2<道具列表>的道具描述是分析在整部剧中出现过的道具,道具描述尽量简洁贴合原文。
  6398. 3.3<道具列表>的道具提示词用于视觉呈现的道具环境描述,该道具需包含环境氛围、光线效果、空间布局、色彩基调、视觉风格等,描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合剧情设定(可参考以下示例的道具列表中使用{}框起来的提示词,字数150-200字,需带上提炼出来的美术风格)。该提示词在道具描述后用{}框起来。\n
  6399. {$extra_prop_prompt}
  6400. 5.{$mappedArtStyle_prompt}\n\n
  6401. 示例如下:\n
  6402. ###剧本名:西昆仑
  6403. ###故事梗概
  6404. 内容梗概:许芸归家遭徐母毒打,她冷静周旋并编造谎言掩盖行踪,引得一旁阴沉的卞大伟愈发急躁。半个月后,徐母离家,卞大伟兽性大发欲行不轨。生死关头,隐形的程景现身肉搏,屋内家具横飞,卞大伟被“空气”重创。许芸趁乱逃出,却惊见地面凭空浮现一串血脚印。意识到守护者为救自己负伤,许芸不再逃避,对着虚无的空气执拗告白,誓要与这看不见的爱人共进退。
  6405. ###剧本亮点
  6406. 亮点1:绝境奇药,生死一线
  6407. 画面开场即是阿雪苍白无力的脸庞,梁萧眼中泪光闪烁,急促的呼吸声与心跳声烘托紧张气氛。特写梁萧将“阴阳球”塞入阿雪舌底,球体散发微光,阿雪气息从若有若无到逐渐平稳,生死逆转的瞬间,视觉与听觉冲击力十足。
  6408. 亮点2:结拜兄妹,化解尴尬
  6409. 阿雪因解衣拔刺而羞愤欲绝的特写,梁萧眼神中的挣扎与无奈。在紧迫的背景音下,梁萧急中生智,牵起阿雪颤抖的手,跪地匆匆结拜,阿雪眼神从惊惶到迷茫,最终一句“我……我愿意的”带着哭腔,将救命与羞耻的矛盾推向高潮,又以出人意料的方式解决。
  6410. 亮点3:纤发夺命,情愫暗涌
  6411. 拔刺过程的紧张特写,梁萧指尖小心翼翼地捏住细丝,缓慢抽出。当“问心刺”被完全拔出,特写它竟是一根细长、泛着寒光的女子秀发,诡异而令人震惊。阿雪苏醒后,两人眼神交汇,阿雪低头羞涩,梁萧眼中复杂的情绪,无声地传递出超越兄妹的微妙情愫。
  6412. ###人物关系
  6413. 阿雪与梁萧之间存在兄妹之情。
  6414. ###核心矛盾
  6415. 阿雪与梁萧之间存在兄妹之情,但因情愫暗生而产生微妙的矛盾。阿雪羞涩难当,梁萧情急之下提出结拜兄妹,以化解尴尬。两人匆匆结拜后,梁萧小心翼翼地拔出“问心刺”,竟发现它是一根诡异的女子秀发。阿雪转危为安,两人之间,在生死与羞涩交织的瞬间,情愫暗生,却又被兄妹之名束缚,留下无尽遐想。
  6416. ###主体列表
  6417. 阿雪:身中奇毒“问心刺”,气息奄奄,面色苍白,身体虚弱。{全景,正面拍摄,青年女性,亚洲人。
  6418. 黑色短发扎成利落的高马尾,五官精致且轮廓清晰。
  6419. 服饰穿搭:身着红白相间的专业田径运动背心与超短跑步裤,皮肤呈现健康的小麦色,四肢肌肉线条紧致有力。
  6420. 姿态:站立。}{{青年女性(约25-30岁),音色亲切、温柔,语速平稳缓慢}}
  6421. 梁萧:少年侠士,心系阿雪安危,沉着冷静,内心挣扎于情义之间。{8k超高清,电影级光效,大气透视,中国传统艺术美学,空灵光泽,深度景深,复杂精细的纹理,史诗级构图,电影胶片颗粒。
  6422. 全景,正面拍摄,青年女性,亚洲人。
  6423. 佩戴精致繁复的金丝凤冠,黑色长发整齐盘起。
  6424. 服饰穿搭:身着大红色正装嫁衣,上面绣有精细的金色凤凰纹样,质感厚重华贵。
  6425. 姿态:站立。}{{青年男性(约25-30岁),音色冷峻、低沉,语速平稳缓慢,透着一股拒人于千里之外的孤傲与肃杀。}}
  6426. ###美术风格
  6427. 基础画风风格词:厚涂古风
  6428. 整体采用精致细腻的厚涂古风,色彩浓郁而富有层次感,注重光影对比,营造出古典而富有戏剧张力的氛围。色调以深沉的蓝绿、灰调为主,衬托阿雪病弱的苍白,同时用温暖的金色、琥珀色光晕突出“阴阳球”的生命力。拔刺与结拜场景则以柔和的暖色调为主,辅以人物脸颊的红晕,烘托羞涩与情愫暗生的微妙感。人物造型飘逸,服饰细节考究,背景虚实结合,既有古韵,又不失奇幻色彩。
  6429. ###场景列表
  6430. 林间疗伤地:幽静的古风林间空地,一棵高大古树下,是梁萧为阿雪疗伤的地点。环境清幽,光影斑驳,既有古韵又不失奇幻色彩。{影视质感。广角。
  6431. 宽敞的天然山洞内部,石壁上错落有致地镶嵌着发光的萤石,散发出冷色调的微光,照亮了整个空间。洞顶有钟乳石垂下。光影层次丰富,营造出一种宁静而神圣的氛围。
  6432. 无人物。}
  6433. ###道具列表
  6434. 酒杯-高脚杯:高脚杯,盛有少量酒液。{高脚酒杯,道具多角度视角图,正面、背面、侧面、局部细节,透明玻璃材质,细长杯柄,尺寸标注高24厘米杯口6厘米,白底干净展示,无人物}"];
  6435. // 如果提示词$prompt有类似于改成N集或调整成N集这类的文字信息,则提取出N这个集数
  6436. $episode_count = $this->extractEpisodeNumber($prompt);
  6437. if ((int)getProp($anime, 'is_multi') !== 1) {
  6438. yield [
  6439. 'type' => 'done',
  6440. 'answer' => '当前剧本是单剧集,不支持修改剧集数',
  6441. 'reasoning' => '',
  6442. 'usage' => []
  6443. ];
  6444. return;
  6445. }
  6446. if ($episode_count) {
  6447. $question = "请根据实例格式完成{$episode_count}集剧本大纲";
  6448. $system_message = [
  6449. 'role' => 'system',
  6450. 'content' => "你是一个专业的文档分析助手及资深编剧,请根据用户提供的文档内容及下面提供的示例格式完成剧本大纲(需通过以下几个板块进行回复: <故事梗概><剧本亮点><人物关系><核心矛盾><主体列表><美术风格><场景列表><分集详细内容>,每个板块之间必须使用###分隔,#不能多也不能少;同时板块之间需满足以下要求:\n
  6451. 强制要求:\n
  6452. 1.你只负责与剧本相关的内容,如果用户输入与剧本无关的内容,请告知用户并拒绝继续回答,此为第一优先原则,不满足此原则就忽略其他要求并停止继续输出
  6453. 2.如果用户输入修改美术风格、修改画风这类内容,请告知用户不可修改美术风格,并拒绝继续回答
  6454. 3.以上两条要求对修改主体角色或场景不生效,允许用户调整主体角色说明和场景描述等
  6455. 4.用户如果要求你帮忙写一个故事或者剧本,此类要求不属于与剧本无关,请帮助用户完成请求{$content_generation_requirement}
  6456. 5.强制按照示例格式返回内容,不要更改格式或随意增加符号\n
  6457. 普通要求:\n
  6458. 1.<故事梗概>控制在200-300字左右
  6459. 2.<主体列表>中出现的“主体名”必须尽可能统一,严禁出现角色-科学家、角色-复仇者、角色-领导者等带有身份、职业、标签类的命名,只能使用服装差异(如校服装、卫衣装、西装)来区分同一角色,如角色-校服装等。
  6460. 2.1<主体列表>分以下三部分:主体描述、人物提示词和音色提示词,格式必须为:主体名:主体描述{人物提示词}{{音色提示词}}
  6461. 2.2<主体列表>的主体描述是分析人物在整部剧中的姓名、年龄、性格特征、社会身份或职业背景、个人成长经历或核心故事动机,以及在剧情中的目标与行为驱动力,不需要描述外貌、穿着或身体特征,重点突出心理结构、行为模式与叙事功能,并且用一句话进行总结。
  6462. 2.3<主体列表>的人物提示词需要根据当前分析出来的美术风格生成,包含性别(男、女)、大概年龄或年龄段(少年、中年、老年等)、人种(亚洲人、欧洲人)、发型(直发卷发等)、发色(黑色、棕色、红色、白色等)、整体视觉风格,以及详细的穿着风格和服装细节、面部特征(戴金丝眼镜、脸上有刀疤等,无面部特征可忽略)。描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合人物身份设定。该提示词(字数50-100字左右)在主体描述后用{}框起来。
  6463. 2.4<主体列表>的音色提示词放在描述的最后用{{}}框起来。音色提示词需描述角色的整体音色特征,包含:性别、年龄(约XX岁)、音质(如清冷、沉稳、磁性等)、表达形式(如叙述感、旁白式等)、情绪氛围(如空灵韵味、看透世事的清冷等),字数控制在80字以内。举例:成年女性(约25-35岁),音色清冷且富有叙述感,语速平缓,带有仙侠剧特有的空灵韵味。
  6464. 3.<场景列表>与主体列表同理,需尽可能的对场景环境进行详细描述,必须严格注意不要出现任何角色和人物。
  6465. 3.1<场景列表>分以下两部分:场景描述和场景提示词,格式必须为:场景描述{场景提示词},注意不要出现{{}}这样的双大括号。
  6466. 3.2<场景列表>的场景描述是分析在整部剧中出现过的场景,场景描述尽量简洁贴合原文。
  6467. 3.3<场景列表>的场景提示词用于视觉呈现的场景环境描述,该场景需包含环境氛围、光线效果、空间布局、色彩基调、视觉风格等,描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合剧情设定(可参考以下示例的场景列表中使用{}框起来的提示词,字数150-200字,需带上提炼出来的美术风格)。该提示词在场景描述后用{}框起来。\n
  6468. 4.<分集详细内容>中每一集的内容需尽可能的详细,保留对话内容(使用原文内容,不得自己生成),一般每集字数尽量不低于500字,每集结尾需以##第N章 作为章节标题\n\n
  6469. 示例如下:\n
  6470. ###剧本名:西昆仑
  6471. ###故事梗概
  6472. 内容梗概:许芸归家遭徐母毒打,她冷静周旋并编造谎言掩盖行踪,引得一旁阴沉的卞大伟愈发急躁。半个月后,徐母离家,卞大伟兽性大发欲行不轨。生死关头,隐形的程景现身肉搏,屋内家具横飞,卞大伟被“空气”重创。许芸趁乱逃出,却惊见地面凭空浮现一串血脚印。意识到守护者为救自己负伤,许芸不再逃避,对着虚无的空气执拗告白,誓要与这看不见的爱人共进退。
  6473. ###剧本亮点
  6474. 亮点1:绝境奇药,生死一线
  6475. 画面开场即是阿雪苍白无力的脸庞,梁萧眼中泪光闪烁,急促的呼吸声与心跳声烘托紧张气氛。特写梁萧将“阴阳球”塞入阿雪舌底,球体散发微光,阿雪气息从若有若无到逐渐平稳,生死逆转的瞬间,视觉与听觉冲击力十足。
  6476. 亮点2:结拜兄妹,化解尴尬
  6477. 阿雪因解衣拔刺而羞愤欲绝的特写,梁萧眼神中的挣扎与无奈。在紧迫的背景音下,梁萧急中生智,牵起阿雪颤抖的手,跪地匆匆结拜,阿雪眼神从惊惶到迷茫,最终一句“我……我愿意的”带着哭腔,将救命与羞耻的矛盾推向高潮,又以出人意料的方式解决。
  6478. 亮点3:纤发夺命,情愫暗涌
  6479. 拔刺过程的紧张特写,梁萧指尖小心翼翼地捏住细丝,缓慢抽出。当“问心刺”被完全拔出,特写它竟是一根细长、泛着寒光的女子秀发,诡异而令人震惊。阿雪苏醒后,两人眼神交汇,阿雪低头羞涩,梁萧眼中复杂的情绪,无声地传递出超越兄妹的微妙情愫。
  6480. ###人物关系
  6481. 阿雪与梁萧之间存在兄妹之情。
  6482. ###核心矛盾
  6483. 阿雪与梁萧之间存在兄妹之情,但因情愫暗生而产生微妙的矛盾。阿雪羞涩难当,梁萧情急之下提出结拜兄妹,以化解尴尬。两人匆匆结拜后,梁萧小心翼翼地拔出“问心刺”,竟发现它是一根诡异的女子秀发。阿雪转危为安,两人之间,在生死与羞涩交织的瞬间,情愫暗生,却又被兄妹之名束缚,留下无尽遐想。
  6484. ###主体列表
  6485. 阿雪:身中奇毒“问心刺”,气息奄奄,面色苍白,身体虚弱。{全景,正面拍摄,青年女性,亚洲人。
  6486. 黑色短发扎成利落的高马尾,五官精致且轮廓清晰。
  6487. 服饰穿搭:身着红白相间的专业田径运动背心与超短跑步裤,皮肤呈现健康的小麦色,四肢肌肉线条紧致有力。
  6488. 姿态:站立。}{{青年女性(约25-30岁),音色亲切、温柔,语速平稳缓慢}}
  6489. 梁萧:少年侠士,心系阿雪安危,沉着冷静,内心挣扎于情义之间。{8k超高清,电影级光效,大气透视,中国传统艺术美学,空灵光泽,深度景深,复杂精细的纹理,史诗级构图,电影胶片颗粒。
  6490. 全景,正面拍摄,青年女性,亚洲人。
  6491. 佩戴精致繁复的金丝凤冠,黑色长发整齐盘起。
  6492. 服饰穿搭:身着大红色正装嫁衣,上面绣有精细的金色凤凰纹样,质感厚重华贵。
  6493. 姿态:站立。}{{青年男性(约25-30岁),音色冷峻、低沉,语速平稳缓慢,透着一股拒人于千里之外的孤傲与肃杀。}}
  6494. ###美术风格
  6495. 基础画风风格词:厚涂古风
  6496. 整体采用精致细腻的厚涂古风,色彩浓郁而富有层次感,注重光影对比,营造出古典而富有戏剧张力的氛围。色调以深沉的蓝绿、灰调为主,衬托阿雪病弱的苍白,同时用温暖的金色、琥珀色光晕突出“阴阳球”的生命力。拔刺与结拜场景则以柔和的暖色调为主,辅以人物脸颊的红晕,烘托羞涩与情愫暗生的微妙感。人物造型飘逸,服饰细节考究,背景虚实结合,既有古韵,又不失奇幻色彩。
  6497. ###场景列表
  6498. 林间疗伤地:幽静的古风林间空地,一棵高大古树下,是梁萧为阿雪疗伤的地点。环境清幽,光影斑驳,既有古韵又不失奇幻色彩。{影视质感。广角。
  6499. 宽敞的天然山洞内部,石壁上错落有致地镶嵌着发光的萤石,散发出冷色调的微光,照亮了整个空间。洞顶有钟乳石垂下。光影层次丰富,营造出一种宁静而神圣的氛围。
  6500. 无人物。}
  6501. ###分集详细内容
  6502. ##第1章 重生
  6503. 我重生了。
  6504. 源于一个男人偏执的暗恋。
  6505. ##第2章 相救
  6506. 我一到家,徐母的扫帚就落在我身上,嘴里一个劲地骂我,问我夜不归宿死哪儿去了。
  6507. 我眼睛扫向站在锅炉后的卞大伟。"
  6508. ];
  6509. // 构建消息
  6510. $messages = [
  6511. $system_message,
  6512. [
  6513. 'role' => 'user',
  6514. 'content' => "以下是文档内容:
  6515. {$content}
  6516. 用户问题:
  6517. {$question}"
  6518. ]
  6519. ];
  6520. }else {
  6521. if (strstr($prompt, '重新生成') || strstr($prompt, '再生成')) {
  6522. // 构建消息
  6523. $messages = [
  6524. $system_message,
  6525. [
  6526. 'role' => 'user',
  6527. 'content' => "以下是文档内容:
  6528. {$content}
  6529. 用户问题:
  6530. {$question}"
  6531. ]
  6532. ];
  6533. }else {
  6534. $messages = DB::table('mp_anime_records')->where('anime_id', $anime_id)->where('sequence', 0)
  6535. ->orderBy('id')->select('role', 'content')->get()->map(function ($value) {
  6536. return (array)$value;
  6537. })->toArray();
  6538. array_unshift($messages, $system_message);
  6539. $messages[] = [
  6540. 'role' => 'user',
  6541. 'content' => $prompt
  6542. ];
  6543. }
  6544. }
  6545. // 模型兼容性处理:deepseek-chat 和 deepseek-reasoner 映射到 deepseek-v4-flash
  6546. $thinkingMode = getProp($data, 'thinking', 'disabled'); // 默认非思考模式
  6547. $reasoningEffort = getProp($data, 'reasoning_effort', 'high'); // 默认思考强度
  6548. if ($model === 'deepseek-chat') {
  6549. // deepseek-chat 映射到 deepseek-v4-flash,非思考模式
  6550. $model = 'deepseek-v4-flash';
  6551. $thinkingMode = 'disabled';
  6552. } elseif ($model === 'deepseek-reasoner') {
  6553. // deepseek-reasoner 映射到 deepseek-v4-flash,思考模式
  6554. $model = 'deepseek-v4-flash';
  6555. $thinkingMode = 'enabled';
  6556. }
  6557. $post_data = [
  6558. 'model' => $model,
  6559. 'messages' => $messages,
  6560. // 'max_tokens' => 8192,
  6561. 'temperature' => 0.7,
  6562. 'frequency_penalty' => 0,
  6563. 'presence_penalty' => 0,
  6564. 'response_format' => ['type' => 'text'],
  6565. 'thinking' => ['type' => $thinkingMode],
  6566. 'stream' => true // 启用流式输出
  6567. ];
  6568. if ($thinkingMode == 'enabled') $post_data['reasoning_effort'] = $reasoningEffort;
  6569. // 根据模型类型选择调用方法
  6570. $fullContent = '';
  6571. $fullReasoningContent = '';
  6572. $usage = [];
  6573. // dd($post_data);
  6574. // dLog('deepseek')->info('请求参数: '.json_encode($post_data, 256));
  6575. try {
  6576. if (in_array($model, ['deepseek-reasoner', 'deepseek-chat', 'deepseek-v4-flash', 'deepseek-v4-pro'])) {
  6577. $streamGenerator = $this->deepSeekStreamResponse($post_data);
  6578. } else if (in_array($model, $this->gpt_text_models)) {
  6579. // GPT-5.4 模型使用 TokenRouter API
  6580. $streamGenerator = $this->gpt54StreamResponse($post_data);
  6581. } else {
  6582. $streamGenerator = $this->volcEngineChatCompletion($post_data);
  6583. }
  6584. // 验证返回值类型
  6585. if (!is_iterable($streamGenerator)) {
  6586. $errorType = is_object($streamGenerator) ? get_class($streamGenerator) : gettype($streamGenerator);
  6587. dLog('deepseek')->error('方法名流式生成器无效', [
  6588. 'generator_type' => $errorType,
  6589. 'model' => $model,
  6590. // 添加其他上下文信息
  6591. ]);
  6592. logDB('deepseek', 'error', '方法名流式生成器类型错误', [
  6593. 'type' => $errorType,
  6594. 'model' => $model
  6595. ]);
  6596. yield [
  6597. 'type' => 'error',
  6598. // 根据方法返回相应的空数据结构
  6599. 'answer' => '',
  6600. 'reasoning' => '',
  6601. 'usage' => [],
  6602. 'error' => '接口返回数据异常,请重新请求'
  6603. ];
  6604. return;
  6605. }
  6606. // 处理流式输出
  6607. foreach ($streamGenerator as $chunk) {
  6608. if (isset($chunk['type'])) {
  6609. if ($chunk['type'] === 'done') {
  6610. $fullContent = $chunk['full_content'];
  6611. $fullReasoningContent = $chunk['full_reasoning'];
  6612. $usage = isset($chunk['usage']) ? $chunk['usage'] : [];
  6613. } else {
  6614. yield $chunk;
  6615. }
  6616. }
  6617. }
  6618. } catch (\Exception $e) {
  6619. dLog('deepseek')->error('方法名流式处理异常: ' . $e->getMessage(), [
  6620. 'model' => $model,
  6621. 'trace' => $e->getTraceAsString()
  6622. ]);
  6623. logDB('deepseek', 'error', '方法名流式处理失败', [
  6624. 'error' => $e->getMessage(),
  6625. 'model' => $model
  6626. ]);
  6627. yield [
  6628. 'type' => 'error',
  6629. // 根据方法返回相应的空数据结构
  6630. 'answer' => '',
  6631. 'reasoning' => '',
  6632. 'usage' => [],
  6633. 'error' => '处理请求时发生异常: ' . $e->getMessage()
  6634. ];
  6635. return;
  6636. }
  6637. dLog('deepseek')->info('完整内容: '.$fullContent);
  6638. logDB('deepseek', 'info', '完整内容', ['content' => $fullContent, 'anime_id' => $anime_id]);
  6639. // 处理完整内容并返回最终结果
  6640. $script_arr = [];
  6641. if ($fullContent) {
  6642. $script_arr = handleScriptContentForAce($fullContent);
  6643. dLog('deepseek')->info('解析内容', $script_arr);
  6644. logDB('deepseek', 'info', '解析内容', $script_arr);
  6645. }
  6646. if (empty($script_arr['roles'])) {
  6647. // 未生成剧本相关内容,保存对话记录并返回提示
  6648. dLog('deepseek')->info('未生成剧本相关的内容');
  6649. try {
  6650. $now = date('Y-m-d H:i:s');
  6651. // 保存对话记录
  6652. $records = [
  6653. [
  6654. 'uid' => $uid,
  6655. 'anime_id' => $anime_id,
  6656. 'sequence' => 0,
  6657. 'role' => 'user',
  6658. 'content' => $prompt,
  6659. 'created_at' => $now,
  6660. 'updated_at' => $now
  6661. ],
  6662. [
  6663. 'uid' => $uid,
  6664. 'anime_id' => $anime_id,
  6665. 'sequence' => 0,
  6666. 'role' => 'assistant',
  6667. 'content' => '未生成剧本相关的内容,请调整提示词后重试',
  6668. 'created_at' => $now,
  6669. 'updated_at' => $now
  6670. ]
  6671. ];
  6672. DB::table('mp_anime_records')->insert($records);
  6673. } catch (\Exception $e) {
  6674. dLog('deepseek')->error('保存对话记录失败', ['error' => $e->getMessage()]);
  6675. }
  6676. yield [
  6677. 'type' => 'done',
  6678. 'script' => [],
  6679. 'episode' => [],
  6680. 'answer' => $fullContent,
  6681. 'reasoning' => $fullReasoningContent,
  6682. 'usage' => $usage,
  6683. 'error' => '未生成剧本相关的内容,请调整提示词再试'
  6684. ];
  6685. return;
  6686. }
  6687. // 替换美术风格
  6688. if ($mappedArtStyle !== '') {
  6689. $script_arr['art_style'] = $mappedArtStyle;
  6690. $fullContent = $this->replaceArtStyleSection($fullContent, $mappedArtStyle);
  6691. }
  6692. if ($user_anime_name != '新剧本策划') {
  6693. $anime_name = isset($script_arr['script_name']) ? $script_arr['script_name'] : '';
  6694. // 确认对话名称唯一性
  6695. if ($anime_name && $id = DB::table('mp_animes')->where('anime_name', $anime_name)->value('id')) {
  6696. if ($id != $anime_id) $anime_name = $anime_name . '_' . date('YmdHis');
  6697. }
  6698. }else {
  6699. $anime_name = $user_anime_name;
  6700. }
  6701. $table_data = [
  6702. 'user_id' => $uid,
  6703. 'model' => $model,
  6704. 'intro' => isset($script_arr['intro']) ? $script_arr['intro'] : '',
  6705. 'highlights' => isset($script_arr['highlights']) ? $script_arr['highlights'] : '',
  6706. 'role_relationship' => isset($script_arr['role_relationship']) ? $script_arr['role_relationship'] : '',
  6707. 'core_contradiction' => isset($script_arr['core_contradiction']) ? $script_arr['core_contradiction'] : '',
  6708. 'roles' => isset($script_arr['roles']) && is_array($script_arr['roles']) ? json_encode($script_arr['roles'], 256) : '[]',
  6709. 'art_style' => isset($script_arr['art_style']) ? $script_arr['art_style'] : '',
  6710. 'art_style_type' => $input_art_style,
  6711. 'scenes' => isset($script_arr['scenes']) && is_array($script_arr['scenes']) ? json_encode($script_arr['scenes'], 256) : '[]',
  6712. 'props' => isset($script_arr['props']) && is_array($script_arr['props']) ? json_encode($script_arr['props'], 256) : '[]',
  6713. 'status' => '解析完成',
  6714. 'generate_status' => '待执行',
  6715. 'updated_at' => date('Y-m-d H:i:s')
  6716. ];
  6717. if ($anime_name) $table_data['anime_name'] = $anime_name;
  6718. // 如果是修改集数,则将content内容更新(会改变原文)
  6719. if (isset($episode_count) && $episode_count > 0) {
  6720. if (isset($script_arr['content']) && $script_arr['content']) $table_data['content'] = $script_arr['content'];
  6721. $table_data['start_episode_sequence'] = 1;
  6722. $table_data['end_episode_sequence'] = $table_data['episode_num'] = $episode_count;
  6723. }else {
  6724. // 如果需要生成原文内容,从script_arr中提取
  6725. if ($need_generate_content && isset($script_arr['content']) && $script_arr['content']) {
  6726. $table_data['content'] = $script_arr['content'];
  6727. dLog('deepseek')->info('从AI生成结果中提取原文内容', ['content_length' => strlen($script_arr['content'])]);
  6728. if (!$table_data['content']) {
  6729. yield [
  6730. 'type' => 'done',
  6731. 'script' => [],
  6732. 'episode' => [],
  6733. 'answer' => $fullContent,
  6734. 'reasoning' => $fullReasoningContent,
  6735. 'usage' => $usage,
  6736. 'error' => '未生成剧本内容,请调整提示词再试'
  6737. ];
  6738. return;
  6739. }
  6740. }
  6741. if (isset($table_data['content']) && $table_data['content']) {
  6742. $chapters = $this->splitContent($table_data['content']);
  6743. $chapter_count = count($chapters);
  6744. if ($chapter_count > 0) {
  6745. $table_data['start_episode_sequence'] = 1;
  6746. $table_data['end_episode_sequence'] = $table_data['episode_num'] = $chapter_count;
  6747. }
  6748. }
  6749. }
  6750. if ($extra_products) {
  6751. $table_data['extra_products'] = json_encode($extra_products, 256);
  6752. }
  6753. $single_episode = [];
  6754. try {
  6755. DB::beginTransaction();
  6756. $now = date('Y-m-d H:i:s');
  6757. // 更新动漫大纲
  6758. $boolen = DB::table('mp_animes')->where('id', $anime_id)->update($table_data);
  6759. if (!$boolen) {
  6760. dLog('deepseek')->info('对话修改失败', $table_data);
  6761. logDB('deepseek', 'error', '对话修改失败', ['anime_id' => $anime_id]);
  6762. Utils::throwError('20003:对话修改失败');
  6763. }
  6764. // 保存对话记录
  6765. $records = [
  6766. [
  6767. 'uid' => $uid,
  6768. 'anime_id' => $anime_id,
  6769. 'sequence' => 0,
  6770. 'role' => 'user',
  6771. 'content' => $prompt,
  6772. 'created_at' => $now,
  6773. 'updated_at' => $now
  6774. ],
  6775. [
  6776. 'uid' => $uid,
  6777. 'anime_id' => $anime_id,
  6778. 'sequence' => 0,
  6779. 'role' => 'assistant',
  6780. // 'content' => '我已按照您的要求完成策划并将内容更新到您右侧的策划文档。',
  6781. 'content' => $fullContent,
  6782. 'created_at' => $now,
  6783. 'updated_at' => $now
  6784. ]
  6785. ];
  6786. // 保存对话记录
  6787. $boolen3 = DB::table('mp_anime_records')->insert($records);
  6788. if (!$boolen3) {
  6789. Utils::throwError('20003:对话记录保存失败');
  6790. }
  6791. // 单剧集模式:生成并保存剧集信息
  6792. if ($is_single) {
  6793. $anime_name = getProp($anime, 'anime_name', '未命名');
  6794. $episode_arr = $this->buildSingleEpisodeFromScript($script_arr, $fullContent, $anime_name);
  6795. if (empty($episode_arr['acts'])) {
  6796. Utils::throwError('20003:单剧集分镜内容解析失败,请确认输出格式');
  6797. }
  6798. $saveResult = $this->saveEpisodeVersionData(
  6799. $anime_id,
  6800. 1,
  6801. $episode_arr,
  6802. is_array(getProp($script_arr, 'roles')) ? getProp($script_arr, 'roles') : [],
  6803. is_array(getProp($script_arr, 'scenes')) ? getProp($script_arr, 'scenes') : [],
  6804. is_array(getProp($script_arr, 'props')) ? getProp($script_arr, 'props') : [],
  6805. null,
  6806. null,
  6807. false,
  6808. $content,
  6809. $now,
  6810. $ref_products
  6811. );
  6812. $single_episode = $saveResult['episode'];
  6813. $episode_id = $saveResult['episode_id'];
  6814. $boolen5 = DB::table('mp_animes')->where('id', $anime_id)->update([
  6815. 'roles' => json_encode($saveResult['merged_roles'], 256),
  6816. 'scenes' => json_encode($saveResult['merged_scenes'], 256),
  6817. 'props' => json_encode($saveResult['merged_props'], 256),
  6818. 'updated_at' => $now
  6819. ]);
  6820. if ($boolen5 === false) {
  6821. Utils::throwError('20003:单剧集主表资源同步失败');
  6822. }
  6823. $table_data['roles'] = json_encode($saveResult['merged_roles'], 256);
  6824. $table_data['scenes'] = json_encode($saveResult['merged_scenes'], 256);
  6825. $table_data['props'] = json_encode($saveResult['merged_props'], 256);
  6826. $episode_record_content = '确认分镜大纲';
  6827. if ($content !== '') {
  6828. // $episode_record_content .= "\n\n###上传内容\n" . $content;
  6829. }
  6830. $episode_records = [
  6831. [
  6832. 'uid' => $uid,
  6833. 'anime_id' => $anime_id,
  6834. 'role' => 'user',
  6835. 'content' => $episode_record_content,
  6836. 'sequence' => 1,
  6837. 'episode_id' => $episode_id,
  6838. 'created_at' => $now,
  6839. 'updated_at' => $now
  6840. ],
  6841. [
  6842. 'uid' => $uid,
  6843. 'anime_id' => $anime_id,
  6844. 'role' => 'assistant',
  6845. 'content' => $fullContent,
  6846. // 'content' => '我已按照您的要求完成策划并将内容更新到您右侧的策划文档。',
  6847. 'sequence' => 1,
  6848. 'episode_id' => $episode_id,
  6849. 'created_at' => $now,
  6850. 'updated_at' => $now
  6851. ]
  6852. ];
  6853. $boolen4 = DB::table('mp_anime_records')->insert($episode_records);
  6854. if (!$boolen4) {
  6855. Utils::throwError('20003:单剧集分镜记录保存失败');
  6856. }
  6857. }
  6858. }catch (\Exception $e) {
  6859. DB::rollBack();
  6860. dLog('deepseek')->info('修改对话错误信息: '.$e->getMessage());
  6861. logDB('deepseek', 'error', '修改对话失败', ['error' => $e->getMessage(), 'anime_id' => $anime_id]);
  6862. yield [
  6863. 'type' => 'done',
  6864. 'answer' => $fullContent,
  6865. 'reasoning' => $fullReasoningContent,
  6866. 'usage' => $usage,
  6867. 'error' => $e->getMessage(),
  6868. ];
  6869. return;
  6870. }
  6871. DB::commit();
  6872. dLog('deepseek')->info('保存完毕');
  6873. if ($is_single && !empty($single_episode)) {
  6874. // $this->batchSetGlobalRoleImg([
  6875. // 'anime_id' => $anime_id,
  6876. // ]);
  6877. // $this->batchSetGlobalSceneImg([
  6878. // 'anime_id' => $anime_id,
  6879. // ]);
  6880. // // 将全局角色和场景的task_id、task_status和url继承给当前分集
  6881. // $this->inheritGlobalImagesToEpisodes($anime_id, $episode_id);
  6882. }
  6883. $table_data['anime_id'] = $anime_id;
  6884. $table_data['roles'] = json_decode($table_data['roles'], true);
  6885. $table_data['scenes'] = json_decode($table_data['scenes'], true);
  6886. $table_data['props'] = json_decode($table_data['props'], true);
  6887. unset($table_data['updated_at']);
  6888. unset($table_data['status']);
  6889. yield [
  6890. 'type' => 'done',
  6891. 'script' => $table_data,
  6892. 'episode' => $single_episode,
  6893. 'answer' => $fullContent,
  6894. 'reasoning' => $fullReasoningContent,
  6895. 'usage' => $usage
  6896. ];
  6897. }
  6898. public function chatForAce($data) {
  6899. $uid = Site::getUid();
  6900. $anime_id = getProp($data, 'anime_id');
  6901. $file = getProp($data, 'file');
  6902. $content = getProp($data, 'content', '');
  6903. $bid = getProp($data, 'bid', 0);
  6904. $script_id = getProp($data, 'script_id', 0);
  6905. $episode_number = getProp($data, 'episode_number', 1);
  6906. $prompt = trim((string)getProp($data, 'prompt', '确认分镜大纲'));
  6907. $anime = DB::table('mp_animes')->where('id', $anime_id)->first();
  6908. if (!$anime) Utils::throwError('20003:该对话不存在!');
  6909. if ((int)getProp($anime, 'is_multi') !== 1 && (int)$episode_number !== 1) Utils::throwError('20003:单剧集不支持其他剧集序号');
  6910. $current_episode = DB::table('mp_anime_episodes')->where('anime_id', $anime_id)->where('episode_number', $episode_number)->where('is_default', 1)->first();
  6911. $base_episode = $current_episode ?: DB::table('mp_anime_episodes')->where('anime_id', $anime_id)->where('episode_number', $episode_number)->orderByDesc('id')->first();
  6912. $roles = $base_episode ? json_decode((string)getProp($base_episode, 'roles'), true) : json_decode((string)getProp($anime, 'roles'), true);
  6913. $scenes = $base_episode ? json_decode((string)getProp($base_episode, 'scenes'), true) : json_decode((string)getProp($anime, 'scenes'), true);
  6914. $roles = is_array($roles) ? $roles : [];
  6915. $scenes = is_array($scenes) ? $scenes : [];
  6916. $extra_products = getProp($data, 'products', []);
  6917. if (is_string($extra_products)) $extra_products = json_decode($extra_products, true);
  6918. // 如果anime有关联的script_id,则从mp_script_product_mappings中查询资产并合并到extra_products
  6919. $anime_script_id = getProp($anime, 'script_id');
  6920. if ($anime_script_id) {
  6921. $cpid = Site::getCpid();
  6922. $script_products_mappings = DB::table('mp_script_product_mappings as mapping')
  6923. ->join('mp_products as product', 'mapping.product_id', '=', 'product.id')
  6924. ->where('mapping.script_id', $anime_script_id)
  6925. ->where('product.cpid', $cpid)
  6926. ->where('product.is_deleted', 0);
  6927. // 如果提供了 episode_number,则过滤指定集数
  6928. if ($episode_number !== null && $episode_number !== '') {
  6929. $script_products_mappings->where('mapping.episode_number', $episode_number);
  6930. }
  6931. $mappings = $script_products_mappings->select(
  6932. 'mapping.product_id',
  6933. 'mapping.episode_number',
  6934. 'product.product_name',
  6935. 'product.type',
  6936. 'product.product',
  6937. 'product.pic_prompt',
  6938. 'product.url',
  6939. 'product.pic_task_id',
  6940. 'product.pic_task_status',
  6941. 'product.three_view_image_url'
  6942. )
  6943. ->get();
  6944. // 按 product_id 分组,合并 episode_number
  6945. $productMap = [];
  6946. foreach ($mappings as $item) {
  6947. $product_id = $item->product_id;
  6948. $product_name = $item->product_name;
  6949. // 处理product_name两边的符号
  6950. $product_name = preg_replace('/^[\s*\[\]【】[]{}{}\x{3000}]+|[\s*\[\]【】[]{}{}\x{3000}]+$/u', '', $product_name);
  6951. if (!isset($productMap[$product_id])) {
  6952. $productMap[$product_id] = [
  6953. 'product_id' => $product_id,
  6954. 'product_name' => $product_name,
  6955. 'type' => (int)$item->type,
  6956. 'product' => (int)$item->product,
  6957. 'pic_prompt' => $item->pic_prompt,
  6958. 'url' => $item->url,
  6959. 'pic_task_id' => $item->pic_task_id,
  6960. 'pic_task_status' => $item->pic_task_status,
  6961. 'episode_numbers' => [],
  6962. ];
  6963. }
  6964. // 添加 episode_number(去重)
  6965. if (!in_array($item->episode_number, $productMap[$product_id]['episode_numbers'])) {
  6966. $productMap[$product_id]['episode_numbers'][] = $item->episode_number;
  6967. }
  6968. }
  6969. // 将查询到的剧本资产合并到extra_products中
  6970. // 先构建extra_products的索引(以product_id+episode_number为key)
  6971. $extraProductsMap = [];
  6972. foreach ($extra_products as $item) {
  6973. $key = getProp($item, 'product_id') . '_' . getProp($item, 'episode_number', '');
  6974. $extraProductsMap[$key] = $item;
  6975. }
  6976. // 合并剧本资产(去重:product_id+episode_number)
  6977. foreach ($productMap as $product) {
  6978. foreach ($product['episode_numbers'] as $ep_num) {
  6979. $key = $product['product_id'] . '_' . $ep_num;
  6980. // 如果不存在则添加
  6981. if (!isset($extraProductsMap[$key])) {
  6982. $extraProductsMap[$key] = [
  6983. 'product_id' => $product['product_id'],
  6984. 'product_name' => $product['product_name'],
  6985. 'type' => $product['type'],
  6986. 'product' => $product['product'],
  6987. 'pic_prompt' => $product['pic_prompt'],
  6988. 'url' => $product['url'],
  6989. 'pic_task_id' => $product['pic_task_id'],
  6990. 'pic_task_status' => $product['pic_task_status'],
  6991. 'episode_number' => $ep_num,
  6992. ];
  6993. }
  6994. }
  6995. }
  6996. // 重新赋值给extra_products
  6997. $extra_products = array_values($extraProductsMap);
  6998. }
  6999. // 处理提示词
  7000. if ($prompt && $extra_products) {
  7001. // 遍历extra_products,将prompt中的{product_name}替换为实际的product_name
  7002. foreach($extra_products as $item) {
  7003. $product_name = getProp($item, 'product_name');
  7004. if ($product_name) {
  7005. // 替换 {product_name} 为 product_name
  7006. $prompt = str_replace('{' . $product_name . '}', $product_name, $prompt);
  7007. }
  7008. }
  7009. }
  7010. // 获取最后一集序号
  7011. $end_episode_sequence = getProp($anime, 'end_episode_sequence');
  7012. if ($end_episode_sequence && $episode_number > $end_episode_sequence) Utils::throwError("20003:总集数只有{$end_episode_sequence}集,不允许继续策划下一集!");
  7013. // 转换主体列表和场景列表的格式
  7014. // 获取参考主体或场景
  7015. if ((int)$episode_number === 1) {
  7016. $ref_data = DB::table('mp_animes')->where('id', $anime_id)->first();
  7017. // $ref_data = []; // 第一集让AI自由发挥不再使用全局主体(会影响后续分集的继承问题)
  7018. }else {
  7019. $prev_episode_number = $episode_number - 1;
  7020. $ref_data = DB::table('mp_anime_episodes')->where('anime_id', $anime_id)->where('episode_number', $prev_episode_number)->where('is_default', 1)->first();
  7021. }
  7022. $ref_roles = getProp($ref_data, 'roles') ? json_decode(getProp($ref_data, 'roles'), true) : [];
  7023. $ref_scenes = getProp($ref_data, 'scenes') ? json_decode(getProp($ref_data, 'scenes'), true) : [];
  7024. $ref_props = getProp($ref_data, 'props') ? json_decode(getProp($ref_data, 'props'), true) : [];
  7025. // 合并anime_products和extra_products(extra_products优先,去重)
  7026. $anime_products = getProp($anime, 'extra_products');
  7027. $anime_products = $anime_products ? json_decode($anime_products, true) : [];
  7028. $ref_products = [];
  7029. foreach($anime_products as $item) {
  7030. $product_name = getProp($item, 'product_name');
  7031. $ref_products[$product_name] = $item;
  7032. }
  7033. // 处理extra_products,将其合并到ref_roles和ref_scenes中(入参优先)
  7034. $extra_roles = []; // 从extra_products中提取的角色
  7035. $extra_scenes = []; // 从extra_products中提取的场景
  7036. $extra_props = []; // 从extra_products中提取的道具
  7037. if ($extra_products && is_array($extra_products)) {
  7038. foreach($extra_products as $item) {
  7039. $product = (int)getProp($item, 'product');
  7040. $product_name = getProp($item, 'product_name');
  7041. if ($product_name == '旁白') continue;
  7042. $pic_prompt = getProp($item, 'pic_prompt');
  7043. $ref_products[$product_name] = $item;
  7044. if ($product === 1) {
  7045. // 角色
  7046. $extra_roles[$product_name] = [
  7047. 'role' => $product_name,
  7048. 'pic_prompt' => $pic_prompt,
  7049. 'url' => getProp($item, 'url', ''),
  7050. ];
  7051. } elseif ($product === 2) {
  7052. // 场景
  7053. $extra_scenes[$product_name] = [
  7054. 'scene' => $product_name,
  7055. 'pic_prompt' => $pic_prompt,
  7056. 'url' => getProp($item, 'url', ''),
  7057. ];
  7058. } elseif ($product === 3) {
  7059. // 道具
  7060. $extra_props[$product_name] = [
  7061. 'prop' => $product_name,
  7062. 'pic_prompt' => $pic_prompt,
  7063. 'url' => getProp($item, 'url', ''),
  7064. ];
  7065. }
  7066. }
  7067. }
  7068. // 合并ref_roles和extra_roles(extra_roles优先,去重)
  7069. $merged_roles = [];
  7070. foreach($ref_roles as $role) {
  7071. $role_name = getProp($role, 'role');
  7072. if (!isset($extra_roles[$role_name])) {
  7073. $merged_roles[$role_name] = $role;
  7074. }
  7075. }
  7076. // 添加extra_roles
  7077. foreach($extra_roles as $role_name => $role) {
  7078. $merged_roles[$role_name] = $role;
  7079. }
  7080. $ref_roles = array_values($merged_roles);
  7081. // 合并ref_scenes和extra_scenes(extra_scenes优先,去重)
  7082. $merged_scenes = [];
  7083. foreach($ref_scenes as $scene) {
  7084. $scene_name = getProp($scene, 'scene');
  7085. if (!isset($extra_scenes[$scene_name])) {
  7086. $merged_scenes[$scene_name] = $scene;
  7087. }
  7088. }
  7089. // 添加extra_scenes
  7090. foreach($extra_scenes as $scene_name => $scene) {
  7091. $merged_scenes[$scene_name] = $scene;
  7092. }
  7093. $ref_scenes = array_values($merged_scenes);
  7094. // 合并ref_props和extra_props(extra_props优先,去重)
  7095. $merged_props = [];
  7096. foreach($ref_props as $prop) {
  7097. $prop_name = getProp($prop, 'prop');
  7098. if (!isset($extra_props[$prop_name])) {
  7099. $merged_props[$prop_name] = $prop;
  7100. }
  7101. }
  7102. // 添加extra_props
  7103. foreach($extra_props as $prop_name => $prop) {
  7104. $merged_props[$prop_name] = $prop;
  7105. }
  7106. $ref_props = array_values($merged_props);
  7107. $roles_content = $this->buildEpisodeItemContent($ref_roles, 'role');
  7108. $scenes_content = $this->buildEpisodeItemContent($ref_scenes, 'scene');
  7109. $props_content = $this->buildEpisodeItemContent($ref_props, 'prop');
  7110. // 构建强制主体、场景和道具提示词
  7111. $forced_roles_prompt = "";
  7112. $forced_scenes_prompt = "";
  7113. $forced_props_prompt = "";
  7114. if (!empty($extra_roles)) {
  7115. $forced_roles_list = "";
  7116. foreach($extra_roles as $role_name => $role) {
  7117. $forced_roles_list .= $role_name . ":{" . getProp($role, 'pic_prompt') . "}\n";
  7118. }
  7119. $forced_roles_prompt = "\n\n【强制主体设定-优先级最高-不可修改】\n以下主体必须全部出现在<主体列表>中,这些主体的优先级最高,必须包含:\n" . $forced_roles_list . "\n格式要求:\n1. 主体名称:必须保持不变\n2. 主体描述:你必须根据剧情为每个主体生成主体描述(分析人物在整部剧中的姓名、年龄、性格特征、社会身份或职业背景、个人成长经历或核心故事动机,以及在剧情中的目标与行为驱动力,用一句话进行总结)\n3. 人物提示词:{}中的内容必须完整保留,不得修改\n4. 音色提示词:你必须根据角色特征生成音色提示词(描述角色的整体音色特征,包含:性别、年龄、音质、表达形式、情绪氛围,字数控制在80字以内),用{{}}框起来\n\n最终格式必须为:主体名:[你生成的主体描述]{人物提示词-保持不变}{{你生成的音色提示词}}\n\n【重要】以上每个主体在<主体列表>中的完整格式必须是:主体名:主体描述{人物提示词}{{音色提示词}},其中主体描述和音色提示词是你必须生成的内容,{}中的人物提示词必须原样保留。";
  7120. }
  7121. if (!empty($extra_scenes)) {
  7122. $forced_scenes_list = "";
  7123. foreach($extra_scenes as $scene_name => $scene) {
  7124. $forced_scenes_list .= $scene_name . ":{" . getProp($scene, 'pic_prompt') . "}\n";
  7125. }
  7126. $forced_scenes_prompt = "\n\n【强制场景设定-优先级最高-不可修改】\n以下场景必须全部出现在<场景列表>中,这些场景的优先级最高,必须包含:\n" . $forced_scenes_list . "\n格式要求:\n1. 场景名称:必须保持不变\n2. 场景描述:你必须根据剧情为每个场景生成场景描述(分析在整部剧中出现过的场景,场景描述尽量简洁贴合原文)\n3. 场景提示词:{}中的内容必须完整保留,不得修改\n\n最终格式必须为:场景名:[你生成的场景描述]{场景提示词-保持不变}\n\n【重要】以上每个场景在<场景列表>中的完整格式必须是:场景名:场景描述{场景提示词},其中场景描述是你必须生成的内容,{}中的场景提示词必须原样保留。";
  7127. }
  7128. if (!empty($extra_props)) {
  7129. $forced_props_list = "";
  7130. foreach($extra_props as $prop_name => $prop) {
  7131. $forced_props_list .= $prop_name . ":{" . getProp($prop, 'pic_prompt') . "}\n";
  7132. }
  7133. $forced_props_prompt = "\n\n【强制道具设定-优先级最高-不可修改】\n以下道具必须全部出现在<道具列表>中,这些道具的优先级最高,必须包含:\n" . $forced_props_list . "\n格式要求:\n1. 道具名称:必须保持不变\n2. 道具描述:你必须根据剧情为每个道具生成道具描述(分析在整部剧中出现过的道具,道具描述尽量简洁贴合原文)\n3. 道具提示词:{}中的内容必须完整保留,不得修改\n\n最终格式必须为:道具名:[你生成的道具描述]{道具提示词-保持不变}\n\n【重要】以上每个道具在<道具列表>中的完整格式必须是:道具名:道具描述{道具提示词},其中道具描述是你必须生成的内容,{}中的道具提示词必须原样保留。";
  7134. }
  7135. if ($anime_script_id) {
  7136. $role_demo = "2.<主体列表>只能使用以下主体,不得自定义内容(必需带上冒号后的具体描述以及{}和{{}}中的内容,原样输出)。{$forced_roles_prompt}";
  7137. }else if ($roles_content) {
  7138. $role_demo = "2.<主体列表>可在以下主体中选择多个(根据拆分出的分镜剧本来确认具体几个,不要出现旁白角色),不得自定义内容(必需带上冒号后的具体描述以及{}和{{}}中的内容,原样输出)。
  7139. 主体范围:\n {$roles_content}\n
  7140. 2.1如果本集剧情内容中出现了和给出的<主体列表>中不存在的新主体(或原主体发生了比较大的年龄、服装或身体变化),则不用在上述主体中选择,而是按照相同的格式新增一个主体显示在<主体列表>中。(以下是格式要求和生成要求,仅适用新出现的主体):
  7141. - 新主体分以下三部分:主体描述、人物提示词和音色提示词,格式必须为:主体名:主体描述{人物提示词}{{音色提示词}}
  7142. - 新主体的主体描述是分析人物在整部剧中的姓名、年龄、性格特征、社会身份或职业背景、个人成长经历或核心故事动机,以及在剧情中的目标与行为驱动力,不需要描述外貌、穿着或身体特征,重点突出心理结构、行为模式与叙事功能(缩略成一句话讲述完毕)。
  7143. - 新主体的人物提示词需要根据当前分析出来的美术风格生成(每个人物都只允许一套服装,如果多套服装则需区分主体名),包含性别(男、女)、大概年龄或年龄段(少年、中年、老年等)、人种(亚洲人、欧洲人)、发型(直发卷发等)、发色(黑色、棕色、红色、白色等)、整体视觉风格,以及详细的穿着风格和服装细节、面部特征(戴金丝眼镜、脸上有刀疤等,无面部特征可忽略)。描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合人物身份设定。该提示词(字数50-100字左右)在主体描述后用{}框起来。
  7144. - 新主体的音色提示词放在描述的最后用{{}}框起来。音色提示词需描述角色的整体音色特征,包含:性别、年龄(约XX岁)、音质(如清冷、沉稳、磁性等)、表达形式(如叙述感、旁白式等)、情绪氛围(如空灵韵味、看透世事的清冷等),字数控制在80字以内。举例:成年女性(约25-35岁),音色清冷且富有叙述感,语速平缓,带有仙侠剧特有的空灵韵味。{$forced_roles_prompt}";
  7145. }else {
  7146. $role_demo = "2.<主体列表>中出现的“主体名”必须尽可能统一,严禁出现角色-科学家、角色-复仇者、角色-领导者等带有身份、职业、标签类的命名,只能使用服装差异(如校服装、卫衣装、西装)来区分同一角色,如角色-校服装等。
  7147. - <主体列表>分以下三部分:主体描述、人物提示词和音色提示词,格式必须为:主体名:主体描述{人物提示词}{{音色提示词}}
  7148. - <主体列表>的主体描述是分析人物在整部剧中的姓名、年龄、性格特征、社会身份或职业背景、个人成长经历或核心故事动机,以及在剧情中的目标与行为驱动力,不需要描述外貌、穿着或身体特征,重点突出心理结构、行为模式与叙事功能(缩略成一句话讲述完毕)。
  7149. - <主体列表>的人物提示词需要根据当前分析出来的美术风格生成(每个人物都只允许一套服装,如果多套服装则需区分主体名),包含性别(男、女)、大概年龄或年龄段(少年、中年、老年等)、人种(亚洲人、欧洲人)、发型(直发卷发等)、发色(黑色、棕色、红色、白色等)、整体视觉风格,以及详细的穿着风格和服装细节、面部特征(戴金丝眼镜、脸上有刀疤等,无面部特征可忽略)。描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合人物身份设定。该提示词(字数50-100字左右)在主体描述后用{}框起来。
  7150. - <主体列表>的音色提示词放在描述的最后用{{}}框起来。音色提示词需描述角色的整体音色特征,包含:性别、年龄(约XX岁)、音质(如清冷、沉稳、磁性等)、表达形式(如叙述感、旁白式等)、情绪氛围(如空灵韵味、看透世事的清冷等),字数控制在80字以内。举例:成年女性(约25-35岁),音色清冷且富有叙述感,语速平缓,带有仙侠剧特有的空灵韵味。{$forced_roles_prompt}";
  7151. }
  7152. if ($anime_script_id) {
  7153. $scene_demo = "3.<场景列表>只能使用以下场景,不得自定义内容(必需带上冒号后的具体描述以及{}和{{}}中的内容,原样输出)。{$forced_scenes_prompt}";
  7154. }else if ($scenes_content) {
  7155. $scene_demo = "3.<场景列表>可在以下场景中选择多个(需带上冒号后的具体描述),根据拆分出的分镜剧本来确认具体几个,不得自定义(必需带上冒号后的具体描述以及{}中的内容,原样输出)。
  7156. 场景范围:\n {$scenes_content}\n
  7157. 3.1如果本集剧情内容中出现了和给出的<场景列表>中不存在的新场景,则不用在上述场景中选择,而是按照相同的格式新增一个场景显示在<场景列表>中,必须严格注意不要出现任何角色和人物。(以下是格式要求和生成要求,仅适用新出现的场景):。
  7158. - 新场景分以下两部分:场景描述和场景提示词,格式必须为:场景名:场景描述{场景提示词}
  7159. - 新场景的场景描述是分析在整部剧中出现过的场景,场景描述尽量简洁贴合原文。
  7160. - 新场景的场景提示词用于视觉呈现的场景环境描述,该场景需包含环境氛围、光线效果、空间布局、色彩基调、视觉风格等,描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合剧情设定(可参考以下示例的场景列表中使用{}框起来的提示词,字数150-200字,需带上提炼出来的美术风格)。该提示词在场景描述后用{}框起来。{$forced_scenes_prompt}\n";
  7161. }else {
  7162. $scene_demo = "3.<场景列表>与主体列表同理,需尽可能的对场景环境进行详细描述,必须严格注意不要出现任何角色和人物。
  7163. - <场景列表>分以下两部分:场景描述和场景提示词,格式必须为:场景名:场景描述{场景提示词},注意不要出现{{}}这样的双大括号。
  7164. - <场景列表>的场景描述是分析在整部剧中出现过的场景,场景描述尽量简洁贴合原文。
  7165. - <场景列表>的场景提示词用于视觉呈现的场景环境描述,该场景需包含环境氛围、光线效果、空间布局、色彩基调、视觉风格等,描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合剧情设定(可参考以下示例的场景列表中使用{}框起来的提示词,字数150-200字,需带上提炼出来的美术风格)。该提示词在场景描述后用{}框起来。{$forced_scenes_prompt}\n";
  7166. }
  7167. if ($anime_script_id) {
  7168. $prop_demo = "4.<道具列表>只能使用以下道具,不得自定义内容(必需带上冒号后的具体描述以及{}和{{}}中的内容,原样输出)。{$forced_props_prompt}";
  7169. }else if ($props_content) {
  7170. $prop_demo = "4.<道具列表>可在以下道具中选择多个(需带上冒号后的具体描述),根据拆分出的分镜剧本来确认具体几个,不得自定义(必需带上冒号后的具体描述以及{}中的内容,原样输出)。
  7171. 道具范围:\n {$props_content}\n
  7172. 4.1如果本集剧情内容中出现了和给出的<道具列表>中不存在的新道具,则不用在上述道具中选择,而是按照相同的格式新增一个道具显示在<道具列表>中,必须严格注意不要出现任何角色和人物。(以下是格式要求和生成要求,仅适用新出现的道具):。
  7173. - 新道具分以下两部分:道具描述和道具提示词,格式必须为:道具名:道具描述{道具提示词}
  7174. - 新道具的道具描述是分析在整部剧中出现过的道具,道具描述尽量简洁贴合原文。
  7175. - 新道具的道具提示词用于视觉呈现的道具环境描述,该道具需包含环境氛围、光线效果、空间布局、色彩基调、视觉风格等,描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合剧情设定(可参考以下示例的道具列表中使用{}框起来的提示词,字数150-200字,需带上提炼出来的美术风格)。该提示词在道具描述后用{}框起来。{$forced_scenes_prompt}\n";
  7176. }else {
  7177. $prop_demo = "4.<道具列表>与主体列表同理,需尽可能的对道具环境进行详细描述,必须严格注意不要出现任何角色和人物。
  7178. - <道具列表>分以下两部分:道具描述和道具提示词,格式必须为:道具名:道具描述{道具提示词},注意不要出现{{}}这样的双大括号。
  7179. - <道具列表>的道具描述是分析在整部剧中出现过的道具,道具描述尽量简洁贴合原文。
  7180. - <道具列表>的道具提示词用于视觉呈现的道具环境描述,该道具需包含环境氛围、光线效果、空间布局、色彩基调、视觉风格等,描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合剧情设定(可参考以下示例的道具列表中使用{}框起来的提示词,字数150-200字,需带上提炼出来的美术风格)。该提示词在道具描述后用{}框起来。{$forced_scenes_prompt}\n";
  7181. }
  7182. // 提取文件内容
  7183. if ($file) {
  7184. $uploaded_content = $this->extractFileContent($file);
  7185. if (!$uploaded_content) {
  7186. Utils::throwError('20003:无法提取文件内容,请检查文件格式');
  7187. }
  7188. } elseif ($script_id) {
  7189. $uploaded_content = $this->getContentByScriptId($script_id);
  7190. if (!$uploaded_content) {
  7191. Utils::throwError('20003:无法获取剧本内容,请检查剧本');
  7192. }
  7193. } elseif ($bid) {
  7194. $uploaded_content = $this->getContentByBid($bid);
  7195. if (!$uploaded_content) {
  7196. Utils::throwError('20003:无法获取书籍内容,请检查书籍');
  7197. }
  7198. } elseif ($content) {
  7199. $uploaded_content = filterContent((string)getProp($data, 'content', ''));
  7200. }
  7201. // elseif ($prompt) {
  7202. // $uploaded_content = filterContent($prompt);
  7203. // }
  7204. else {
  7205. $uploaded_content = '';
  7206. }
  7207. // 美术风格
  7208. $input_art_style = getProp($data, 'art_style');
  7209. if (!$input_art_style) {
  7210. $mappedArtStyle = getProp($anime, 'art_style');
  7211. }else {
  7212. $mappedArtStyle = $this->getArtStylePromptByInput($input_art_style);
  7213. }
  7214. if (!$mappedArtStyle) {
  7215. $mappedArtStyle_prompt = '美术风格:根据剧本内容自定义美术风格';
  7216. }else {
  7217. if (strstr($mappedArtStyle, '基础画风风格词')) {
  7218. $mappedArtStyle_prompt = "美术风格强制设置如下(原样输出,不得更改任何内容):\n{$mappedArtStyle}\n";
  7219. }else {
  7220. $art_style_type = $input_art_style ? $input_art_style : getProp($anime, 'art_style_type');
  7221. $mappedArtStyle_prompt = "美术风格强制设置如下(原样输出,不得更改任何内容):\n基础画风风格词: {$art_style_type}\n{$mappedArtStyle}\n";
  7222. }
  7223. }
  7224. $prompt = trim((string)getProp($data, 'prompt', '确认分镜大纲'));
  7225. // $demo = "你是一个专业的文档分析助手及资深编剧,请根据用户提供的文档内容完成示例格式的内容输出(需包含以下几个板块: <剧集标题><故事梗概><美术风格><旁白音色提示词><主体列表><场景列表><分段剧本>,每个板块之间必须使用###分隔,#不能多也不能少;同时板块之间需满足以下要求:\n
  7226. // 强制要求:
  7227. // 1.你只负责与剧本相关的内容,如果用户输入与剧本无关的内容,请告知用户并拒绝继续回答,此为第一优先原则,不满足此原则就忽略其他要求并停止继续输出
  7228. // 2.如果用户输入修改美术风格、修改画风这类内容,请告知用户不可修改美术风格,并拒绝继续回答
  7229. // 3.以上两条要求对修改主体角色或场景不生效,允许用户调整主体角色说明和场景描述等
  7230. // 4.用户如果要求你帮忙写一个故事或者剧本,此类要求不属于与剧本无关,请帮助用户完成请求
  7231. // 5.强制按照示例格式返回内容,不要更改格式或随意增加符号
  7232. // 6.如果给出的本集剧情内容是剧本格式,则不要改动剧本内容(必须强制使用原文内容),严格按照剧本的旁白和台词进行分镜的策划,不要跳过任何剧情
  7233. // 7.分镜不是二次创作,必须严格“原文拆句”。优先把原文中可直接成镜头的句子拆成分镜,禁止把两句合并后再脑补过渡。
  7234. // 8.画面描述必须直接使用,禁止新增任何原文没有写出的动作、人物、道具、地点、气氛、结果或因果。
  7235. // 9.不要把“打开衣柜”扩写成“伸手轻抚嫁衣”,不要把“管家拎着两个箱子跟在后面”改成“沈耀和管家一起拎箱子”。主语、动作、数量、先后顺序必须完全按原文。
  7236. // 10.分镜要和场景列表、人物主体保持一致\n
  7237. // 普通要求:
  7238. // 1.剧集标题必须与文档内容高度相关,剧集序号是{$episode_number}。
  7239. // {$role_demo}
  7240. // {$scene_demo}
  7241. // 4.<分段剧本>按照“分段模式”进行拆分和组织,要求如下:
  7242. // 4.1片段分割逻辑(优先级从高到低):
  7243. // - 第一优先级:时长上限(单个片段不超过15秒,超过必须切为新片段)
  7244. // - 第二优先级:叙事节拍Beat(一个完整的事件/动作/情绪起落单元,如一段对话的来回、一个动作序列:推门→看见→震惊、一次闪回/回忆、旁白+画面组合)
  7245. // - 第三优先级:语义块(按“谁、在哪、做什么、情绪怎样”聚合,如同一段旁白/独白、同一个角色的连续动作、同一个视角的连续镜头)
  7246. // - 只要事件没结束、情绪没断,哪怕换场景,也留在同一个片段里
  7247. // 4.2片段结构:片段数量+旁白音色+多个片段(每个片段由多个分镜组合而成(片段序号格式:片段1、片段2...))
  7248. // - 片段数量格式为: 片段数量:8
  7249. // - 旁白音色需描述旁白的整体音色特征,包含:性别、年龄(约XX岁)、音质(如清冷、沉稳、磁性等)、表达形式(如叙述感、旁白式等)、情绪氛围(如空灵韵味、看透世事的清冷等),字数控制在80字以内。举例:成年女性(约25-35岁),音色清冷且富有叙述感,语速平缓,带有仙侠剧特有的空灵韵味。
  7250. // - 片段序号格式必须如下: ##片段+片段序号\n时长:xxs,例如: ##片段1\n时长:12s
  7251. // 4.3分镜结构:每个分镜包含以下字段:
  7252. // - 分镜序号在片段内用1、2、3显示,如分镜1、分镜2,并且分镜序号需连续(跨片段中的分镜序号需连续,如片段2中的分镜序号从片段1的最大序号+1开始)
  7253. // - 场景:必须选择本集中的<场景列表>中的一个(不带详细表述)
  7254. // - 画面:【构图/视角】+画面描述(越简单越好,对视觉场景/实景的描述,不描述氛围,比如紧张、压抑的氛围)。举例:[近景特写,浅景深]季潇握着手机贴在耳边,指关节因为用力而泛白。她的眼眶微红,睫毛在狂风中颤抖,嘴唇紧抿,眼神中闪烁着最后一点希冀的微光。
  7255. // - 画面中如果出现主体,只能选择上述主体列表中的一个,主体的名称也需要保持一致(不带主体具体描述)
  7256. // - 运镜:场景+画面描述+运镜
  7257. // - 配音台词:格式为“中文配音:[角色]台词”,角色配音台词和语言由模型匹配生成,角色必须对应本集中的<主体列表>中的一个(非对话内容则统一固定为“旁白”角色)
  7258. // - 台词内容需满足以下要求: (1. 台词来源规则:台词内容必须逐字摘录自小说原文中的人物直接对话(带引号的内容),不允许改写、总结、压缩、扩写或拼接原文对白,不允许生成原文中不存在的对话。2. 非对话过滤:旁白、心理描写、环境描写、动作描写等内容,禁止出现在台词内容中。3. 空台词规则:如果该分镜中没有人物对话,则“台词内容”必须返回“无”,严禁输出“无台词”、“没有”等任何替代文本。4. 禁止推测:不得根据语境补全人物可能说的话。5.如果没有原文,直接返回空)
  7259. // - 背景音效:背景音效需根据内容和画面生成,贴合当前画面描述和动作,例如:背景音效:高跟鞋的脚本声
  7260. // 5.$mappedArtStyle_prompt\n\n
  7261. // 示例格式:\n
  7262. // 第1集:隐形的守护者
  7263. // ###故事梗概
  7264. // 内容梗概:许芸归家遭徐母毒打,她冷静周旋并编造谎言掩盖行踪,引得一旁阴沉的卞大伟愈发急躁。半个月后,徐母离家,卞大伟兽性大发欲行不轨。生死关头,隐形的程景现身肉搏,屋内家具横飞,卞大伟被“空气”重创。许芸趁乱逃出,却惊见地面凭空浮现一串血脚印。意识到守护者为救自己负伤,许芸不再逃避,对着虚无的空气执拗告白,誓要与这看不见的爱人共进退。
  7265. // ###美术风格
  7266. // 基础画风风格词:胡金铨武侠
  7267. // 视觉风格描述:整体采用复古武侠电影美学,以低饱和度的青灰与墨色调为主,营造冷峻、肃杀的江湖意境。光影上运用强烈的侧逆光与深邃硬阴影,完美契合“墨影剑客”神秘莫测的特质。画面带有35mm胶片的粗粝质感,构图简约且富有张力,展现出深沉的复仇叙事氛围。
  7268. // ###主体列表
  7269. // 陆沉:冷酷神秘的复仇剑客,陆家庄唯一幸存者,身负绝世武功与血海深仇。{全景,正面拍摄,青年男性,亚洲人,黑色长发束起,五官轮廓深邃。服饰穿搭:黑色连帽斗篷,深灰色交领劲装,黑色束腰,背负一柄漆黑宽大的无锋重剑。姿态:站立。}{{青年男性(约25-30岁),音色冷峻、低沉,语速平稳缓慢,透着一股拒人于千里之外的孤傲与肃杀。}}
  7270. // 刀疤脸:武林盟的嚣张爪牙,性格暴戾、欺软怕硬,是赵无极麾下的打手。{全景,正面拍摄,中年男性,亚洲人,短发,左脸有一道明显的深长刀疤。服饰穿搭:土黄色粗布武士服,腰间挂着一枚古铜色的“武林盟”令牌。姿态:站立。}{{中年男性(约35-45岁),音色粗犷、沙哑,语气嚣张跋扈,带有明显的威胁感和市井痞气。}}
  7271. // 老李:悦来酒馆的老板,胆小卑微的底层百姓,在乱世中艰难求生。{全景,正面拍摄,老年男性,亚洲人,灰白乱发,面容消瘦且布满皱纹。服饰穿搭:破旧的深蓝色粗布长袍,腰间系着一条带有污渍的灰色围裙。姿态:站立。}{{老年男性(约60岁以上),音色苍老、沙哑且带有颤抖,语气唯唯诺诺,充满了卑微与惊恐。}}
  7272. // ###场景列表
  7273. // 边境小镇街道:夜幕笼罩下的边陲小镇,风沙肆虐,街道两旁是破旧的木质建筑,透着一股荒凉与肃杀的气息,是故事发生的地理背景。{远景,夜幕下的边境小镇,破败的木质建筑错落有致,风沙在空旷的街道上飞扬,远处有微弱的灯火。无人物。}
  7274. // 悦来酒馆:酒馆内部陈设简陋,木质桌椅散乱分布,柜台上摆放着酒坛。室内光线昏暗,只有几盏摇曳的油灯提供微弱的暖光,与室外的冷调形成对比。{室内场景,昏暗的古风酒馆内部,粗糙的木质桌椅,木质柜台与酒坛,空气中浮动着尘埃,强烈的侧逆光勾勒出环境轮廓,深邃的硬阴影。无人物。}
  7275. // ###分段剧本
  7276. // 片段数量:8
  7277. // 旁白音色:成年男性(约35-50岁),音色厚重且富有磁性,带有老电影叙事般的沧桑感,语调低沉平稳,富有江湖气息。
  7278. // ##片段1
  7279. // 时长:12s
  7280. // 分镜1
  7281. // 场景:边境小镇街道
  7282. // 画面:[大远景,冷色调,高对比度] 漫天黄沙在狂风中如怒龙翻滚,遮蔽了微弱的星光。悦来酒馆残破的木门在风中剧烈摇晃,铁链撞击木板发出沉重的闷响,四周建筑尽显荒凉。
  7283. // 运镜:大远景固定镜头,随后风沙席卷向镜头形成遮挡转场。
  7284. // 配音台词:
  7285. // 背景音效:
  7286. // 分镜2
  7287. // 场景:悦来酒馆
  7288. // 画面:[全景,暖色调阴影构图] 酒馆内部昏暗,几盏油灯在风中疯狂摇曳,投射出扭曲的长影。角落里的食客低头缩颈,双手紧紧攥着粗瓷碗,空气中漂浮着细小的尘埃。
  7289. // 运镜:缓慢横移,掠过阴影中瑟缩的食客。
  7290. // 配音台词:
  7291. // 背景音效:
  7292. // 分镜3
  7293. // 场景:悦来酒馆
  7294. // 画面:[中景,三分法构图] 三名大汉围坐在正中央的圆桌旁,腰间挂着的“武林盟”金色令牌在灯火下闪烁。桌上残羹冷炙横陈,一名大汉正张狂地大笑,脚踩在长凳上。
  7295. // 运镜:从中景推向令牌特写。
  7296. // 配音台词:
  7297. // 背景音效:
  7298. // 分镜4
  7299. // 场景:悦来酒馆
  7300. // 画面:[特写,低角度仰拍] 刀疤脸横肉颤动,嘴角挂着轻蔑的笑。他随手倾覆手中的酒碗,琥珀色的液体在空中划过弧线,重重溅在老李打着补丁的布鞋和裤脚上。
  7301. // 运镜:极速推向酒水溅起的瞬间。
  7302. // 配音台词:
  7303. // 背景音效:
  7304. // 分镜5
  7305. // 镜头描述
  7306. // 场景:悦来酒馆
  7307. // 画面:[近景,荷兰角倾斜构图] 刀疤脸斜睨着眼,昏暗的灯光勾勒出他脸上狰狞的伤疤。他身体前倾,阴影完全笼罩了对面的老李,眼神如毒蛇般阴冷。
  7308. // 运镜:固定机位,利用倾斜构图制造压迫感。
  7309. // 配音台词:中文配音:[刀疤脸]老李头,这酒里掺水了吧?
  7310. // 背景音效:
  7311. // \n\n";
  7312. $demo = "你是一个国际顶尖的执行导演/编剧,熟悉各种风格的影视化落地,我需要你将这个剧本调整一下,将其变成一个高质量的影视分镜剧本(需包含以下几个板块: <剧集标题><故事梗概><美术风格><旁白音色提示词><主体列表><场景列表><道具列表><分段剧本>,每个板块之间必须使用###分隔,#不能多也不能少),要求画面和构图等进行高质量影视化改编,快节奏,改编的格式为先建立主体角色库、场景建库(其中场景建库的时候请分析故事发生的时候场景为白天还是晚上,建库的时候一起标注出来)以及关键的道具库。
  7313. 与此同时请将这个文本的情绪做一个分析,并且根据我下面提供的《情绪-视听语言映射表》进行一个文本情绪与视听语言的内容匹配。然后将每一个分镜按照「场景/场景时间(白天/晚上/傍晚等)」「画面」「运镜」「配音台词」「背景音效」的格式来划分,请按照每分钟25个左右的分镜的节奏帮我生成(这是一个整体性的标准,如果文本的情绪分析需要更多或者更少的镜头数量,请根据具体情况调整),分镜头请细腻的拆分文本中的内容,将动作、场景转换、事件的逻辑关系等内容清晰的交代出来,分镜之间需要呈现场景之间的转换逻辑,不要突然跳到下一个场景之中。于此同时保持镜头剪辑的顿挫感,节奏不要太平均。
  7314. 每一个分镜的「画面」请参考上一个分镜中人物的状态/动作和位置来写,保持人物状态以及在场景中的位置的一致性,不要断裂感,同时也不要直接在内容里写“参考上个分镜”这种话,因为视频模型无法识别,使用模型可以直接识别的文本内容。每一个分镜中需要出现的内容都请标出,例如出场角色,出场道具等等。请尽可能详细的描述画面内容。
  7315. 「背景音效」层面不要生成背景音乐,只需要生成音效和角色台词或者旁白,台词请先标注说台词的角色,旁白和角色心理活动时注明角色不要张嘴说话。\n
  7316. 普通要求:
  7317. 1.剧集标题必须与文档内容高度相关,剧集序号是{$episode_number}。
  7318. {$role_demo}
  7319. {$scene_demo}
  7320. {$prop_demo}
  7321. 5.<分段剧本>按照“分段模式”进行拆分和组织,要求如下:
  7322. 5.1片段分割逻辑(优先级从高到低):
  7323. - 第一优先级:时长上限(单个片段不超过15秒,超过必须切为新片段)
  7324. - 第二优先级:叙事节拍Beat(一个完整的事件/动作/情绪起落单元,如一段对话的来回、一个动作序列:推门→看见→震惊、一次闪回/回忆、旁白+画面组合)
  7325. - 第三优先级:语义块(按“谁、在哪、做什么、情绪怎样”聚合,如同一段旁白/独白、同一个角色的连续动作、同一个视角的连续镜头)
  7326. - 只要事件没结束、情绪没断,哪怕换场景,也留在同一个片段里
  7327. 5.2片段结构:片段数量+旁白音色+多个片段(每个片段由多个分镜组合而成(片段序号格式:片段1、片段2...))
  7328. - 片段数量格式为: 片段数量:8
  7329. - 旁白音色需描述旁白的整体音色特征,包含:性别、年龄(约XX岁)、音质(如清冷、沉稳、磁性等)、表达形式(如叙述感、旁白式等)、情绪氛围(如空灵韵味、看透世事的清冷等),字数控制在80字以内。举例:成年女性(约25-35岁),音色清冷且富有叙述感,语速平缓,带有仙侠剧特有的空灵韵味。
  7330. - 片段序号格式必须如下: ##片段+片段序号\n时长:xxs,例如: ##片段1\n时长:12s
  7331. 5.3分镜结构:每个分镜包含以下字段:
  7332. - 分镜序号在片段内用1、2、3显示,如分镜1、分镜2,并且分镜序号需连续(跨片段中的分镜序号需连续,如片段2中的分镜序号从片段1的最大序号+1开始),分镜序号前后不要带任何其他符号,特别是*符号。
  7333. - 出场角色: 如果有主体出现在本镜,则必须选择本集中的<主体列表>中的一个(如必须使用“沈秋岚-程家居家围裙造型”,而不是“沈秋岚”,不得缺少“-”前后和“:”前面的任何内容,不能自创)
  7334. - 场景:必须选择本集中的<场景列表>中的一个(如需使用“酒店会客室-景池筹备会版”,而不是“酒店会客室”,不得缺少“-”前后和“:”前面的任何内容,不能自创)
  7335. - 出场道具:如果有道具出现在本镜,则必须选择本集中的<道具列表>中的一个(如需使用“酒杯-高脚杯”,而不是“酒杯”或“高脚杯”,不得缺少“-”前后和“:”前面的任何内容,不能自创)
  7336. - 画面:【构图/视角】+画面描述(越简单越好,对视觉场景/实景的描述,不描述氛围,比如紧张、压抑的氛围)。举例:[近景特写,浅景深]季潇握着手机贴在耳边,指关节因为用力而泛白。她的眼眶微红,睫毛在狂风中颤抖,嘴唇紧抿,眼神中闪烁着最后一点希冀的微光。
  7337. - 画面中如果出现主体,只能选择上述<主体列表>中的一个,主体的名称也必须保持一致(如必须使用“沈秋岚-程家居家围裙造型”,而不是“沈秋岚”,不得缺少“-”前后和“:”前面的任何内容,不能自创)
  7338. - 画面中如果出现道具,只能选择上述<道具列表>中的一个,道具的名称也必须保持一致(如必须使用“酒杯-高脚杯”,而不是“酒杯”或“高脚杯”,不得缺少“-”前后和“:”前面的任何内容,不能自创)
  7339. - 运镜:场景+画面描述+运镜
  7340. - 运镜描述中如果出现主体或道具,逻辑需同画面一直,必须选择<主体列表>或<道具列表>中的一个,名称也必须保持一致
  7341. - 配音台词:格式为“中文配音:[角色]台词”,角色配音台词和语言由模型匹配生成,角色必须对应本集中的<主体列表>中的一个(非对话内容则统一固定为“旁白”角色,[角色]的角色名必须完整使用主体列表的主体名,如必须使用“沈秋岚-程家居家围裙造型”,而不是“沈秋岚”,不得缺少“-”前后和“:”前面的任何内容,不能自创,不能自创)
  7342. - 台词内容需满足以下要求: (1. 台词来源规则:台词内容必须逐字摘录自小说原文中的人物直接对话(带引号的内容),不允许改写、总结、压缩、扩写或拼接原文对白,不允许生成原文中不存在的对话。2. 非对话过滤:旁白、心理描写、环境描写、动作描写等内容,禁止出现在台词内容中。3. 空台词规则:如果该分镜中没有人物对话,则“台词内容”必须返回“无”,严禁输出“无台词”、“没有”等任何替代文本。4. 禁止推测:不得根据语境补全人物可能说的话。5.如果没有原文,直接返回空)
  7343. - 背景音效:背景音效需根据内容和画面生成,贴合当前画面描述和动作,例如:背景音效:高跟鞋的脚本声
  7344. 6.《情绪-视听语言映射表》:
  7345. 6.1 场景空间与时间心理学 (Scene & Temporality)
  7346. 维度 | 视听特征 | 对应核心情绪 | 心理学/叙事原理
  7347. -----|---------|------------|----------------
  7348. 场景空间 | 封闭狭小 (Corridors/Closets) | 压抑、幽闭恐惧 | 物理限制映射心理自由的剥夺。
  7349. 场景空间 | 极度开阔 (Empty Plains/Desert) | 孤独、渺小、疏离 | 剥离社会性支撑,强化个体的虚无感。
  7350. 时间选择 | 黄金时刻 (Golden Hour) | 怀旧、温情、希望 | 柔和漫反射光线符合大脑对“美好记忆”的视觉加工。
  7351. 时间选择 | 蓝调时刻/深夜 (Blue/Night) | 冷漠、忧郁、未知危险 | 冷色调抑制血清素,增强感官的不安全感。
  7352. 6.2 剪辑节奏与感知阈值 (Shot Duration & Rhythm)
  7353. 维度 | 视听特征 | 对应核心情绪 | 心理学/叙事原理
  7354. -----|---------|------------|----------------
  7355. 长镜头 | >15秒的静止或缓慢移动 | 沉思、肃穆、煎熬 | 强迫观众进入实时流逝,产生生理性的存在感。
  7356. 快切剪辑 | <2秒的频繁切换 | 混乱、亢奋、惊恐 | 模拟高肾上腺素下的视觉捕捉,剥夺深度思考时间。
  7357. 节奏错位 | 非同步切分 (L-cut/J-cut) | 扭曲、不协调 | 破坏连续性假定,产生心理上的不适与悬疑感。
  7358. 6.3 运镜调度与情绪调动 (Cinematic Movement & Affect)
  7359. 运镜方式 | 物理特征 | 心理/情绪功能 | 专业应用场景
  7360. --------|---------|--------------|-------------
  7361. 推镜头 (Dolly In) | 摄影机物理接近主体 | 专注、发现、心理逼近。通过压缩背景空间,强制观众忽略环境,进入主体的内心世界。 | 角色顿悟、秘密揭晓、情感升温。
  7362. 拉镜头 (Dolly Out) | 摄影机物理远离主体 | 疏离、解脱、宿命感。将主体置于广阔背景中,显示主体的渺小或强调事情已成定局。 | 悲剧结尾、孤独的告别、审视全局。
  7363. 摇镜头 (Pan/Tilt) | 轴心转动,视点改变 | 观察、审读、惊奇感。模拟人眼转头,用于揭示空间关系或进行逻辑上的“连接”。 | 发现潜伏的危机、环视战场、建立空间地理志。
  7364. 跟拍 (Tracking Shot) | 保持与主体同速移动 | 强烈的参与感、伴随性焦虑。让观众感觉自己是角色的共谋者或影子,产生“实时感”。 | 逃亡、快步穿行于走廊、展示角色的行动力。
  7365. 横移 (Truck/Side Track) | 与主体平行横向移动 | 客观审视、叙事流逝。像卷轴画一样展开,产生一种冷静、观察者的旁白视角。 | 展示环境细节、两个角色在边走边谈时的心理博弈。
  7366. 手持/肩扛 (Handheld) | 生理性不规则晃动 | 不安、真实感、心理动荡。模拟肉眼观察,通过不稳定性唤起观众的本能警觉。 | 冲突现场、纪录片式纪实感、角色精神崩溃。
  7367. 斯坦尼康 (Steadicam) | 流畅、近乎滑行的移动 | 全知感、梦幻、无缝观察。消除物理重力感,产生一种“上帝视角”或“游魂般”的观察。 | 漫长而复杂的转场、舞会等优雅场景的穿梭。
  7368. 希区柯克变焦 (Dolly Zoom) | 推进同时反向变焦 | 眩晕、世界崩塌、极度惊骇。空间透视的物理扭曲直接引发生理性失衡。 | 角色得知真相的瞬间、高度恐惧。
  7369. 升降/摇臂 (Crane Shot) | 垂直或斜向空间位移 | 释放、崇高、超越性。打破二维平面的限制,将情绪从微观拉向宏观。 | 史诗感的结局、战场全景、情绪的爆发式升华。
  7370. 6.4 构图、色彩与符号学 (Composition & Aesthetics)
  7371. 维度 | 视听特征 | 对应核心情绪 | 心理学原理
  7372. -----|---------|------------|------------
  7373. 角度 | 仰角 (Low Angle) | 威严、恐惧、崇高 | 模拟幼年视角,确立权力层级。
  7374. 角度 | 俯角 (High Angle) | 脆弱、无助、卑微 | 模拟审视感,削弱主体力量。
  7375. 构图 | 荷兰坡 (Dutch Tilt) | 疯狂、崩溃、失衡 | 重力轴偏离导致前庭系统产生失衡反应。
  7376. 色彩 | 极高饱和度/冷暖撞色 | 狂喜、冲突、超现实 | 强烈的视觉刺激诱发情绪阈值波动。
  7377. 6.5 进阶视听修辞与认知干预 (Advanced AV Rhetoric)
  7378. 修辞策略 | 技术实现 | 心理/情绪效应 | 理论基石
  7379. --------|---------|--------------|----------
  7380. 声画对位 (Contrapuntal AV) | 在极端残酷/激烈的画面中配以极其舒缓优雅的音乐(如古典乐)。 | 荒诞恐怖感、神圣的残酷、间离效果。通过感官冲突迫使观众从原始情绪中抽离,进入形而上的思考。 | 安德烈·塔可夫斯基《雕刻时光》、库布里克式美学。
  7381. 非平衡构图 (Compositional Tension) | 突破平衡法则,将视觉重点(Eye-tracking)置于画面边缘或极端角落。 | 本能警觉、不安全感、期待感。利用大脑对“视觉缺失”的补充本能,暗示画面外存在不可见的威胁。 | 格式塔心理学、眼动追踪实验。
  7382. 认知负荷调控 (Neurocinematics) | 利用快速剪辑抑制前额叶皮层(理性区),激活杏仁核(情绪区)。 | 纯粹感官化、生存模式切换。若要观众思考,则延长镜头以激活认知分析;若要观众受惊,则通过视觉过载切断其逻辑判断。 | 神经电影学 (Neurocinematics)。
  7383. 6.6 声音设计与潜意识影响 (Soundscape)
  7384. 维度 | 视听特征 | 对应核心情绪 | 心理学/生理学原理
  7385. -----|---------|------------|------------------
  7386. 音色 | 极低频音 (Infra-sound) | 潜伏恐惧、生理不安 | 模仿自然灾害频率,唤醒生物避害本能。
  7387. 环境声 | 极度死寂 (Silence) | 震惊、神圣、虚无 | 听觉剥夺放大视觉信号,产生心理张力。
  7388. 非现场音 | 不和谐音/尖锐音效 | 精神紧绷、躁郁 | 高频声音直接刺激杏仁核,引发防御机制。
  7389. 理论基石:
  7390. - The Five C's of Cinematography by Joseph V. Mascelli
  7391. - Film Art: An Introduction by David Bordwell
  7392. - Sight, Sound, Motion by Herbert Zettl
  7393. - Notes on the Cinematograph by Robert Bresson
  7394. \n\n
  7395. 7.{$mappedArtStyle_prompt}
  7396. 示例格式:\n
  7397. 第1集:隐形的守护者
  7398. ###故事梗概
  7399. 内容梗概:许芸归家遭徐母毒打,她冷静周旋并编造谎言掩盖行踪,引得一旁阴沉的卞大伟愈发急躁。半个月后,徐母离家,卞大伟兽性大发欲行不轨。生死关头,隐形的程景现身肉搏,屋内家具横飞,卞大伟被“空气”重创。许芸趁乱逃出,却惊见地面凭空浮现一串血脚印。意识到守护者为救自己负伤,许芸不再逃避,对着虚无的空气执拗告白,誓要与这看不见的爱人共进退。
  7400. ###美术风格
  7401. 基础画风风格词:胡金铨武侠
  7402. 视觉风格描述:整体采用复古武侠电影美学,以低饱和度的青灰与墨色调为主,营造冷峻、肃杀的江湖意境。光影上运用强烈的侧逆光与深邃硬阴影,完美契合“墨影剑客”神秘莫测的特质。画面带有35mm胶片的粗粝质感,构图简约且富有张力,展现出深沉的复仇叙事氛围。
  7403. ###主体列表
  7404. 陆沉:冷酷神秘的复仇剑客,陆家庄唯一幸存者,身负绝世武功与血海深仇。{全景,正面拍摄,青年男性,亚洲人,黑色长发束起,五官轮廓深邃。服饰穿搭:黑色连帽斗篷,深灰色交领劲装,黑色束腰,背负一柄漆黑宽大的无锋重剑。姿态:站立。}{{青年男性(约25-30岁),音色冷峻、低沉,语速平稳缓慢,透着一股拒人于千里之外的孤傲与肃杀。}}
  7405. 刀疤脸:武林盟的嚣张爪牙,性格暴戾、欺软怕硬,是赵无极麾下的打手。{全景,正面拍摄,中年男性,亚洲人,短发,左脸有一道明显的深长刀疤。服饰穿搭:土黄色粗布武士服,腰间挂着一枚古铜色的“武林盟”令牌。姿态:站立。}{{中年男性(约35-45岁),音色粗犷、沙哑,语气嚣张跋扈,带有明显的威胁感和市井痞气。}}
  7406. 老李:悦来酒馆的老板,胆小卑微的底层百姓,在乱世中艰难求生。{全景,正面拍摄,老年男性,亚洲人,灰白乱发,面容消瘦且布满皱纹。服饰穿搭:破旧的深蓝色粗布长袍,腰间系着一条带有污渍的灰色围裙。姿态:站立。}{{老年男性(约60岁以上),音色苍老、沙哑且带有颤抖,语气唯唯诺诺,充满了卑微与惊恐。}}
  7407. ###场景列表
  7408. 边境小镇街道:夜幕笼罩下的边陲小镇,风沙肆虐,街道两旁是破旧的木质建筑,透着一股荒凉与肃杀的气息,是故事发生的地理背景。{远景,夜幕下的边境小镇,破败的木质建筑错落有致,风沙在空旷的街道上飞扬,远处有微弱的灯火。无人物。}
  7409. 悦来酒馆:酒馆内部陈设简陋,木质桌椅散乱分布,柜台上摆放着酒坛。室内光线昏暗,只有几盏摇曳的油灯提供微弱的暖光,与室外的冷调形成对比。{室内场景,昏暗的古风酒馆内部,粗糙的木质桌椅,木质柜台与酒坛,空气中浮动着尘埃,强烈的侧逆光勾勒出环境轮廓,深邃的硬阴影。无人物。}
  7410. ###道具列表
  7411. 全家福照片:一张全家福照片{AI真人短剧,横屏16:9,8K电影级写实,无人物实体出镜的道具参考图,家庭全家福相框,多角度视角图,正面、背面、侧面、局部细节,木质或金属相框,照片内容可模糊表现多人合影构图,重点展示边缘站位感,相框尺寸标注25厘米×20厘米×2厘米,白底产品级干净展示}
  7412. 酒杯-高脚杯:一个高脚杯{AI真人短剧,横屏16:9,8K电影级写实,高脚酒杯,道具多角度视角图,正面、背面、侧面、局部细节,透明玻璃材质,细长杯柄,尺寸标注高24厘米杯口6厘米,白底干净展示,无人物}
  7413. ###分段剧本
  7414. 片段数量:8
  7415. 旁白音色:成年男性(约35-50岁),音色厚重且富有磁性,带有老电影叙事般的沧桑感,语调低沉平稳,富有江湖气息。
  7416. ##片段1
  7417. 时长:12s
  7418. 分镜1
  7419. 出场角色:刀疤脸
  7420. 场景:边境小镇街道
  7421. 出场道具:酒杯-高脚杯
  7422. 画面:[大远景,冷色调,高对比度] 漫天黄沙在狂风中如怒龙翻滚,遮蔽了微弱的星光。悦来酒馆残破的木门在风中剧烈摇晃,铁链撞击木板发出沉重的闷响,四周建筑尽显荒凉。
  7423. 运镜:大远景固定镜头,随后风沙席卷向镜头形成遮挡转场。
  7424. 配音台词:
  7425. 背景音效:
  7426. 分镜2
  7427. 出场角色:刀疤脸
  7428. 场景:悦来酒馆
  7429. 出场道具:酒杯-高脚杯
  7430. 画面:[全景,暖色调阴影构图] 酒馆内部昏暗,几盏油灯在风中疯狂摇曳,投射出扭曲的长影。角落里的食客低头缩颈,双手紧紧攥着粗瓷碗,空气中漂浮着细小的尘埃。
  7431. 运镜:缓慢横移,掠过阴影中瑟缩的食客。
  7432. 配音台词:
  7433. 背景音效:
  7434. 分镜3
  7435. 出场角色:刀疤脸
  7436. 场景:悦来酒馆
  7437. 出场道具:酒杯-高脚杯
  7438. 画面:[中景,三分法构图] 三名大汉围坐在正中央的圆桌旁,腰间挂着的“武林盟”金色令牌在灯火下闪烁。桌上残羹冷炙横陈,一名大汉正张狂地大笑,脚踩在长凳上。
  7439. 运镜:从中景推向令牌特写。
  7440. 配音台词:
  7441. 背景音效:
  7442. 分镜4
  7443. 出场角色:刀疤脸
  7444. 场景:悦来酒馆
  7445. 出场道具:酒杯-高脚杯
  7446. 画面:[特写,低角度仰拍] 刀疤脸横肉颤动,嘴角挂着轻蔑的笑。他随手倾覆手中的酒碗,琥珀色的液体在空中划过弧线,重重溅在老李打着补丁的布鞋和裤脚上。
  7447. 运镜:极速推向酒水溅起的瞬间。
  7448. 配音台词:
  7449. 背景音效:
  7450. 分镜5
  7451. 出场角色:刀疤脸
  7452. 场景:悦来酒馆
  7453. 出场道具:酒杯-高脚杯
  7454. 画面:[近景,荷兰角倾斜构图] 刀疤脸斜睨着眼,昏暗的灯光勾勒出他脸上狰狞的伤疤。他身体前倾,阴影完全笼罩了对面的老李,眼神如毒蛇般阴冷。
  7455. 运镜:固定机位,利用倾斜构图制造压迫感。
  7456. 配音台词:中文配音:[刀疤脸]老李头,这酒里掺水了吧?
  7457. 背景音效:
  7458. \n\n";
  7459. $origin_prompt = trim((string)getProp($data, 'prompt', '确认分镜大纲'));
  7460. $is_regenerate_version = !in_array($origin_prompt, ['确认分镜大纲', '继续策划下一集'], true);
  7461. $is_global_generate_pics = $origin_prompt == '确认分镜大纲' ? 1 : 0;
  7462. $prompt = $origin_prompt;
  7463. $is_single = (int)getProp($anime, 'is_multi') !== 1;
  7464. // 获取章节内容
  7465. $full_content = getProp($anime, 'content');
  7466. if ($uploaded_content) {
  7467. $full_content = $uploaded_content;
  7468. }
  7469. // 根据章节内容拆分章节
  7470. $chapters = $this->splitContent($full_content);
  7471. $chapter_content = isset($chapters[($episode_number-1)]['content']) ? $chapters[($episode_number-1)]['content'] : '';
  7472. if (!$chapter_content) $chapter_content = $full_content; // 如果无法拆分章节则直接用原文
  7473. $messages = [];
  7474. if ($prompt == '确认分镜大纲') {
  7475. // 暂时保留
  7476. $content = $chapter_content;
  7477. if ($is_single) $content = $full_content; // 单剧集使用全文
  7478. if (!$content) {
  7479. Utils::throwError('20003:章节内容无法获取,请确认');
  7480. }
  7481. $question = $is_single
  7482. ? $demo . "请根据以上要求分析完整剧情,直接输出第1集完整单剧集分镜剧本,需一次性生成并可直接保存,剧情内容如下: \n\n$content"
  7483. : $demo . "请根据以上要求分析本章剧情,完成分镜剧本,本集剧情内容如下: \n\n$content";
  7484. // 构建消息
  7485. $messages[] =
  7486. [
  7487. 'role' => 'user',
  7488. 'content' => $question
  7489. ];
  7490. }else if ($prompt == '继续策划下一集') {
  7491. if ($is_single) {
  7492. Utils::throwError('20003:单剧集不支持继续策划下一集');
  7493. }
  7494. $content = $chapter_content;
  7495. if (!$content) {
  7496. Utils::throwError('20003:章节内容无法获取,请确认');
  7497. }
  7498. $question = $demo."请根据以上要求分析本集剧情内容,完成本集分镜剧本(需衔接上一集内容)";
  7499. // 获取上一集最后记录
  7500. $prev_sequence = $episode_number - 1;
  7501. $messages = $this->getEpisodeChatContent($anime_id, $prev_sequence, $content);
  7502. // 构建消息
  7503. $messages[] =
  7504. [
  7505. 'role' => 'user',
  7506. 'content' => $question
  7507. ];
  7508. // dd($messages);
  7509. }else {
  7510. $content = $uploaded_content ? $chapter_content : trim((string)getProp($base_episode, 'content'));
  7511. if (!$content) {
  7512. $content = $is_single ? $full_content : ($chapter_content ?: $full_content);
  7513. }
  7514. if (!$content) {
  7515. Utils::throwError('20003:章节内容无法获取,请确认');
  7516. }
  7517. $messages = $this->getEpisodeChatMessages($anime_id, $episode_number);
  7518. $targetEpisodeText = $is_single ? '第1集完整单剧集版本' : "第{$episode_number}集完整的新版本剧集";
  7519. $question = $demo . "\n请基于以上历史对话记录,重新生成{$targetEpisodeText},并严格按示例格式输出完整内容。\n";
  7520. if ($origin_prompt) {
  7521. $question .= "本次修改要求如下:\n{$origin_prompt}";
  7522. }
  7523. $messages[] =
  7524. [
  7525. 'role' => 'user',
  7526. 'content' => $question
  7527. ];
  7528. }
  7529. // 处理文本模型
  7530. $model = getProp($data, 'model');
  7531. if (!$model) {
  7532. // 用户没有输入,从anime表获取
  7533. $anime = DB::table('mp_animes')->where('id', $anime_id)->first();
  7534. $model = getProp($anime, 'model');
  7535. if (!$model) {
  7536. // anime表也没有,使用默认值
  7537. $model = 'doubao-seed-2-0-mini-260215';
  7538. }
  7539. }
  7540. // 验证模型是否在可用模型表中
  7541. if (!DB::table('mp_text_models')->where('model', $model)->where('is_enabled', 1)->exists()) {
  7542. $model = 'doubao-seed-2-0-mini-260215';
  7543. }
  7544. // 模型兼容性处理:deepseek-chat 和 deepseek-reasoner 映射到 deepseek-v4-flash
  7545. $thinkingMode = getProp($data, 'thinking', 'disabled'); // 默认非思考模式
  7546. $reasoningEffort = getProp($data, 'reasoning_effort', 'high'); // 默认思考强度
  7547. if ($model === 'deepseek-chat') {
  7548. // deepseek-chat 映射到 deepseek-v4-flash,非思考模式
  7549. $model = 'deepseek-v4-flash';
  7550. $thinkingMode = 'disabled';
  7551. } elseif ($model === 'deepseek-reasoner') {
  7552. // deepseek-reasoner 映射到 deepseek-v4-flash,思考模式
  7553. $model = 'deepseek-v4-flash';
  7554. $thinkingMode = 'enabled';
  7555. }
  7556. $post_data = [
  7557. 'model' => $model,
  7558. 'messages' => $messages,
  7559. // 'max_tokens' => 8192,
  7560. 'temperature' => 0.7,
  7561. 'frequency_penalty' => 0,
  7562. 'presence_penalty' => 0,
  7563. 'response_format' => ['type' => 'text'],
  7564. 'thinking' => ['type' => $thinkingMode],
  7565. 'stream' => true // 启用流式输出
  7566. ];
  7567. if ($thinkingMode == 'enabled') $post_data['reasoning_effort'] = $reasoningEffort;
  7568. // 根据模型类型选择调用方法
  7569. $fullContent = '';
  7570. $fullReasoningContent = '';
  7571. $usage = [];
  7572. try {
  7573. if (in_array($model, ['deepseek-reasoner', 'deepseek-chat', 'deepseek-v4-flash', 'deepseek-v4-pro'])) {
  7574. // DeepSeek 官方模型使用 DeepSeek API
  7575. $streamGenerator = $this->deepSeekStreamResponse($post_data);
  7576. } else if (in_array($model, $this->gpt_text_models)) {
  7577. // GPT-5.4 模型使用 TokenRouter API
  7578. $streamGenerator = $this->gpt54StreamResponse($post_data);
  7579. } else {
  7580. // 其他模型使用火山引擎API
  7581. $streamGenerator = $this->volcEngineChatCompletion($post_data);
  7582. }
  7583. // 验证返回值类型
  7584. if (!is_iterable($streamGenerator)) {
  7585. $errorType = is_object($streamGenerator) ? get_class($streamGenerator) : gettype($streamGenerator);
  7586. dLog('deepseek')->error('chat流式生成器无效', [
  7587. 'generator_type' => $errorType,
  7588. 'model' => $model,
  7589. 'anime_id' => $anime_id,
  7590. 'episode_number' => $episode_number
  7591. ]);
  7592. logDB('deepseek', 'error', 'chat流式生成器类型错误', [
  7593. 'type' => $errorType,
  7594. 'model' => $model
  7595. ]);
  7596. yield [
  7597. 'type' => 'error',
  7598. 'episode' => [],
  7599. 'answer' => '',
  7600. 'reasoning' => '',
  7601. 'usage' => [],
  7602. 'error' => '接口返回数据异常,请重新请求'
  7603. ];
  7604. return;
  7605. }
  7606. // 处理流式输出
  7607. foreach ($streamGenerator as $chunk) {
  7608. if (isset($chunk['type'])) {
  7609. if ($chunk['type'] === 'done') {
  7610. // 最终结果
  7611. $fullContent = $chunk['full_content'];
  7612. $fullReasoningContent = $chunk['full_reasoning'];
  7613. $usage = isset($chunk['usage']) ? $chunk['usage'] : [];
  7614. } else {
  7615. // 逐块yield数据
  7616. yield $chunk;
  7617. }
  7618. }
  7619. }
  7620. } catch (\Exception $e) {
  7621. dLog('deepseek')->error('chat流式处理异常: ' . $e->getMessage(), [
  7622. 'model' => $model,
  7623. 'anime_id' => $anime_id,
  7624. 'episode_number' => $episode_number,
  7625. 'trace' => $e->getTraceAsString()
  7626. ]);
  7627. logDB('deepseek', 'error', 'chat流式处理失败', [
  7628. 'error' => $e->getMessage(),
  7629. 'model' => $model
  7630. ]);
  7631. yield [
  7632. 'type' => 'error',
  7633. 'episode' => [],
  7634. 'answer' => '',
  7635. 'reasoning' => '',
  7636. 'usage' => [],
  7637. 'error' => '处理请求时发生异常: ' . $e->getMessage()
  7638. ];
  7639. return;
  7640. }
  7641. dLog('deepseek')->info('完整内容: '.$fullContent);
  7642. logDB('deepseek', 'info', '完整内容', ['content' => $fullContent, 'anime_id' => $anime_id]);
  7643. // 处理完整内容并返回最终结果
  7644. $episode_arr = handleEpisodeContentForAce($fullContent);
  7645. logDB('deepseek', 'info', '解析内容', $episode_arr);
  7646. // // 如果有anime_script_id,进行主体和场景名称的智能匹配和替换(激进策略)
  7647. // if ($anime_script_id && !empty($extra_roles) && !empty($extra_scenes)) {
  7648. // $episode_arr = $this->matchAndReplaceRolesAndScenes($episode_arr, $extra_roles, $extra_scenes);
  7649. // logDB('deepseek', 'info', '匹配替换后的解析内容', $episode_arr);
  7650. // }
  7651. // 验证逻辑:如果anime_script_id有值,需要验证生成的主体和场景是否匹配
  7652. $max_retries = 3;
  7653. $retry_count = 0;
  7654. $validation_failed = false;
  7655. $validation_error_msg = '';
  7656. // 验证生成的内容
  7657. if ($anime_script_id) {
  7658. // 检查是否有acts数据
  7659. if (empty($episode_arr['acts'])) {
  7660. $validation_failed = true;
  7661. $validation_error_msg = '未生成分镜剧本相关的内容';
  7662. } else {
  7663. // 检查主体列表是否匹配 - 只验证生成的主体名是否都在预期列表中
  7664. $generated_roles = array_column($episode_arr['roles'], 'role');
  7665. // 过滤掉"旁白"角色
  7666. $generated_roles = array_filter($generated_roles, function($role) {
  7667. return $role !== '旁白';
  7668. });
  7669. $expected_roles = array_keys($extra_roles);
  7670. // 找出生成的主体中不在预期列表中的主体
  7671. $invalid_roles = array_diff($generated_roles, $expected_roles);
  7672. if (!empty($invalid_roles)) {
  7673. $validation_failed = true;
  7674. $validation_error_msg = '生成的主体列表包含不在【强制主体设定】中的主体:' . implode('、', $invalid_roles);
  7675. }
  7676. // 检查场景列表是否匹配 - 只验证生成的场景名是否都在预期列表中
  7677. if (!$validation_failed) {
  7678. $generated_scenes = array_column($episode_arr['scenes'], 'scene');
  7679. $expected_scenes = array_keys($extra_scenes);
  7680. // 找出生成的场景中不在预期列表中的场景
  7681. $invalid_scenes = array_diff($generated_scenes, $expected_scenes);
  7682. if (!empty($invalid_scenes)) {
  7683. $validation_failed = true;
  7684. $validation_error_msg = '生成的场景列表包含不在【强制场景设定】中的场景:' . implode('、', $invalid_scenes);
  7685. }
  7686. }
  7687. }
  7688. } else {
  7689. // 如果没有anime_script_id,只检查是否有acts数据
  7690. if (empty($episode_arr['acts'])) {
  7691. $validation_failed = true;
  7692. $validation_error_msg = '未生成分镜剧本相关的内容';
  7693. }
  7694. }
  7695. // 重试逻辑
  7696. while ($validation_failed && $retry_count < $max_retries) {
  7697. $retry_count++;
  7698. dLog('deepseek')->warning("验证失败,开始第{$retry_count}次重试", [
  7699. 'reason' => $validation_error_msg,
  7700. 'anime_id' => $anime_id,
  7701. 'episode_number' => $episode_number
  7702. ]);
  7703. logDB('deepseek', 'warning', "第{$retry_count}次重试", [
  7704. 'reason' => $validation_error_msg,
  7705. 'anime_id' => $anime_id
  7706. ]);
  7707. // 发送流式重试提示
  7708. yield [
  7709. 'type' => 'retry',
  7710. 'retry_count' => $retry_count,
  7711. 'reason' => $validation_error_msg
  7712. ];
  7713. // 构建重试提示词
  7714. $retry_prompt = "生成的剧本存在问题:{$validation_error_msg}。\n\n请严格按照要求重新生成,确保:\n";
  7715. if ($anime_script_id) {
  7716. $retry_prompt .= "1. 主体列表中的所有主体名称必须从【强制主体设定】中选择,不得使用其他主体名称\n";
  7717. $retry_prompt .= "2. 场景列表中的所有场景名称必须从【强制场景设定】中选择,不得使用其他场景名称\n";
  7718. $retry_prompt .= "3. 必须包含完整的分镜剧本内容\n";
  7719. } else {
  7720. $retry_prompt .= "1. 必须包含完整的分镜剧本内容\n";
  7721. }
  7722. // 添加重试消息到messages
  7723. $messages[] = [
  7724. 'role' => 'assistant',
  7725. 'content' => $fullContent
  7726. ];
  7727. $messages[] = [
  7728. 'role' => 'user',
  7729. 'content' => $retry_prompt
  7730. ];
  7731. // 更新post_data的messages
  7732. $post_data['messages'] = $messages;
  7733. // 重新调用API
  7734. try {
  7735. if (in_array($model, ['deepseek-reasoner', 'deepseek-chat', 'deepseek-v4-flash', 'deepseek-v4-pro'])) {
  7736. $streamGenerator = $this->deepSeekStreamResponse($post_data);
  7737. } else if (in_array($model, $this->gpt_text_models)) {
  7738. $streamGenerator = $this->gpt54StreamResponse($post_data);
  7739. } else {
  7740. $streamGenerator = $this->volcEngineChatCompletion($post_data);
  7741. }
  7742. // 处理流式输出
  7743. $fullContent = '';
  7744. $fullReasoningContent = '';
  7745. foreach ($streamGenerator as $chunk) {
  7746. if (isset($chunk['type'])) {
  7747. if ($chunk['type'] === 'done') {
  7748. $fullContent = $chunk['full_content'];
  7749. $fullReasoningContent = $chunk['full_reasoning'];
  7750. $usage = isset($chunk['usage']) ? $chunk['usage'] : [];
  7751. } else {
  7752. yield $chunk;
  7753. }
  7754. }
  7755. }
  7756. // 重新解析内容
  7757. dLog('deepseek')->info("第{$retry_count}次重试完整内容: " . $fullContent);
  7758. logDB('deepseek', 'info', "第{$retry_count}次重试解析内容", ['content' => $fullContent, 'anime_id' => $anime_id]);
  7759. $episode_arr = handleEpisodeContentForAce($fullContent);
  7760. // // 进行主体和场景名称的智能匹配和替换(激进策略)
  7761. // if ($anime_script_id && !empty($extra_roles) && !empty($extra_scenes)) {
  7762. // $episode_arr = $this->matchAndReplaceRolesAndScenes($episode_arr, $extra_roles, $extra_scenes);
  7763. // }
  7764. dLog('deepseek')->info('第'.$retry_count.'次重试后内容: '.$fullContent);
  7765. logDB('deepseek', 'info', "第{$retry_count}次重试解析内容", $episode_arr);
  7766. // 重新验证
  7767. $validation_failed = false;
  7768. $validation_error_msg = '';
  7769. if ($anime_script_id) {
  7770. if (empty($episode_arr['acts'])) {
  7771. $validation_failed = true;
  7772. $validation_error_msg = '未生成分镜剧本相关的内容';
  7773. } else {
  7774. // 检查主体列表 - 只验证生成的主体名是否都在预期列表中
  7775. $generated_roles = array_column($episode_arr['roles'], 'role');
  7776. // 过滤掉"旁白"角色
  7777. $generated_roles = array_filter($generated_roles, function($role) {
  7778. return $role !== '旁白';
  7779. });
  7780. $expected_roles = array_keys($extra_roles);
  7781. $invalid_roles = array_diff($generated_roles, $expected_roles);
  7782. if (!empty($invalid_roles)) {
  7783. $validation_failed = true;
  7784. $validation_error_msg = '生成的主体列表包含不在【强制主体设定】中的主体:' . implode('、', $invalid_roles);
  7785. }
  7786. // 检查场景列表 - 只验证生成的场景名是否都在预期列表中
  7787. if (!$validation_failed) {
  7788. $generated_scenes = array_column($episode_arr['scenes'], 'scene');
  7789. $expected_scenes = array_keys($extra_scenes);
  7790. $invalid_scenes = array_diff($generated_scenes, $expected_scenes);
  7791. if (!empty($invalid_scenes)) {
  7792. $validation_failed = true;
  7793. $validation_error_msg = '生成的场景列表包含不在【强制场景设定】中的场景:' . implode('、', $invalid_scenes);
  7794. }
  7795. }
  7796. }
  7797. } else {
  7798. if (empty($episode_arr['acts'])) {
  7799. $validation_failed = true;
  7800. $validation_error_msg = '未生成分镜剧本相关的内容';
  7801. }
  7802. }
  7803. } catch (\Exception $e) {
  7804. dLog('deepseek')->error("第{$retry_count}次重试异常: " . $e->getMessage(), [
  7805. 'trace' => $e->getTraceAsString()
  7806. ]);
  7807. break;
  7808. }
  7809. }
  7810. // 如果重试后仍然失败
  7811. if ($validation_failed) {
  7812. dLog('deepseek')->error('达到最大重试次数,仍未生成有效内容', [
  7813. 'retry_count' => $retry_count,
  7814. 'last_error' => $validation_error_msg
  7815. ]);
  7816. try {
  7817. $now = date('Y-m-d H:i:s');
  7818. $records = [
  7819. [
  7820. 'uid' => $uid,
  7821. 'anime_id' => $anime_id,
  7822. 'sequence' => $episode_number,
  7823. 'role' => 'user',
  7824. 'content' => $prompt,
  7825. 'created_at' => $now,
  7826. 'updated_at' => $now
  7827. ],
  7828. [
  7829. 'uid' => $uid,
  7830. 'anime_id' => $anime_id,
  7831. 'sequence' => $episode_number,
  7832. 'role' => 'assistant',
  7833. 'content' => $fullContent,
  7834. 'created_at' => $now,
  7835. 'updated_at' => $now
  7836. ]
  7837. ];
  7838. DB::table('mp_anime_records')->insert($records);
  7839. } catch (\Exception $e) {
  7840. dLog('deepseek')->error('保存对话记录失败', ['error' => $e->getMessage()]);
  7841. }
  7842. yield [
  7843. 'type' => 'done',
  7844. 'episode' => [],
  7845. 'answer' => $fullContent,
  7846. 'reasoning' => $fullReasoningContent,
  7847. 'usage' => $usage,
  7848. 'error' => "生成剧本有误,已重试{$retry_count}次仍失败,请稍后重试。错误原因:{$validation_error_msg}"
  7849. ];
  7850. return;
  7851. }
  7852. // 验证成功,记录日志
  7853. if ($retry_count > 0) {
  7854. dLog('deepseek')->info("经过{$retry_count}次重试后验证成功");
  7855. logDB('deepseek', 'info', '重试成功', ['retry_count' => $retry_count, 'anime_id' => $anime_id]);
  7856. }
  7857. // 老逻辑
  7858. $existing_roles = $base_episode ? json_decode((string)getProp($base_episode, 'roles'), true) : [];
  7859. $existing_scenes = $base_episode ? json_decode((string)getProp($base_episode, 'scenes'), true) : [];
  7860. $existing_props = $base_episode ? json_decode((string)getProp($base_episode, 'props'), true) : [];
  7861. $existing_roles = is_array($existing_roles) ? $existing_roles : [];
  7862. $existing_scenes = is_array($existing_scenes) ? $existing_scenes : [];
  7863. $existing_props = is_array($existing_props) ? $existing_props : [];
  7864. $now = date('Y-m-d H:i:s');
  7865. try {
  7866. DB::beginTransaction();
  7867. $saveResult = $this->saveEpisodeVersionData(
  7868. $anime_id,
  7869. $episode_number,
  7870. $episode_arr,
  7871. $existing_roles,
  7872. $existing_scenes,
  7873. $existing_props,
  7874. $current_episode,
  7875. $base_episode,
  7876. $is_regenerate_version,
  7877. $content,
  7878. $now,
  7879. $ref_products
  7880. );
  7881. $episode = $saveResult['episode'];
  7882. $episode_id = $saveResult['episode_id'];
  7883. $merged_roles = $saveResult['merged_roles'];
  7884. $merged_scenes = $saveResult['merged_scenes'];
  7885. $merged_props = $saveResult['merged_props'];
  7886. $record_content = $origin_prompt;
  7887. if ($uploaded_content !== '') {
  7888. // $record_content .= "\n\n###上传内容\n" . $uploaded_content;
  7889. }
  7890. $records = [
  7891. [
  7892. 'uid' => $uid,
  7893. 'anime_id' => $anime_id,
  7894. 'role' => 'user',
  7895. 'content' => $record_content,
  7896. 'sequence' => $episode_number,
  7897. 'episode_id' => $episode_id,
  7898. 'created_at' => $now,
  7899. 'updated_at' => $now
  7900. ],
  7901. [
  7902. 'uid' => $uid,
  7903. 'anime_id' => $anime_id,
  7904. 'role' => 'assistant',
  7905. 'content' => $fullContent,
  7906. // 'content' => '我已按照您的要求完成策划并将内容更新到您右侧的策划文档。',
  7907. 'sequence' => $episode_number,
  7908. 'episode_id' => $episode_id,
  7909. 'created_at' => $now,
  7910. 'updated_at' => $now
  7911. ]
  7912. ];
  7913. $boolen4 = DB::table('mp_anime_records')->insert($records);
  7914. if (!$boolen4) {
  7915. Utils::throwError('20003:对话记录保存失败');
  7916. }
  7917. // 保存模型到anime表
  7918. $update_anime_data = [
  7919. 'model' => $model,
  7920. 'updated_at' => $now
  7921. ];
  7922. if ($uploaded_content) {
  7923. $update_anime_data['content'] = $uploaded_content;
  7924. }
  7925. DB::table('mp_animes')->where('id', $anime_id)->update($update_anime_data);
  7926. }catch (\Exception $e) {
  7927. DB::rollBack();
  7928. dLog('deepseek')->info('新建对话错误信息: '.$e->getMessage());
  7929. yield [
  7930. 'type' => 'done',
  7931. 'answer' => $fullContent,
  7932. 'reasoning' => $fullReasoningContent,
  7933. 'usage' => $usage,
  7934. 'error' => $e->getMessage(),
  7935. ];
  7936. return;
  7937. }
  7938. DB::commit();
  7939. if ($is_global_generate_pics) {
  7940. // // 批量生成全局角色图片
  7941. // $this->batchSetGlobalRoleImg([
  7942. // 'anime_id' => $anime_id,
  7943. // ], true);
  7944. // // 批量生成全局场景图片
  7945. // $this->batchSetGlobalSceneImg([
  7946. // 'anime_id' => $anime_id,
  7947. // ], true);
  7948. // // 将全局角色和场景的task_id、task_status和url继承给当前分集
  7949. // $this->inheritGlobalImagesToEpisodes($anime_id, $episode_id);
  7950. }
  7951. $episode['episode_id'] = $episode_id;
  7952. $episode['roles'] = $merged_roles;
  7953. $episode['scenes'] = $merged_scenes;
  7954. // $episode['acts'] = isset($episode_arr['acts']) && is_array($episode_arr['acts']) ? $episode_arr['acts'] : [];
  7955. yield [
  7956. 'type' => 'done',
  7957. 'episode' => $episode,
  7958. 'answer' => $fullContent,
  7959. 'reasoning' => $fullReasoningContent,
  7960. 'usage' => $usage
  7961. ];
  7962. }
  7963. /**
  7964. * chatForAce的非流式版本 - 用于队列任务
  7965. * 与chatForAce逻辑完全一致,但不使用流式输出
  7966. *
  7967. * @param array $data 请求数据
  7968. * @return array 生成结果
  7969. */
  7970. public function chatForAceNonStream($data) {
  7971. $allChunks = [];
  7972. $finalResult = null;
  7973. // 调用流式方法并收集所有数据
  7974. foreach ($this->chatForAce($data) as $chunk) {
  7975. $allChunks[] = $chunk;
  7976. // 保存最终结果
  7977. if (isset($chunk['type']) && $chunk['type'] === 'done') {
  7978. $finalResult = $chunk;
  7979. }
  7980. }
  7981. // 返回最终结果
  7982. if ($finalResult) {
  7983. return $finalResult;
  7984. }
  7985. // 如果没有done类型的数据,返回最后一个chunk
  7986. return end($allChunks) ?: ['error' => '生成失败'];
  7987. }
  7988. /**
  7989. * 批量生成多个分集
  7990. *
  7991. * @param array $data 请求数据
  7992. * @return array 任务信息
  7993. */
  7994. public function batchGenerateEpisodes($data) {
  7995. $uid = Site::getUid();
  7996. $cpid = Site::getCpid();
  7997. $anime_id = getProp($data, 'anime_id');
  7998. $generate_episode_number = (int)getProp($data, 'generate_episode_number', 1);
  7999. if (!$anime_id) {
  8000. Utils::throwError('20003:anime_id参数缺失');
  8001. }
  8002. if ($generate_episode_number < 1) {
  8003. Utils::throwError('20003:生成集数必须大于0');
  8004. }
  8005. // 获取动漫信息
  8006. $anime = DB::table('mp_animes')->where('id', $anime_id)->first();
  8007. if (!$anime) {
  8008. Utils::throwError('20003:该剧集不存在');
  8009. }
  8010. // 检查是否是多剧集模式
  8011. if ((int)getProp($anime, 'is_multi') !== 1) {
  8012. Utils::throwError('20003:单剧集不支持批量生成');
  8013. }
  8014. // 获取当前已生成的最大集数
  8015. $currentMaxEpisode = DB::table('mp_anime_episodes')
  8016. ->where('anime_id', $anime_id)
  8017. ->where('is_default', 1)
  8018. ->max('episode_number');
  8019. $currentMaxEpisode = $currentMaxEpisode ?: 0;
  8020. // 确定起始集数
  8021. $startEpisodeNumber = $currentMaxEpisode + 1;
  8022. // 如果用户指定了起始集数(通过episode_number参数)
  8023. if (isset($data['episode_number'])) {
  8024. $userStartEpisode = (int)getProp($data, 'episode_number');
  8025. if ($userStartEpisode > $currentMaxEpisode) {
  8026. $startEpisodeNumber = $userStartEpisode;
  8027. }
  8028. }
  8029. // 计算结束集数
  8030. $endEpisodeNumber = $startEpisodeNumber + $generate_episode_number - 1;
  8031. // 检查是否超过总集数限制
  8032. $end_episode_sequence = getProp($anime, 'end_episode_sequence');
  8033. if ($end_episode_sequence && $endEpisodeNumber > $end_episode_sequence) {
  8034. Utils::throwError("20003:总集数只有{$end_episode_sequence}集,无法生成到第{$endEpisodeNumber}集");
  8035. }
  8036. // 检查是否已有进行中的任务
  8037. $existingTask = DB::table('mp_batch_episode_generation_tasks')
  8038. ->where('anime_id', $anime_id)
  8039. ->whereIn('status', ['pending', 'processing'])
  8040. ->first();
  8041. if ($existingTask) {
  8042. Utils::throwError('20003:该剧集已有批量生成任务正在进行中,请等待完成后再试');
  8043. }
  8044. $now = date('Y-m-d H:i:s');
  8045. // 准备任务数据
  8046. $taskData = [
  8047. 'anime_id' => $anime_id,
  8048. 'uid' => $uid,
  8049. 'cpid' => $cpid,
  8050. 'start_episode' => $startEpisodeNumber,
  8051. 'end_episode' => $endEpisodeNumber,
  8052. 'total_episodes' => $generate_episode_number,
  8053. 'current_episode' => 0,
  8054. 'completed_episodes' => 0,
  8055. 'status' => 'pending',
  8056. 'request_data' => json_encode($data, JSON_UNESCAPED_UNICODE),
  8057. 'created_at' => $now,
  8058. 'updated_at' => $now
  8059. ];
  8060. // 创建批量生成任务记录
  8061. $taskId = DB::table('mp_batch_episode_generation_tasks')->insertGetId($taskData);
  8062. if (!$taskId) {
  8063. Utils::throwError('20003:创建批量生成任务失败');
  8064. }
  8065. // 更新任务状态为processing
  8066. DB::table('mp_batch_episode_generation_tasks')
  8067. ->where('id', $taskId)
  8068. ->update([
  8069. 'status' => 'processing',
  8070. 'updated_at' => $now
  8071. ]);
  8072. // 分发到队列
  8073. \App\Jobs\ProcessBatchEpisodeGenerationJob::dispatch(
  8074. $taskId,
  8075. $anime_id,
  8076. $startEpisodeNumber,
  8077. $generate_episode_number,
  8078. $data,
  8079. $uid,
  8080. $cpid
  8081. );
  8082. return [
  8083. 'task_id' => $taskId,
  8084. 'anime_id' => $anime_id,
  8085. 'start_episode' => $startEpisodeNumber,
  8086. 'end_episode' => $endEpisodeNumber,
  8087. 'total_episodes' => $generate_episode_number,
  8088. 'status' => 'processing',
  8089. 'message' => "已创建批量生成任务,将生成第{$startEpisodeNumber}集到第{$endEpisodeNumber}集,共{$generate_episode_number}集"
  8090. ];
  8091. }
  8092. /**
  8093. * 获取批量生成任务状态
  8094. *
  8095. * @param array $data 请求数据
  8096. * @return array 任务状态信息
  8097. */
  8098. public function getBatchGenerationTaskStatus($data) {
  8099. $task_id = getProp($data, 'task_id');
  8100. $anime_id = getProp($data, 'anime_id');
  8101. if (!$task_id && !$anime_id) {
  8102. Utils::throwError('20003:task_id或anime_id参数缺失');
  8103. }
  8104. $query = DB::table('mp_batch_episode_generation_tasks');
  8105. if ($task_id) {
  8106. $query->where('id', $task_id);
  8107. } else {
  8108. // 获取该anime最新的任务
  8109. $query->where('anime_id', $anime_id)
  8110. ->orderByDesc('id')
  8111. ->limit(1);
  8112. }
  8113. $task = $query->first();
  8114. if (!$task) {
  8115. Utils::throwError('20003:任务不存在');
  8116. }
  8117. // 获取任务详情
  8118. $details = DB::table('mp_batch_episode_generation_details')
  8119. ->where('task_id', $task->id)
  8120. ->orderBy('episode_number')
  8121. ->get()
  8122. ->map(function($item) {
  8123. return [
  8124. 'episode_number' => $item->episode_number,
  8125. 'status' => $item->status,
  8126. 'error_message' => $item->error_message,
  8127. 'created_at' => $item->created_at,
  8128. 'updated_at' => $item->updated_at
  8129. ];
  8130. })
  8131. ->toArray();
  8132. // 计算进度百分比
  8133. $progress = 0;
  8134. if ($task->total_episodes > 0) {
  8135. $progress = round(($task->completed_episodes / $task->total_episodes) * 100, 2);
  8136. }
  8137. return [
  8138. 'task_id' => $task->id,
  8139. 'anime_id' => $task->anime_id,
  8140. 'start_episode' => $task->start_episode,
  8141. 'end_episode' => $task->end_episode,
  8142. 'total_episodes' => $task->total_episodes,
  8143. 'current_episode' => $task->current_episode,
  8144. 'completed_episodes' => $task->completed_episodes,
  8145. 'progress' => $progress,
  8146. 'status' => $task->status,
  8147. 'error_message' => $task->error_message,
  8148. 'details' => $details,
  8149. 'created_at' => $task->created_at,
  8150. 'updated_at' => $task->updated_at,
  8151. 'completed_at' => $task->completed_at
  8152. ];
  8153. }
  8154. /**
  8155. * 智能匹配并替换主体和场景名称
  8156. * 如果生成的主体或场景名称是强制设定名称的子串,则自动替换为完整名称
  8157. *
  8158. * @param array $episode_arr 解析后的剧集数据
  8159. * @param array $extra_roles 强制主体设定(key为主体名称)
  8160. * @param array $extra_scenes 强制场景设定(key为场景名称)
  8161. * @return array 替换后的剧集数据
  8162. */
  8163. private function matchAndReplaceRolesAndScenes($episode_arr, $extra_roles, $extra_scenes) {
  8164. $replaced_count = 0;
  8165. // 1. 处理主体列表
  8166. if (!empty($episode_arr['roles'])) {
  8167. foreach ($episode_arr['roles'] as &$role) {
  8168. $generated_role_name = $role['role'] ?? '';
  8169. // 跳过旁白
  8170. if ($generated_role_name === '旁白') {
  8171. continue;
  8172. }
  8173. // 如果生成的主体名不在强制设定中,尝试模糊匹配
  8174. if (!isset($extra_roles[$generated_role_name])) {
  8175. $matched_role = $this->findMatchingName($generated_role_name, array_keys($extra_roles));
  8176. if ($matched_role) {
  8177. dLog('deepseek')->info("主体名称替换: {$generated_role_name} => {$matched_role}");
  8178. $role['role'] = $matched_role;
  8179. $replaced_count++;
  8180. }
  8181. }
  8182. }
  8183. }
  8184. // 2. 处理场景列表
  8185. if (!empty($episode_arr['scenes'])) {
  8186. foreach ($episode_arr['scenes'] as &$scene) {
  8187. $generated_scene_name = $scene['scene'] ?? '';
  8188. // 如果生成的场景名不在强制设定中,尝试模糊匹配
  8189. if (!isset($extra_scenes[$generated_scene_name])) {
  8190. $matched_scene = $this->findMatchingName($generated_scene_name, array_keys($extra_scenes));
  8191. if ($matched_scene) {
  8192. dLog('deepseek')->info("场景名称替换: {$generated_scene_name} => {$matched_scene}");
  8193. $scene['scene'] = $matched_scene;
  8194. $replaced_count++;
  8195. }
  8196. }
  8197. }
  8198. }
  8199. // 3. 处理分段剧本中的主体和场景引用(全能模式下只使用segment_content字段)
  8200. if (!empty($episode_arr['acts'])) {
  8201. foreach ($episode_arr['acts'] as &$act) {
  8202. if (!empty($act['segments'])) {
  8203. foreach ($act['segments'] as &$segment) {
  8204. if (!empty($segment['segment_content'])) {
  8205. $original_content = $segment['segment_content'];
  8206. $replaced_content = $original_content;
  8207. // 替换场景名 - 在文本中查找并替换
  8208. foreach (array_keys($extra_scenes) as $full_scene_name) {
  8209. // 尝试找到可能的短名称
  8210. $potential_short_names = [];
  8211. // 提取场景名的主要部分(冒号之前)
  8212. if (mb_strpos($full_scene_name, ':') !== false) {
  8213. $short_name = mb_substr($full_scene_name, 0, mb_strpos($full_scene_name, ':'));
  8214. $potential_short_names[] = $short_name;
  8215. } elseif (mb_strpos($full_scene_name, ':') !== false) {
  8216. $short_name = mb_substr($full_scene_name, 0, mb_strpos($full_scene_name, ':'));
  8217. $potential_short_names[] = $short_name;
  8218. }
  8219. // 替换场景字段中的短名称
  8220. foreach ($potential_short_names as $short_name) {
  8221. if ($short_name !== $full_scene_name) {
  8222. $pattern = '/(?<=场景:|场景:|拍摄场景:|拍摄场景:|背景场景:|背景场景:|环境:|环境:)' . preg_quote($short_name, '/') . '(?=\s|$|\n)/u';
  8223. $new_content = preg_replace($pattern, $full_scene_name, $replaced_content);
  8224. if ($new_content !== $replaced_content) {
  8225. $replaced_content = $new_content;
  8226. $replaced_count++;
  8227. dLog('deepseek')->info("场景名替换: {$short_name} => {$full_scene_name}");
  8228. }
  8229. }
  8230. }
  8231. }
  8232. // 替换配音角色名(跳过旁白)
  8233. foreach (array_keys($extra_roles) as $full_role_name) {
  8234. if ($full_role_name === '旁白') {
  8235. continue;
  8236. }
  8237. // 尝试找到可能的短名称
  8238. $potential_short_names = [];
  8239. // 提取角色名的主要部分(短横线之前)
  8240. if (mb_strpos($full_role_name, '-') !== false) {
  8241. $short_name = mb_substr($full_role_name, 0, mb_strpos($full_role_name, '-'));
  8242. $potential_short_names[] = $short_name;
  8243. }
  8244. // 替换配音台词中的短名称
  8245. foreach ($potential_short_names as $short_name) {
  8246. if ($short_name !== $full_role_name) {
  8247. $pattern = '/(?<=配音台词:\[|配音台词:\[|中文配音:\[|中文配音:\[)\s*' . preg_quote($short_name, '/') . '\s*(?=\])/u';
  8248. $new_content = preg_replace($pattern, $full_role_name, $replaced_content);
  8249. if ($new_content !== $replaced_content) {
  8250. $replaced_content = $new_content;
  8251. $replaced_count++;
  8252. dLog('deepseek')->info("角色名替换: {$short_name} => {$full_role_name}");
  8253. }
  8254. }
  8255. }
  8256. }
  8257. // 如果有替换,更新segment_content
  8258. if ($replaced_content !== $original_content) {
  8259. $segment['segment_content'] = $replaced_content;
  8260. }
  8261. }
  8262. }
  8263. }
  8264. }
  8265. }
  8266. if ($replaced_count > 0) {
  8267. logDB('deepseek', 'info', '主体和场景名称智能替换', [
  8268. 'replaced_count' => $replaced_count
  8269. ]);
  8270. }
  8271. return $episode_arr;
  8272. }
  8273. /**
  8274. * 查找匹配的名称
  8275. * 规则:如果生成的名称是强制设定名称的子串(开头部分),则返回匹配的强制设定名称
  8276. *
  8277. * @param string $generated_name 生成的名称
  8278. * @param array $expected_names 预期的名称列表
  8279. * @return string|null 匹配到的名称,如果没有匹配则返回null
  8280. */
  8281. private function findMatchingName($generated_name, $expected_names) {
  8282. if (empty($generated_name)) {
  8283. return null;
  8284. }
  8285. // 尝试精确匹配
  8286. if (in_array($generated_name, $expected_names)) {
  8287. return $generated_name;
  8288. }
  8289. // 尝试模糊匹配:生成的名称是否为某个预期名称的开头部分
  8290. foreach ($expected_names as $expected_name) {
  8291. // 检查生成的名称是否是预期名称的开头(如"沈秋岚"是"沈秋岚-程家居家围裙造型"的开头)
  8292. if (mb_strpos($expected_name, $generated_name) === 0) {
  8293. return $expected_name;
  8294. }
  8295. }
  8296. // 更宽松的匹配:检查生成的名称是否包含在预期名称中的任意位置
  8297. // 这样可以处理一些变体,但优先级较低
  8298. foreach ($expected_names as $expected_name) {
  8299. if (mb_strpos($expected_name, $generated_name) !== false) {
  8300. return $expected_name;
  8301. }
  8302. }
  8303. return null;
  8304. }
  8305. /**
  8306. * 重新生成分段剧本
  8307. * @param array $data 包含以下字段:
  8308. * - prompt: string|null 用户修改要求(可选)
  8309. * - template_id: int|null 提示词模板ID(可选)
  8310. * - act_id: int|null 单个片段ID(与episode_id二选一)
  8311. * - episode_id: int|null 剧集ID,用于重新生成整个剧集的所有片段(与act_id二选一)
  8312. * @return array 返回生成结果
  8313. */
  8314. public function regenerateSegmentScript($data) {
  8315. $uid = Site::getUid();
  8316. $prompt = trim((string)getProp($data, 'prompt', ''));
  8317. $template_id = getProp($data, 'template_id', 0);
  8318. $act_id = getProp($data, 'act_id', 0);
  8319. $episode_id = getProp($data, 'episode_id', 0);
  8320. // 验证:prompt和template_id至少提供一个
  8321. if (empty($prompt) && empty($template_id)) {
  8322. Utils::throwError('20003:请提供修改要求或选择提示词模板');
  8323. }
  8324. // 验证:act_id和episode_id二选一
  8325. if (empty($act_id) && empty($episode_id)) {
  8326. Utils::throwError('20003:请提供片段ID或剧集ID');
  8327. }
  8328. if (!empty($act_id) && !empty($episode_id)) {
  8329. Utils::throwError('20003:片段ID和剧集ID只能选择一个');
  8330. }
  8331. // 如果提供了template_id,获取模板提示词
  8332. $template_prompt = '';
  8333. if ($template_id > 0) {
  8334. $template = DB::table('mp_prompt_templates')
  8335. ->where('id', $template_id)
  8336. ->where('is_deleted', 0)
  8337. ->first();
  8338. if (!$template) {
  8339. Utils::throwError('20003:提示词模板不存在或已删除');
  8340. }
  8341. $template_prompt = $template->template_prompt ?? '';
  8342. }
  8343. // 合并用户提示词和模板提示词
  8344. $final_prompt = '';
  8345. if (!empty($template_prompt)) {
  8346. $final_prompt = $template_prompt;
  8347. if (!empty($prompt)) {
  8348. $final_prompt .= "\n\n补充要求:" . $prompt;
  8349. }
  8350. } else {
  8351. $final_prompt = $prompt;
  8352. }
  8353. // 获取需要重新生成的内容参考
  8354. $reference_content = '';
  8355. $target_episode_id = 0;
  8356. $target_anime_id = 0;
  8357. $target_episode_number = 0;
  8358. if ($act_id > 0) {
  8359. // 单个片段模式:获取该片段的内容
  8360. $segment = DB::table('mp_episode_segments')
  8361. ->where('id', $act_id)
  8362. ->first();
  8363. if (!$segment) {
  8364. Utils::throwError('20003:片段不存在');
  8365. }
  8366. $target_episode_id = $segment->episode_id;
  8367. $target_anime_id = $segment->anime_id;
  8368. $target_episode_number = $segment->episode_number;
  8369. $reference_content = $segment->act_content ?? '';
  8370. $reference_content = "\n\n【原分段剧本内容】\n{$reference_content}";
  8371. } else {
  8372. // 整个剧集模式:获取所有片段的内容
  8373. $segments = DB::table('mp_episode_segments')
  8374. ->where('episode_id', $episode_id)
  8375. ->orderBy('act_number')
  8376. ->get();
  8377. if ($segments->isEmpty()) {
  8378. Utils::throwError('20003:该剧集没有片段数据');
  8379. }
  8380. $target_episode_id = $episode_id;
  8381. $target_anime_id = $segments[0]->anime_id;
  8382. $target_episode_number = $segments[0]->episode_number;
  8383. // 拼接所有片段内容,保留格式用于AI理解
  8384. $act_contents = [];
  8385. foreach ($segments as $seg) {
  8386. $act_number = $seg->act_number;
  8387. $act_content = $seg->act_content ?? '';
  8388. if (!empty($act_content)) {
  8389. $act_contents[] = "##片段{$act_number}\n" . $act_content;
  8390. }
  8391. }
  8392. $reference_content = implode("\n\n", $act_contents);
  8393. $episode_content = DB::table('mp_anime_episodes')->where('id', $episode_id)->value('content');
  8394. if ($episode_content) {
  8395. $reference_content = "\n\n【本集原始内容】\n{$episode_content}\n\n【本集生成剧本内容】\n{$reference_content}";
  8396. }else {
  8397. $reference_content = "\n\n【原分段剧本内容】\n{$reference_content}";
  8398. }
  8399. }
  8400. if (empty($reference_content)) {
  8401. Utils::throwError('20003:没有可参考的片段内容');
  8402. }
  8403. // 获取剧集的基本信息(intro、art_style、roles、scenes)
  8404. $episode = DB::table('mp_anime_episodes')
  8405. ->where('id', $target_episode_id)
  8406. ->first();
  8407. if (!$episode) {
  8408. Utils::throwError('20003:剧集不存在');
  8409. }
  8410. $anime = DB::table('mp_animes')->where('id', $target_anime_id)->first();
  8411. $anime_script_id = getProp($anime, 'script_id');
  8412. $intro = $episode->intro ?? '';
  8413. $art_style = $episode->art_style ?? '';
  8414. $roles = json_decode($episode->roles ?? '[]', true);
  8415. $scenes = json_decode($episode->scenes ?? '[]', true);
  8416. $props = json_decode($episode->props ?? '[]', true);
  8417. // 构建提示词中的主体列表和场景列表参考
  8418. $roles_ref = '';
  8419. if (!empty($roles) && is_array($roles)) {
  8420. $roles_list = [];
  8421. foreach ($roles as $role) {
  8422. $role_name = getProp($role, 'role', '');
  8423. $role_desc = getProp($role, 'description', '');
  8424. $pic_prompt = getProp($role, 'pic_prompt', '');
  8425. $voice_prompt = getProp($role, 'voice_prompt', '');
  8426. if (!empty($role_name)) {
  8427. $role_line = $role_name;
  8428. if (!empty($role_desc)) $role_line .= ":" . $role_desc;
  8429. if (!empty($pic_prompt)) $role_line .= "{" . $pic_prompt . "}";
  8430. if (!empty($voice_prompt)) $role_line .= "{{" . $voice_prompt . "}}";
  8431. $roles_list[] = $role_line;
  8432. }
  8433. }
  8434. if (!empty($roles_list)) {
  8435. $roles_ref = "\n\n【主体列表参考】\n" . implode("\n", $roles_list);
  8436. }
  8437. }
  8438. $scenes_ref = '';
  8439. if (!empty($scenes) && is_array($scenes)) {
  8440. $scenes_list = [];
  8441. foreach ($scenes as $scene) {
  8442. $scene_name = getProp($scene, 'scene', '');
  8443. $scene_desc = getProp($scene, 'description', '');
  8444. $pic_prompt = getProp($scene, 'pic_prompt', '');
  8445. if (!empty($scene_name)) {
  8446. $scene_line = $scene_name;
  8447. if (!empty($scene_desc)) $scene_line .= ":" . $scene_desc;
  8448. if (!empty($pic_prompt)) $scene_line .= "{" . $pic_prompt . "}";
  8449. $scenes_list[] = $scene_line;
  8450. }
  8451. }
  8452. if (!empty($scenes_list)) {
  8453. $scenes_ref = "\n\n【场景列表参考】\n" . implode("\n", $scenes_list);
  8454. }
  8455. }
  8456. $props_ref = '';
  8457. if (!empty($props) && is_array($props)) {
  8458. $props_list = [];
  8459. foreach ($props as $prop) {
  8460. $prop_name = getProp($prop, 'prop', '');
  8461. $prop_desc = getProp($prop, 'description', '');
  8462. $pic_prompt = getProp($prop, 'pic_prompt', '');
  8463. if (!empty($prop_name)) {
  8464. $prop_line = $prop_name;
  8465. if (!empty($prop_desc)) $prop_line .= ":" . $prop_desc;
  8466. if (!empty($pic_prompt)) $prop_line .= "{" . $pic_prompt . "}";
  8467. $props_list[] = $prop_line;
  8468. }
  8469. }
  8470. if (!empty($props_list)) {
  8471. $props_ref = "\n\n【道具列表参考】\n" . implode("\n", $props_list);
  8472. }
  8473. }
  8474. // 构建美术风格参考
  8475. $art_style_ref = '';
  8476. if (!empty($art_style)) {
  8477. $art_style_ref = "\n\n【美术风格参考】\n" . $art_style;
  8478. }
  8479. // 构建内容简介参考
  8480. $intro_ref = '';
  8481. if (!empty($intro)) {
  8482. $intro_ref = "\n\n【内容简介参考】\n" . $intro;
  8483. }
  8484. // 构建完整的AI提示词
  8485. // $system_prompt = "你是一个专业的剧本编剧和影视导演,擅长根据已有的分段剧本进行优化和调整。你需要严格按照用户的要求重新生成分段剧本内容,保持原有的格式和结构。";
  8486. $user_prompt = "请根据以下要求重新生成分段剧本:\n\n【用户要求】\n{$final_prompt}";
  8487. $user_prompt .= $intro_ref;
  8488. $user_prompt .= $art_style_ref;
  8489. $user_prompt .= $reference_content;
  8490. // $user_prompt .= $roles_ref;
  8491. // $user_prompt .= $scenes_ref;
  8492. $system_prompt = "\n\n【强制要求】\n";
  8493. $system_prompt .= "- 请仅生成<分段剧本>部分,不要生成其他任何内容:";
  8494. $system_prompt .= "- 第一优先级:片段时长上限(单个片段不超过15秒,超过必须切为新片段)";
  8495. $system_prompt .= "- 每个分镜包含:分镜序号、出场角色、场景、出场道具、画面、运镜、配音台词、背景音效\n";
  8496. $system_prompt .= "- 场景必须从【场景列表参考】中选择(如果提供了场景列表),场景名必须精确匹配【场景列表参考】中的名称(如需匹配“酒店会客室-景池筹备会版”,而不是“酒店会客室”,不得缺少“-”前后和“:”前面的任何内容,不能自创,不能自创){$scenes_ref}\n";
  8497. $system_prompt .= "- 出场角色(如果有出现在本分镜)必须从【主体列表参考】中选择(如果提供了主体列表),角色名必须精准匹配【主体列表参考】中的名称(如需匹配“沈秋岚-程家居家围裙造型”,而不是“沈秋岚”,不得缺少“-”前后和“:”前面的任何内容,不能自创,不能自创){$roles_ref}\n";
  8498. $system_prompt .= "- 出场道具(如果有出现在本分镜)必须从【道具列表参考】中选择(如果提供了道具列表),道具名必须精准匹配【道具列表参考】中的名称(如需匹配“智能手机-黑色版”,而不是“智能手机”,不得缺少“-”前后和“:”前面的任何内容,不能自创,不能自创){$props_ref}\n";
  8499. $system_prompt .= "- 【重要】在“画面:”和“运镜:”描述中,如果提到角色或道具,必须使用【主体列表参考】或【道具列表参考】中的完整名称,一个字都不能少,一个字都不能改。例如:正确写法“运镜:镜头推向{林启明-黑色大衣七爷造型}”,错误写法“运镜:镜头推向林启明”(缺少造型后缀)。必须包含“-”及其后面的所有内容,不得简化、不得省略、不得自行修改\n";
  8500. $system_prompt .= "- 保持美术风格的一致性(如果提供了美术风格参考)\n";
  8501. $system_prompt .= "- 参考示例格式如下,此为第一优先规则,必须按以下格式生成内容,请忽略其他任何提示词部分关于规则的描述:
  8502. ###分段剧本
  8503. 片段数量:8
  8504. 旁白音色:成年男性(约35-50岁),音色厚重且富有磁性,带有老电影叙事般的沧桑感,语调低沉平稳,富有江湖气息。
  8505. ##片段1
  8506. 时长:12s
  8507. 分镜1
  8508. 出场角色:刀疤脸
  8509. 场景:边境小镇街道
  8510. 出场道具:酒杯-高脚杯
  8511. 画面:[大远景,冷色调,高对比度] 漫天黄沙在狂风中如怒龙翻滚,遮蔽了微弱的星光。悦来酒馆残破的木门在风中剧烈摇晃,铁链撞击木板发出沉重的闷响,四周建筑尽显荒凉。
  8512. 运镜:大远景固定镜头,随后风沙席卷向镜头形成遮挡转场。
  8513. 配音台词:
  8514. 背景音效:
  8515. 分镜2
  8516. 出场角色:刀疤脸
  8517. 场景:悦来酒馆
  8518. 出场道具:酒杯-高脚杯
  8519. 画面:[全景,暖色调阴影构图] 酒馆内部昏暗,几盏油灯在风中疯狂摇曳,投射出扭曲的长影。角落里的食客低头缩颈,双手紧紧攥着粗瓷碗,空气中漂浮着细小的尘埃。
  8520. 运镜:缓慢横移,掠过阴影中瑟缩的食客。
  8521. 配音台词:
  8522. 背景音效:
  8523. 分镜3
  8524. 出场角色:刀疤脸
  8525. 场景:悦来酒馆
  8526. 出场道具:酒杯-高脚杯
  8527. 画面:[中景,三分法构图] 三名大汉围坐在正中央的圆桌旁,腰间挂着的“武林盟”金色令牌在灯火下闪烁。桌上残羹冷炙横陈,一名大汉正张狂地大笑,脚踩在长凳上。
  8528. 运镜:从中景推向令牌特写。
  8529. 配音台词:
  8530. 背景音效:
  8531. 分镜4
  8532. 出场角色:刀疤脸
  8533. 场景:悦来酒馆
  8534. 出场道具:酒杯-高脚杯
  8535. 画面:[特写,低角度仰拍] 刀疤脸横肉颤动,嘴角挂着轻蔑的笑。他随手倾覆手中的酒碗,琥珀色的液体在空中划过弧线,重重溅在老李打着补丁的布鞋和裤脚上。
  8536. 运镜:极速推向酒水溅起的瞬间。
  8537. 配音台词:
  8538. 背景音效:
  8539. 分镜5
  8540. 出场角色:刀疤脸
  8541. 场景:悦来酒馆
  8542. 出场道具:酒杯-高脚杯
  8543. 画面:[近景,荷兰角倾斜构图] 刀疤脸斜睨着眼,昏暗的灯光勾勒出他脸上狰狞的伤疤。他身体前倾,阴影完全笼罩了对面的老李,眼神如毒蛇般阴冷。
  8544. 运镜:固定机位,利用倾斜构图制造压迫感。
  8545. 配音台词:中文配音:[刀疤脸]老李头,这酒里掺水了吧?
  8546. 背景音效:";
  8547. // 获取模型配置
  8548. $model = getProp($data, 'model');
  8549. if (!$model) {
  8550. $model = $anime ? ($anime->model ?? 'deepseek-v4-pro') : 'deepseek-v4-pro';
  8551. }
  8552. // 验证模型是否在可用模型表中
  8553. if (!DB::table('mp_text_models')->where('model', $model)->where('is_enabled', 1)->exists()) {
  8554. $model = 'deepseek-v4-pro';
  8555. }
  8556. // 模型兼容性处理
  8557. $thinkingMode = 'disabled';
  8558. $reasoningEffort = 'high';
  8559. if ($model === 'deepseek-chat') {
  8560. $model = 'deepseek-v4-flash';
  8561. $thinkingMode = 'disabled';
  8562. } elseif ($model === 'deepseek-reasoner') {
  8563. $model = 'deepseek-v4-flash';
  8564. $thinkingMode = 'enabled';
  8565. }
  8566. // 构建消息
  8567. $messages = [
  8568. [
  8569. 'role' => 'system',
  8570. 'content' => $system_prompt
  8571. ],
  8572. [
  8573. 'role' => 'user',
  8574. 'content' => $user_prompt
  8575. ]
  8576. ];
  8577. // dd($messages);
  8578. // 构建请求参数
  8579. $post_data = [
  8580. 'model' => $model,
  8581. 'messages' => $messages,
  8582. 'temperature' => 0.7,
  8583. 'frequency_penalty' => 0,
  8584. 'presence_penalty' => 0,
  8585. 'response_format' => ['type' => 'text'],
  8586. 'thinking' => ['type' => $thinkingMode],
  8587. 'stream' => false // 非流式输出
  8588. ];
  8589. if ($thinkingMode == 'enabled') $post_data['reasoning_effort'] = $reasoningEffort;
  8590. // 调用AI生成新的分段剧本
  8591. try {
  8592. $fullContent = '';
  8593. $usage = [];
  8594. // 根据模型类型选择调用方法
  8595. if (in_array($model, ['deepseek-reasoner', 'deepseek-chat', 'deepseek-v4-flash', 'deepseek-v4-pro'])) {
  8596. // DeepSeek 官方模型使用 DeepSeek API
  8597. $chatResult = $this->chatOnly($post_data);
  8598. } else if (in_array($model, $this->gpt_text_models)) {
  8599. // GPT 模型使用 TokenRouter API
  8600. $chatResult = $this->gpt54ChatOnly($post_data);
  8601. } else {
  8602. // 其他模型使用火山引擎API
  8603. $chatResult = $this->volcEngineChatCompletion($post_data);
  8604. }
  8605. if (is_array($chatResult)) {
  8606. $fullContent = $chatResult['fullContent'] ?? '';
  8607. $usage = $chatResult['usage'] ?? [];
  8608. }
  8609. $generated_content = $fullContent;
  8610. if (empty($generated_content)) {
  8611. Utils::throwError('20003:AI生成内容为空,请重试');
  8612. }
  8613. // 解析生成的内容 - 按片段分割
  8614. $parsed_segments = [];
  8615. // 先在开头添加换行符,确保第1片段也能被正确分割
  8616. $normalizedText = "\n" . $generated_content;
  8617. $parts = preg_split('/\n\s*##/', $normalizedText);
  8618. foreach ($parts as $part) {
  8619. $part = trim($part);
  8620. if (empty($part)) continue;
  8621. // 匹配"片段X"格式
  8622. if (!preg_match('/^片段\d+/', $part)) {
  8623. continue;
  8624. }
  8625. // 分离标题和内容
  8626. $lines = explode("\n", $part, 2);
  8627. $actTitle = trim($lines[0]);
  8628. $actContent = isset($lines[1]) ? trim($lines[1]) : '';
  8629. // 解析标题,提取序号
  8630. $actNumber = 0;
  8631. if (preg_match('/^片段(\d+)\s*$/u', $actTitle, $segmentTitleMatch)) {
  8632. $actNumber = intval($segmentTitleMatch[1]);
  8633. } else {
  8634. $actNumber = count($parsed_segments) + 1;
  8635. }
  8636. // 提取时长(仅保留数字)
  8637. $actDuration = 0;
  8638. if (preg_match('/时长[::]\s*([^\n]+)/u', $actContent, $actDurationMatch)) {
  8639. $actDurationStr = trim($actDurationMatch[1]);
  8640. // 提取数字部分(支持整数和小数)
  8641. if (preg_match('/([-+]?[0-9]*\.?[0-9]+)/', $actDurationStr, $numMatch)) {
  8642. $actDuration = (float)$numMatch[1];
  8643. }
  8644. }
  8645. // 清理act_content:去除时长、旁白音色等非分镜内容行
  8646. // 只保留分镜内容,并确保分镜标记使用【】格式
  8647. $cleaned_content = '';
  8648. $content_lines = explode("\n", $actContent);
  8649. $is_capturing = false; // 标记是否开始捕获分镜内容
  8650. $count = 0;
  8651. foreach ($content_lines as $line) {
  8652. $trimmed_line = trim($line);
  8653. // 跳过时长和旁白音色行
  8654. if (preg_match('/^(时长|旁白音色)[::]/u', $trimmed_line)) {
  8655. continue;
  8656. }
  8657. // 检测是否是分镜开始
  8658. // 支持格式:【分镜X】、【镜头X】、分镜X、镜头X
  8659. if (preg_match('/^(?:【)?(分镜|镜头)(\d+)(?:】)?/u', $trimmed_line, $shot_match)) {
  8660. $count++;
  8661. $is_capturing = true;
  8662. // 如果没有【】,则添加
  8663. if (!preg_match('/^【/u', $trimmed_line)) {
  8664. $trimmed_line = preg_replace('/^(分镜|镜头)(\d+)/u', '【$1$2】', $trimmed_line);
  8665. }
  8666. if ($count > 1) $trimmed_line = "\n".$trimmed_line;
  8667. }
  8668. // 如果已经开始捕获,则添加该行
  8669. if ($is_capturing && !empty($trimmed_line)) {
  8670. $cleaned_content .= $trimmed_line."\n";
  8671. }
  8672. }
  8673. $cleaned_content = str_replace(["@", "{", "}"], "", $cleaned_content);
  8674. $parsed_segments[] = [
  8675. 'act_number' => $actNumber,
  8676. 'act_title' => $actTitle,
  8677. 'act_duration' => $actDuration,
  8678. 'act_content' => $cleaned_content,
  8679. ];
  8680. }
  8681. if (empty($parsed_segments)) {
  8682. Utils::throwError('20003:无法解析生成的分段剧本,请检查格式');
  8683. }
  8684. // 获取角色和场景名称用于标记
  8685. $role_names = [];
  8686. $scene_names = [];
  8687. if (!empty($roles) && is_array($roles)) {
  8688. foreach ($roles as $role) {
  8689. $role_name = getProp($role, 'role', '');
  8690. if (!empty($role_name)) {
  8691. $role_names[] = $role_name;
  8692. }
  8693. }
  8694. }
  8695. if (!empty($scenes) && is_array($scenes)) {
  8696. foreach ($scenes as $scene) {
  8697. $scene_name = getProp($scene, 'scene', '');
  8698. if (!empty($scene_name)) {
  8699. $scene_names[] = $scene_name;
  8700. }
  8701. }
  8702. }
  8703. // 获取道具名称用于标记
  8704. $prop_names = [];
  8705. if (!empty($props) && is_array($props)) {
  8706. foreach ($props as $prop) {
  8707. $prop_name = getProp($prop, 'prop', '');
  8708. if (!empty($prop_name)) {
  8709. $prop_names[] = $prop_name;
  8710. }
  8711. }
  8712. }
  8713. // 对角色、场景、道具名称按长度降序排序(避免短名称先匹配到长名称的一部分)
  8714. usort($role_names, function($a, $b) {
  8715. return mb_strlen($b) - mb_strlen($a);
  8716. });
  8717. usort($scene_names, function($a, $b) {
  8718. return mb_strlen($b) - mb_strlen($a);
  8719. });
  8720. usort($prop_names, function($a, $b) {
  8721. return mb_strlen($b) - mb_strlen($a);
  8722. });
  8723. // 处理每个片段的 act_show_content
  8724. foreach ($parsed_segments as &$segment) {
  8725. $act_content = getProp($segment, 'act_content', '');
  8726. if (empty($act_content)) {
  8727. $segment['act_show_content'] = '';
  8728. continue;
  8729. }
  8730. $processed_content = $act_content;
  8731. // 先匹配场景(在"场景:"后面)
  8732. foreach ($scene_names as $scene_name) {
  8733. // 转义特殊字符
  8734. $escaped_scene = preg_quote($scene_name, '/');
  8735. // 匹配场景:后面的场景名称(可能换行,也可能不换行)
  8736. $pattern = '/(场景:[\s\r\n]*)(' . $escaped_scene . ')/u';
  8737. $processed_content = preg_replace_callback($pattern, function($matches) {
  8738. $prefix = $matches[1];
  8739. $scene_name = $matches[2];
  8740. // 检查是否已经被标记
  8741. if (strpos($scene_name, '{') === false && strpos($scene_name, '}') === false) {
  8742. return $prefix . '{' . $scene_name . '}';
  8743. }
  8744. return $matches[0];
  8745. }, $processed_content);
  8746. }
  8747. // 匹配道具(在"道具:"后面,逻辑与场景一致)
  8748. foreach ($prop_names as $prop_name) {
  8749. // 转义特殊字符
  8750. $escaped_prop = preg_quote($prop_name, '/');
  8751. // 匹配道具:后面的道具名称(可能换行,也可能不换行)
  8752. $pattern = '/(道具:[\s\r\n]*)(' . $escaped_prop . ')/u';
  8753. $processed_content = preg_replace_callback($pattern, function($matches) {
  8754. $full_match = $matches[0];
  8755. $prefix = $matches[1];
  8756. $prop_name = $matches[2];
  8757. // 检查是否已经被标记
  8758. if (strpos($prop_name, '{') === false && strpos($prop_name, '}') === false) {
  8759. return $prefix . '{' . $prop_name . '}';
  8760. }
  8761. return $full_match;
  8762. }, $processed_content);
  8763. }
  8764. // 再匹配角色名称(在"画面:"部分和"角色:"部分)
  8765. foreach ($role_names as $role_name) {
  8766. // 转义特殊字符
  8767. $escaped_role = preg_quote($role_name, '/');
  8768. // 1. 匹配"画面:"开始到换行之前的内容
  8769. $pattern = '/(画面:[^\n]*)/u';
  8770. $processed_content = preg_replace_callback($pattern, function($matches) use ($escaped_role) {
  8771. $line = $matches[1]; // 整行"画面:xxxxx"
  8772. // 在这一行中匹配所有角色名(避免重复标记)
  8773. $new_line = preg_replace(
  8774. '/(?<![{])(' . $escaped_role . ')(?![}])/u',
  8775. '{$1}',
  8776. $line
  8777. );
  8778. return $new_line;
  8779. }, $processed_content);
  8780. // 2. 匹配"角色:"或"出场角色:"这一整行内的角色名称
  8781. $pattern = '/((?:出场)?角色:[^\n]*)/u';
  8782. $processed_content = preg_replace_callback($pattern, function($matches) use ($escaped_role) {
  8783. $line = $matches[1]; // 整行"角色:xxxxx"或"出场角色:xxxxx"
  8784. // 在这一行中匹配所有角色名(避免重复标记)
  8785. $new_line = preg_replace(
  8786. '/(?<![{])(' . $escaped_role . ')(?![}])/u',
  8787. '{$1}',
  8788. $line
  8789. );
  8790. return $new_line;
  8791. }, $processed_content);
  8792. }
  8793. // 匹配道具名称(在"画面:"部分,逻辑与角色一致)
  8794. foreach ($prop_names as $prop_name) {
  8795. // 转义特殊字符
  8796. $escaped_prop = preg_quote($prop_name, '/');
  8797. // 匹配"画面:"开始到换行之前的内容
  8798. $pattern = '/(画面:[^\n]*)/u';
  8799. $processed_content = preg_replace_callback($pattern, function($matches) use ($escaped_prop) {
  8800. $line = $matches[1]; // 整行"画面:xxxxx"
  8801. // 在这一行中匹配所有道具名(避免重复标记)
  8802. $new_line = preg_replace(
  8803. '/(?<![{])(' . $escaped_prop . ')(?![}])/u',
  8804. '{$1}',
  8805. $line
  8806. );
  8807. return $new_line;
  8808. }, $processed_content);
  8809. }
  8810. // 匹配角色名称(在"运镜:"部分)
  8811. foreach ($role_names as $role_name) {
  8812. // 转义特殊字符
  8813. $escaped_role = preg_quote($role_name, '/');
  8814. // 匹配"运镜:"开始到换行之前的内容
  8815. $pattern = '/(运镜:[^\n]*)/u';
  8816. $processed_content = preg_replace_callback($pattern, function($matches) use ($escaped_role) {
  8817. $line = $matches[1]; // 整行"运镜:xxxxx"
  8818. // 在这一行中匹配所有角色名(避免重复标记)
  8819. $new_line = preg_replace(
  8820. '/(?<![{])(' . $escaped_role . ')(?![}])/u',
  8821. '{$1}',
  8822. $line
  8823. );
  8824. return $new_line;
  8825. }, $processed_content);
  8826. }
  8827. // 匹配道具名称(在"运镜:"部分)
  8828. foreach ($prop_names as $prop_name) {
  8829. // 转义特殊字符
  8830. $escaped_prop = preg_quote($prop_name, '/');
  8831. // 匹配"运镜:"开始到换行之前的内容
  8832. $pattern = '/(运镜:[^\n]*)/u';
  8833. $processed_content = preg_replace_callback($pattern, function($matches) use ($escaped_prop) {
  8834. $line = $matches[1]; // 整行"运镜:xxxxx"
  8835. // 在这一行中匹配所有道具名(避免重复标记)
  8836. $new_line = preg_replace(
  8837. '/(?<![{])(' . $escaped_prop . ')(?![}])/u',
  8838. '{$1}',
  8839. $line
  8840. );
  8841. return $new_line;
  8842. }, $processed_content);
  8843. }
  8844. // 处理【镜头X】或【分镜X】重新编号为从1开始的连续整数【镜头X】
  8845. $shot_counter = 0;
  8846. $processed_content = preg_replace_callback(
  8847. '/【(?:镜头|分镜)(\d+)】/u',
  8848. function($matches) use (&$shot_counter) {
  8849. $shot_counter++;
  8850. return '【镜头' . $shot_counter . '】';
  8851. },
  8852. $processed_content
  8853. );
  8854. $segment['act_show_content'] = $processed_content;
  8855. }
  8856. unset($segment); // 解除引用
  8857. // 保存到数据库
  8858. $now = date('Y-m-d H:i:s');
  8859. $saved_acts = []; // 用于记录保存后的片段信息
  8860. if ($act_id > 0) {
  8861. // 单个片段模式:更新单条记录
  8862. if (count($parsed_segments) > 0) {
  8863. $new_segment = $parsed_segments[0];
  8864. $update_data = [
  8865. 'act_content' => getProp($new_segment, 'act_content', ''),
  8866. 'act_show_content' => getProp($new_segment, 'act_show_content', ''),
  8867. 'act_title' => getProp($new_segment, 'act_title', ''),
  8868. 'act_duration' => getProp($new_segment, 'act_duration', 0),
  8869. 'updated_at' => $now,
  8870. ];
  8871. DB::table('mp_episode_segments')
  8872. ->where('id', $act_id)
  8873. ->update($update_data);
  8874. // 获取更新后的记录
  8875. $updated_act = DB::table('mp_episode_segments')
  8876. ->where('id', $act_id)
  8877. ->first();
  8878. if ($updated_act) {
  8879. $saved_acts[] = [
  8880. 'act_id' => $updated_act->id,
  8881. 'act_number' => $updated_act->act_number,
  8882. 'act_duration' => $updated_act->video_duration ? $updated_act->video_duration : $updated_act->act_duration,
  8883. 'act_content' => $updated_act->act_content,
  8884. 'act_show_content' => $updated_act->act_show_content,
  8885. 'video_url' => $updated_act->video_url ?? '',
  8886. 'video_duration' => $updated_act->video_duration ?? 0,
  8887. 'video_time_point_start' => $updated_act->video_time_point_start ?? 0,
  8888. 'video_time_point_end' => $updated_act->video_time_point_end ?? 0,
  8889. ];
  8890. }
  8891. dLog('deepseek')->info('单个片段重新生成成功', ['act_id' => $act_id]);
  8892. }
  8893. } else {
  8894. // 整个剧集模式:删除原有记录并批量保存新记录
  8895. DB::beginTransaction();
  8896. try {
  8897. // 删除原有的所有片段记录
  8898. DB::table('mp_episode_segments')
  8899. ->where('episode_id', $target_episode_id)
  8900. ->delete();
  8901. // 批量插入新的片段记录
  8902. $segments_to_insert = [];
  8903. foreach ($parsed_segments as $index => $segment) {
  8904. $act_number = $index + 1;
  8905. $segments_to_insert[] = [
  8906. 'anime_id' => $target_anime_id,
  8907. 'episode_id' => $target_episode_id,
  8908. 'episode_number' => $target_episode_number,
  8909. 'act_number' => $act_number,
  8910. 'act_title' => getProp($segment, 'act_title', ''),
  8911. 'act_duration' => getProp($segment, 'act_duration', 0),
  8912. 'act_content' => getProp($segment, 'act_content', ''),
  8913. 'act_show_content' => getProp($segment, 'act_show_content', ''),
  8914. 'created_at' => $now,
  8915. 'updated_at' => $now,
  8916. ];
  8917. }
  8918. if (!empty($segments_to_insert)) {
  8919. DB::table('mp_episode_segments')->insert($segments_to_insert);
  8920. }
  8921. DB::commit();
  8922. // 查询保存后的所有片段记录
  8923. $saved_segments = DB::table('mp_episode_segments')
  8924. ->where('episode_id', $target_episode_id)
  8925. ->orderBy('act_number')
  8926. ->get();
  8927. foreach ($saved_segments as $saved_segment) {
  8928. $saved_acts[] = [
  8929. 'act_id' => $saved_segment->id,
  8930. 'act_number' => $saved_segment->act_number,
  8931. 'act_duration' => $saved_segment->video_duration ? $saved_segment->video_duration : $saved_segment->act_duration,
  8932. 'act_content' => $saved_segment->act_content,
  8933. 'act_show_content' => $saved_segment->act_show_content,
  8934. 'video_url' => $saved_segment->video_url ?? '',
  8935. 'video_duration' => $saved_segment->video_duration ?? 0,
  8936. 'video_time_point_start' => $saved_segment->video_time_point_start ?? 0,
  8937. 'video_time_point_end' => $saved_segment->video_time_point_end ?? 0,
  8938. ];
  8939. }
  8940. dLog('deepseek')->info('整个剧集片段重新生成成功', [
  8941. 'episode_id' => $target_episode_id,
  8942. 'segments_count' => count($segments_to_insert)
  8943. ]);
  8944. } catch (\Exception $e) {
  8945. DB::rollBack();
  8946. dLog('deepseek')->error('保存重新生成的片段失败: ' . $e->getMessage());
  8947. Utils::throwError('20003:保存失败,请重试');
  8948. }
  8949. }
  8950. // 构造返回数据结构(参考AnimeService返回格式,但不包含products)
  8951. return [
  8952. 'anime_id' => $target_anime_id,
  8953. 'episode_id' => $target_episode_id,
  8954. 'title' => getProp($episode, 'title', ''),
  8955. 'episode_number' => $target_episode_number,
  8956. 'is_generated' => getProp($episode, 'is_generated', 0),
  8957. 'acts' => $saved_acts,
  8958. ];
  8959. } catch (\Exception $e) {
  8960. dLog('deepseek')->error('重新生成分段剧本失败: ' . $e->getMessage());
  8961. throw $e;
  8962. }
  8963. }
  8964. private function buildSingleEpisodeFromScript(array $script_arr, string $fullContent, string $animeName = ''): array
  8965. {
  8966. $episode_arr = [
  8967. 'episode_title' => $animeName, // 单剧集默认使用剧本名
  8968. 'intro' => getProp($script_arr, 'intro'),
  8969. 'art_style' => getProp($script_arr, 'art_style'),
  8970. 'roles' => is_array(getProp($script_arr, 'roles')) ? getProp($script_arr, 'roles') : [],
  8971. 'scenes' => is_array(getProp($script_arr, 'scenes')) ? getProp($script_arr, 'scenes') : [],
  8972. 'props' => is_array(getProp($script_arr, 'props')) ? getProp($script_arr, 'props') : [],
  8973. 'acts' => is_array(getProp($script_arr, 'acts')) ? getProp($script_arr, 'acts') : [],
  8974. ];
  8975. if (empty($episode_arr['acts'])) {
  8976. $fallback_episode_arr = handleEpisodeContent($fullContent);
  8977. if (!empty($fallback_episode_arr['acts'])) {
  8978. $episode_arr = array_merge($fallback_episode_arr, [
  8979. 'intro' => getProp($fallback_episode_arr, 'intro') ?: getProp($script_arr, 'intro'),
  8980. 'art_style' => getProp($fallback_episode_arr, 'art_style') ?: getProp($script_arr, 'art_style'),
  8981. 'roles' => !empty(getProp($fallback_episode_arr, 'roles')) ? getProp($fallback_episode_arr, 'roles') : getProp($script_arr, 'roles', []),
  8982. 'scenes' => !empty(getProp($fallback_episode_arr, 'scenes')) ? getProp($fallback_episode_arr, 'scenes') : getProp($script_arr, 'scenes', []),
  8983. ]);
  8984. }
  8985. }
  8986. // if (!getProp($episode_arr, 'episode_title')) {
  8987. // $episode_title = '';
  8988. // $episodes = getProp($script_arr, 'episodes', []);
  8989. // if (is_array($episodes) && !empty($episodes[0]['title'])) {
  8990. // $episode_title = '第1集:' . $episodes[0]['title'];
  8991. // }
  8992. // if (!$episode_title) {
  8993. // $episode_title = '第1集:' . ($animeName ?: '未命名');
  8994. // }
  8995. // $episode_arr['episode_title'] = $episode_title;
  8996. // }
  8997. return $episode_arr;
  8998. }
  8999. private function saveEpisodeVersionData(
  9000. int $anime_id,
  9001. int $episode_number,
  9002. array $episode_arr,
  9003. array $existing_roles,
  9004. array $existing_scenes,
  9005. array $existing_props,
  9006. $current_episode,
  9007. $base_episode,
  9008. bool $is_regenerate_version,
  9009. string $content,
  9010. string $now,
  9011. array $ref_products = []
  9012. ): array {
  9013. $generated_roles = isset($episode_arr['roles']) && is_array($episode_arr['roles']) ? $episode_arr['roles'] : [];
  9014. $generated_scenes = isset($episode_arr['scenes']) && is_array($episode_arr['scenes']) ? $episode_arr['scenes'] : [];
  9015. $generated_props = isset($episode_arr['props']) && is_array($episode_arr['props']) ? $episode_arr['props'] : [];
  9016. $merged_roles = $generated_roles;
  9017. $merged_scenes = $generated_scenes;
  9018. $merged_props = $generated_props;
  9019. // $merged_roles = $this->mergeEpisodeResourceItems($existing_roles, $generated_roles, 'role');
  9020. // $merged_scenes = $this->mergeEpisodeResourceItems($existing_scenes, $generated_scenes, 'scene');
  9021. // 获取 mp_animes 表中的全局 roles 和 scenes 用于对比
  9022. $anime = DB::table('mp_animes')->where('id', $anime_id)->first();
  9023. $ace_mode = getProp($anime, 'ace_mode');
  9024. dLog('deepseek')->info('ace_mode', ['ace_mode' => $ace_mode]);
  9025. $global_roles = json_decode(getProp($anime, 'roles'), true);
  9026. $global_scenes = json_decode(getProp($anime, 'scenes'), true);
  9027. $global_roles = is_array($global_roles) ? $global_roles : [];
  9028. $global_scenes = is_array($global_scenes) ? $global_scenes : [];
  9029. $art_style = getProp($episode_arr, 'art_style') ?: getProp($anime, 'art_style');
  9030. $art_style_type = getProp($anime, 'art_style_type');
  9031. // 检查并创建 roles 的图片生成任务
  9032. // $merged_roles = $this->checkAndCreateImageTasks($merged_roles, $global_roles, 'role', $art_style);
  9033. // 检查并创建 scenes 的图片生成任务
  9034. // $merged_scenes = $this->checkAndCreateImageTasks($merged_scenes, $global_scenes, 'scene', $art_style);
  9035. $arr = $this->batchSetEpisodeRolesAndScenesImg($anime_id, $episode_number, $merged_roles, $merged_scenes, $merged_props, $ref_products, $art_style_type);
  9036. if ($arr && is_array($arr)) {
  9037. $merged_roles = $arr['roles'];
  9038. $merged_scenes = $arr['scenes'];
  9039. $merged_props = isset($arr['props']) ? $arr['props'] : [];
  9040. }
  9041. // 确保数据是数组类型
  9042. if (!is_array($merged_roles)) {
  9043. dLog('deepseek')->error('merged_roles不是数组类型', [
  9044. 'type' => gettype($merged_roles),
  9045. 'value' => $merged_roles
  9046. ]);
  9047. $merged_roles = [];
  9048. }
  9049. if (!is_array($merged_scenes)) {
  9050. dLog('deepseek')->error('merged_scenes不是数组类型', [
  9051. 'type' => gettype($merged_scenes),
  9052. 'value' => $merged_scenes
  9053. ]);
  9054. $merged_scenes = [];
  9055. }
  9056. if (!is_array($merged_props)) {
  9057. dLog('deepseek')->error('merged_props不是数组类型', [
  9058. 'type' => gettype($merged_props),
  9059. 'value' => $merged_props
  9060. ]);
  9061. $merged_props = [];
  9062. }
  9063. // 同步新出现的主体和场景到全局
  9064. // $this->syncNewResourcesToGlobal($anime_id, $merged_roles, $merged_scenes, $global_roles, $global_scenes);
  9065. $role_arr = [];
  9066. foreach ($merged_roles as $item) {
  9067. $role_arr[$item['role']] = $item;
  9068. }
  9069. $version_count = DB::table('mp_anime_episodes')
  9070. ->where('anime_id', $anime_id)
  9071. ->where('episode_number', $episode_number)
  9072. ->count('id');
  9073. $episode = [
  9074. 'anime_id' => $anime_id,
  9075. 'episode_number' => $episode_number,
  9076. 'title' => getProp($episode_arr, 'episode_title'),
  9077. 'content' => $content,
  9078. 'intro' => getProp($episode_arr, 'intro'),
  9079. 'art_style' => getProp($episode_arr, 'art_style'),
  9080. 'roles' => json_encode($merged_roles, 256),
  9081. 'scenes' => json_encode($merged_scenes, 256),
  9082. 'props' => json_encode($merged_props, 256),
  9083. 'generate_status' => '待执行',
  9084. 'generate_at' => $now,
  9085. 'is_default' => 1,
  9086. 'updated_at' => $now,
  9087. ];
  9088. $del_flag = false;
  9089. if ($is_regenerate_version) {
  9090. DB::table('mp_anime_episodes')
  9091. ->where('anime_id', $anime_id)
  9092. ->where('episode_number', $episode_number)
  9093. ->update(['is_default' => 0, 'updated_at' => $now]);
  9094. $episode['sequence'] = $version_count + 1;
  9095. $episode['created_at'] = $now;
  9096. $episode['cpid'] = Site::getCpid();
  9097. $episode_id = DB::table('mp_anime_episodes')->insertGetId($episode);
  9098. if (!$episode_id) {
  9099. Utils::throwError('20003:创建剧集版本失败');
  9100. }
  9101. } else {
  9102. $target_episode = $current_episode ?: $base_episode;
  9103. if ($target_episode) {
  9104. $episode_id = getProp($target_episode, 'id');
  9105. $episode['sequence'] = getProp($target_episode, 'sequence', 1);
  9106. DB::table('mp_anime_episodes')
  9107. ->where('anime_id', $anime_id)
  9108. ->where('episode_number', $episode_number)
  9109. ->where('id', '<>', $episode_id)
  9110. ->update(['is_default' => 0, 'updated_at' => $now]);
  9111. $boolen = DB::table('mp_anime_episodes')->where('id', $episode_id)->update($episode);
  9112. if ($boolen === false) {
  9113. logDB('deepseek', 'error', '分集内容保存失败', ['anime_id' => $anime_id, 'episode_id' => $episode_id]);
  9114. Utils::throwError('20003:分集内容保存失败');
  9115. }
  9116. $del_flag = true;
  9117. } else {
  9118. DB::table('mp_anime_episodes')
  9119. ->where('anime_id', $anime_id)
  9120. ->where('episode_number', $episode_number)
  9121. ->update(['is_default' => 0, 'updated_at' => $now]);
  9122. $episode['sequence'] = $version_count > 0 ? ($version_count + 1) : 1;
  9123. $episode['created_at'] = $now;
  9124. $episode['cpid'] = Site::getCpid();
  9125. $episode_id = DB::table('mp_anime_episodes')->insertGetId($episode);
  9126. if (!$episode_id) {
  9127. logDB('deepseek', 'error', '分集内容保存失败', ['anime_id' => $anime_id, 'episode_number' => $episode_number]);
  9128. Utils::throwError('20003:分集内容保存失败');
  9129. }
  9130. }
  9131. }
  9132. $segments = [];
  9133. $acts = isset($episode_arr['acts']) && is_array($episode_arr['acts']) ? $episode_arr['acts'] : [];
  9134. // 如果是第2集及以后,获取上一集的主体音色数据
  9135. $previous_roles_voice = [];
  9136. if ($episode_number >= 2) {
  9137. $previous_episode = DB::table('mp_anime_episodes')
  9138. ->where('anime_id', $anime_id)
  9139. ->where('episode_number', $episode_number - 1)
  9140. ->where('is_default', 1)
  9141. ->first();
  9142. if ($previous_episode) {
  9143. $previous_roles = json_decode(getProp($previous_episode, 'roles'), true);
  9144. if (is_array($previous_roles)) {
  9145. foreach ($previous_roles as $prev_role) {
  9146. $role_name = getProp($prev_role, 'role');
  9147. if ($role_name) {
  9148. $previous_roles_voice[$role_name] = [
  9149. 'voice_name' => getProp($prev_role, 'voice_name'),
  9150. 'voice_type' => getProp($prev_role, 'voice_type'),
  9151. 'voice_audio_url' => getProp($prev_role, 'voice_audio_url'),
  9152. ];
  9153. $voice_prompt = getProp($prev_role, 'voice_prompt');
  9154. if ($voice_prompt) {
  9155. $previous_roles_voice[$role_name]['voice_prompt'] = $voice_prompt;
  9156. }
  9157. }
  9158. }
  9159. }
  9160. }
  9161. }
  9162. // 将继承的音色数据同步到当前集的主体列表
  9163. if (!empty($previous_roles_voice)) {
  9164. foreach ($merged_roles as &$role) {
  9165. $role_name = getProp($role, 'role');
  9166. if ($role_name && isset($previous_roles_voice[$role_name])) {
  9167. // 强制继承上一集的音色数据
  9168. $role['voice_name'] = $previous_roles_voice[$role_name]['voice_name'];
  9169. $role['voice_type'] = $previous_roles_voice[$role_name]['voice_type'];
  9170. $role['voice_audio_url'] = $previous_roles_voice[$role_name]['voice_audio_url'];
  9171. // 如果上一集有 voice_prompt 则继承,否则跳过
  9172. if (!empty($previous_roles_voice[$role_name]['voice_prompt'])) {
  9173. $role['voice_prompt'] = $previous_roles_voice[$role_name]['voice_prompt'];
  9174. }
  9175. }
  9176. }
  9177. unset($role); // 解除引用
  9178. // 更新 role_arr 以便后续使用
  9179. $role_arr = [];
  9180. foreach ($merged_roles as $item) {
  9181. $role_arr[$item['role']] = $item;
  9182. }
  9183. }
  9184. $emotion_list = DB::table('mp_emotion_list')->where('is_enabled', 1)->pluck('emotion_name', 'emotion_code')->toArray();
  9185. $emotion_list = array_flip($emotion_list);
  9186. foreach ($acts as $act) {
  9187. $act_segments = getProp($act, 'segments', []);
  9188. if (!is_array($act_segments)) {
  9189. continue;
  9190. }
  9191. if ((int)$ace_mode === 1) {
  9192. // ace_mode=1: 按幕合并 segment_content,每幕只存一条记录
  9193. $merged_parts = [];
  9194. foreach ($act_segments as $seg) {
  9195. $seg_num = getProp($seg, 'segment_number');
  9196. $seg_content = getProp($seg, 'segment_content');
  9197. $merged_parts[] = "【分镜{$seg_num}】\n{$seg_content}\n";
  9198. }
  9199. $act_content = implode("\n", $merged_parts);
  9200. $act_content = str_replace(["@", "{", "}"], "", $act_content);
  9201. $segments[] = [
  9202. 'anime_id' => $anime_id,
  9203. 'episode_id' => $episode_id,
  9204. 'episode_number' => $episode_number,
  9205. 'act_number' => getProp($act, 'act_number'),
  9206. 'act_title' => getProp($act, 'act_title'),
  9207. 'act_duration' => getProp($act, 'act_duration'),
  9208. 'act_content' => $act_content,
  9209. 'created_at' => $now,
  9210. 'updated_at' => $now
  9211. ];
  9212. } else {
  9213. foreach ($act_segments as $segment) {
  9214. $voice_actor = getProp($segment, 'voice_actor');
  9215. // 优先从上一集继承音色数据
  9216. $timbre_info = [];
  9217. if (isset($previous_roles_voice[$voice_actor])) {
  9218. // 从上一集继承音色数据
  9219. $timbre_info = $previous_roles_voice[$voice_actor];
  9220. } elseif (isset($role_arr[$voice_actor])) {
  9221. // 使用当前集的主体音色数据
  9222. $timbre_info = $role_arr[$voice_actor];
  9223. }
  9224. $voice_type = getProp($timbre_info, 'voice_type');
  9225. $timbre_emotion = DB::table('mp_timbres')->where('timbre_type', $voice_type)->value('emotion');
  9226. if ($timbre_emotion) {
  9227. $timbre_emotion = explode(',', $timbre_emotion);
  9228. } else {
  9229. $timbre_emotion = [];
  9230. }
  9231. $emotion = getProp($segment, 'emotion', '中性');
  9232. if (!in_array($emotion, $timbre_emotion)) {
  9233. $emotion = '中性';
  9234. }
  9235. $emotion_type = isset($emotion_list[$emotion]) ? $emotion_list[$emotion] : 'neutral';
  9236. $segments[] = [
  9237. 'anime_id' => $anime_id,
  9238. 'episode_id' => $episode_id,
  9239. 'episode_number' => $episode_number,
  9240. 'act_number' => getProp($act, 'act_number'),
  9241. 'act_title' => getProp($act, 'act_title'),
  9242. 'act_duration' => getProp($act, 'act_duration'),
  9243. 'segment_id' => getProp($segment, 'segment_id'),
  9244. 'segment_number' => getProp($segment, 'segment_number'),
  9245. 'segment_content' => getProp($segment, 'segment_content'),
  9246. 'description' => getProp($segment, 'description'),
  9247. 'composition' => getProp($segment, 'composition'),
  9248. 'camera_movement' => getProp($segment, 'camera_movement'),
  9249. 'voice_actor' => $voice_actor,
  9250. 'dialogue' => getProp($segment, 'dialogue'),
  9251. 'frame_type' => getProp($segment, 'frame_type'),
  9252. 'scene' => getProp($segment, 'scene'),
  9253. 'characters' => getProp($segment, 'characters'),
  9254. 'tail_frame' => getProp($segment, 'tail_frame'),
  9255. 'voice_name' => getProp($timbre_info, 'voice_name'),
  9256. 'voice_type' => $voice_type,
  9257. 'voice_audio_url' => getProp($timbre_info, 'voice_audio_url'),
  9258. 'emotion' => $emotion,
  9259. 'emotion_type' => $emotion_type,
  9260. 'gender' => getProp($segment, 'gender', '0'),
  9261. 'speed_ratio' => getProp($segment, 'speed_ratio', 0),
  9262. 'loudness_ratio' => getProp($segment, 'loudness_ratio', 0),
  9263. 'emotion_scale' => getProp($segment, 'emotion_scale', 4),
  9264. 'pitch' => getProp($segment, 'pitch', 0),
  9265. 'created_at' => $now,
  9266. 'updated_at' => $now
  9267. ];
  9268. }
  9269. }
  9270. }
  9271. if ($segments) {
  9272. if ($del_flag) {
  9273. DB::table('mp_episode_segments')
  9274. ->where('anime_id', $anime_id)
  9275. ->where('episode_id', $episode_id)
  9276. ->delete();
  9277. }
  9278. $boolen = DB::table('mp_episode_segments')->insert($segments);
  9279. if (!$boolen) {
  9280. logDB('deepseek', 'error', '分镜内容保存失败', ['anime_id' => $anime_id, 'episode_id' => $episode_id, 'segments_count' => count($segments)]);
  9281. Utils::throwError('20003:分镜内容保存失败');
  9282. }
  9283. }
  9284. dLog('deepseek')->info('segments', $segments);
  9285. // ace_mode=1: acts 返回值按合并格式调整
  9286. if ((int)$ace_mode === 1) {
  9287. $table_act_data = DB::table('mp_episode_segments')->where('episode_id', $episode_id)->orderBy('act_number')->select('id as act_id', 'act_number')->get();
  9288. $act_map = [];
  9289. foreach ($table_act_data as $item) {
  9290. $act_map[getProp($item, 'act_number')] = getProp($item, 'act_id');
  9291. }
  9292. $restructured_acts = [];
  9293. foreach ($acts as $act) {
  9294. $act_segments = getProp($act, 'segments', []);
  9295. if (!is_array($act_segments)) {
  9296. continue;
  9297. }
  9298. $merged_parts = [];
  9299. foreach ($act_segments as $seg) {
  9300. $seg_num = getProp($seg, 'segment_number');
  9301. $seg_content = getProp($seg, 'segment_content');
  9302. $merged_parts[] = "【分镜{$seg_num}】{$seg_content}\n";
  9303. }
  9304. $act_content = implode("\n", $merged_parts);
  9305. $act_content = str_replace(["@", "{", "}"], "", $act_content);
  9306. $restructured_acts[] = [
  9307. 'anime_id' => $anime_id,
  9308. 'episode_id' => $episode_id,
  9309. 'episode_number' => $episode_number,
  9310. 'act_id' => isset($act_map[getProp($act, 'act_number')]) ? $act_map[getProp($act, 'act_number')] : 0,
  9311. 'act_number' => getProp($act, 'act_number'),
  9312. 'act_title' => getProp($act, 'act_title'),
  9313. 'act_duration' => getProp($act, 'act_duration'),
  9314. 'act_content' => $act_content,
  9315. 'created_at' => $now,
  9316. 'updated_at' => $now,
  9317. ];
  9318. }
  9319. $acts = $restructured_acts;
  9320. }
  9321. $episode['episode_id'] = $episode_id;
  9322. $episode['roles'] = $merged_roles;
  9323. $episode['scenes'] = $merged_scenes;
  9324. $episode['props'] = $merged_props;
  9325. $episode['acts'] = $acts;
  9326. return [
  9327. 'episode' => $episode,
  9328. 'episode_id' => $episode_id,
  9329. 'merged_roles' => $merged_roles,
  9330. 'merged_scenes' => $merged_scenes,
  9331. 'merged_props' => $merged_props,
  9332. ];
  9333. }
  9334. private function buildEpisodeItemContent(array $items, string $nameKey): string
  9335. {
  9336. $content = '';
  9337. foreach ($items as $item) {
  9338. $name = trim((string)getProp($item, $nameKey));
  9339. if ($name === '' || $name === '旁白') {
  9340. continue;
  9341. }
  9342. $description = trim((string)getProp($item, 'description'));
  9343. $content .= $name . ': ' . $description;
  9344. if ($nameKey == 'role') {
  9345. $pic_prompt = trim(getProp($item, 'pic_prompt'));
  9346. if ($pic_prompt) $content .= '{'.$pic_prompt.'}';
  9347. $voice_name = trim(getProp($item, 'voice_name'));
  9348. if ($voice_name) $content .= '{{'.$voice_name.'}}';
  9349. $voice_prompt = trim(getProp($item, 'voice_prompt'));
  9350. if ($voice_prompt) $content .= '{{'.$voice_prompt.'}}';
  9351. }else {
  9352. $pic_prompt = trim(getProp($item, 'pic_prompt'));
  9353. if ($pic_prompt) $content .= '{'.$pic_prompt.'}';
  9354. }
  9355. $content .= "\n";
  9356. }
  9357. return trim($content);
  9358. }
  9359. private function getEpisodeChatContent($animeId, $sequence, $content) {
  9360. if ((int)$sequence <= 0) {
  9361. return [];
  9362. }
  9363. $origin_content = '';
  9364. if ($content) {
  9365. $origin_content = '本集剧情内容:'.$content;
  9366. }else {
  9367. $origin_content = DB::table('mp_animes')->where('id', $animeId)->value('content');
  9368. if ($origin_content) $origin_content = '剧本全文内容:'.$origin_content;
  9369. }
  9370. $ace_mode = DB::table('mp_animes')->where('id', $animeId)->value('ace_mode');
  9371. // 获取分集信息
  9372. $episode = DB::table('mp_anime_episodes')
  9373. ->where('anime_id', $animeId)
  9374. ->where('episode_number', $sequence)
  9375. ->where('is_default', 1)
  9376. ->first();
  9377. if (!$episode) {
  9378. return [];
  9379. }
  9380. $episode_id = getProp($episode, 'id');
  9381. $episode_number = getProp($episode, 'episode_number');
  9382. $title = getProp($episode, 'title');
  9383. $intro = getProp($episode, 'intro');
  9384. $art_style = getProp($episode, 'art_style');
  9385. $roles = json_decode(getProp($episode, 'roles'), true) ?: [];
  9386. $scenes = json_decode(getProp($episode, 'scenes'), true) ?: [];
  9387. // 组装纯文本内容
  9388. $content = '';
  9389. // 添加分集标题和梗概
  9390. $content .= "###第{$episode_number}集:{$title}\n\n";
  9391. $content .= "###故事梗概\n";
  9392. $content .= trim($intro) . "\n\n";
  9393. // 添加美术风格
  9394. $content .= "###美术风格\n";
  9395. $content .= trim($art_style) . "\n\n";
  9396. // 添加主体列表
  9397. $content .= "###主体列表\n";
  9398. $pangbai_voice_prompt = "";
  9399. foreach ($roles as $role) {
  9400. $name = getProp($role, 'role');
  9401. $description = getProp($role, 'description');
  9402. $pic_prompt = getProp($role, 'pic_prompt');
  9403. $voice_prompt = getProp($role, 'voice_prompt');
  9404. $voice_name = getProp($role, 'voice_name');
  9405. $content .= "{$name}: {$description}";
  9406. if ($pic_prompt) $content .= '{'.$pic_prompt.'}';
  9407. if ($voice_prompt) $content .= '{{'.$voice_prompt.'}}';
  9408. if ($voice_name) $content .= '{{'.$voice_name.'}}';
  9409. if ($name == '旁白') $pangbai_voice_prompt = $voice_prompt;
  9410. $content .= "\n";
  9411. }
  9412. $content .= "\n";
  9413. // 添加场景列表
  9414. $content .= "###场景列表\n";
  9415. foreach ($scenes as $scene) {
  9416. $name = getProp($scene, 'scene');
  9417. $description = getProp($scene, 'description');
  9418. $pic_prompt = getProp($scene, 'pic_prompt');
  9419. $content .= "{$name}: {$description}";
  9420. if ($pic_prompt) $content .= '{'.$pic_prompt.'}';
  9421. $content .= "\n";
  9422. }
  9423. $content .= "\n";
  9424. // 获取分镜信息
  9425. $segments = DB::table('mp_episode_segments')
  9426. ->where('anime_id', $animeId)
  9427. ->where('episode_id', $episode_id)
  9428. ->orderBy('segment_number')
  9429. ->get();
  9430. if ($segments && count($segments) > 0) {
  9431. if ((int)$ace_mode === 1) {
  9432. $content .= "###分段剧本\n";
  9433. // 获取片段数量
  9434. $act_count = DB::table('mp_episode_segments')->where('anime_id', $animeId)->where('episode_id', $episode_id)->groupBy('act_number')->count('act_number');
  9435. $content .= "片段数量:{$act_count}\n";
  9436. if ($pangbai_voice_prompt) $content .= "旁白音色:{$pangbai_voice_prompt}\n";
  9437. $content .= "\n";
  9438. }else {
  9439. $content .= "###分镜剧本\n";
  9440. }
  9441. // 按幕分组
  9442. $currentAct = null;
  9443. foreach ($segments as $segment) {
  9444. $act_number = getProp($segment, 'act_number');
  9445. $act_title = getProp($segment, 'act_title');
  9446. $act_duration = getProp($segment, 'act_duration');
  9447. $segment_number = getProp($segment, 'segment_number');
  9448. $segment_content = getProp($segment, 'segment_content');
  9449. $scene_description = getProp($segment, 'scene_description');
  9450. $scene_name = getProp($segment, 'scene_name');
  9451. $composition = getProp($segment, 'composition');
  9452. $camera_movement = getProp($segment, 'camera_movement');
  9453. $voice_type = getProp($segment, 'voice_type');
  9454. $characters = getProp($segment, 'characters');
  9455. $dialogue = getProp($segment, 'dialogue');
  9456. $frame_type = getProp($segment, 'frame_type');
  9457. $tail_frame_description = getProp($segment, 'tail_frame_description');
  9458. // 判断是否是全能模式
  9459. if ((int)$ace_mode === 1) { // 全能模式
  9460. // 如果是新的分段,添加分段标题
  9461. if ($act_number !== $currentAct) {
  9462. $currentAct = $act_number;
  9463. $content .= "##{$act_title}\n";
  9464. $content .= "时长:{$act_duration}s\n";
  9465. }
  9466. }else {
  9467. // 如果是新的幕,添加幕标题
  9468. if ($act_number !== $currentAct) {
  9469. $currentAct = $act_number;
  9470. $content .= "##第{$act_number}幕:{$scene_name}\n";
  9471. }
  9472. }
  9473. // 添加分镜信息
  9474. $content .= "分镜{$segment_number}\n";
  9475. $content .= "分镜内容:\n{$segment_content}\n";
  9476. // $content .= "画面描述:{$scene_description}\n";
  9477. // $content .= "场景:{$scene_name}\n";
  9478. // $content .= "构图设计:{$composition}\n";
  9479. // $content .= "运镜调度:{$camera_movement}\n";
  9480. // if (!empty($voice_type)) {
  9481. // $content .= "配音角色:{$voice_type}\n";
  9482. // }
  9483. // if (!empty($characters)) {
  9484. // $content .= "出镜角色:{$characters}\n";
  9485. // }
  9486. // if (!empty($dialogue)) {
  9487. // $content .= "台词内容:\"{$dialogue}\"\n";
  9488. // }
  9489. // $content .= "画面类型:{$frame_type}\n";
  9490. // $content .= "尾帧描述:{$tail_frame_description}\n";
  9491. $content .= "\n";
  9492. }
  9493. }
  9494. $content = trim($content);
  9495. if($content) $content = "上集剧本内容:\n{$content}";
  9496. return [
  9497. [
  9498. 'role' => 'user',
  9499. 'content' => $origin_content
  9500. ],
  9501. [
  9502. 'role' => 'assistant',
  9503. 'content' => $content
  9504. ]
  9505. ];
  9506. }
  9507. private function getEpisodeChatMessages($animeId, $sequence): array
  9508. {
  9509. if ((int)$sequence <= 0) {
  9510. return [];
  9511. }
  9512. return DB::table('mp_anime_records')
  9513. ->where('anime_id', $animeId)
  9514. ->where('sequence', $sequence)
  9515. ->where('episode_id', '>', 0)
  9516. ->select('role', 'content')
  9517. ->orderBy('created_at')
  9518. ->orderBy('id')
  9519. ->get()
  9520. ->map(function ($value) {
  9521. return (array)$value;
  9522. })
  9523. ->toArray();
  9524. }
  9525. private function mergeEpisodeResourceItems(array $existingItems, array $generatedItems, string $nameKey): array
  9526. {
  9527. $existingMap = [];
  9528. foreach ($existingItems as $item) {
  9529. $itemKey = $this->normalizeEpisodeResourceKey(getProp($item, $nameKey));
  9530. if ($itemKey === '') {
  9531. continue;
  9532. }
  9533. $existingMap[$itemKey] = $item;
  9534. }
  9535. if (!$generatedItems) {
  9536. return array_values($existingItems);
  9537. }
  9538. $merged = [];
  9539. foreach ($generatedItems as $item) {
  9540. $itemKey = $this->normalizeEpisodeResourceKey(getProp($item, $nameKey));
  9541. if ($itemKey === '') {
  9542. continue;
  9543. }
  9544. if (isset($existingMap[$itemKey])) {
  9545. if (trim((string)getProp($item, 'description')) === '') {
  9546. $merged[] = $existingMap[$itemKey];
  9547. continue;
  9548. }
  9549. // 检查内容是否发生变化
  9550. $isChanged = $this->isEpisodeResourceChanged($existingMap[$itemKey], $item, $nameKey);
  9551. if (!$isChanged) {
  9552. // nameKey 和 description 一致,继承现有项的 URL、task_id 和 task_status
  9553. $mergedItem = [
  9554. $nameKey => trim((string)getProp($item, $nameKey)),
  9555. 'description' => trim((string)getProp($item, 'description')),
  9556. ];
  9557. // 如果有 pic_prompt,添加到合并项中
  9558. $picPrompt = getProp($item, 'pic_prompt');
  9559. if (!empty($picPrompt)) {
  9560. $mergedItem['pic_prompt'] = trim((string)$picPrompt);
  9561. }
  9562. // 继承现有项的 URL
  9563. $existingUrl = getProp($existingMap[$itemKey], 'url');
  9564. if (!empty($existingUrl)) {
  9565. $mergedItem['url'] = $existingUrl;
  9566. }
  9567. // 继承现有项的 task_id
  9568. $existingTaskId = getProp($existingMap[$itemKey], 'task_id');
  9569. if (!empty($existingTaskId)) {
  9570. $mergedItem['task_id'] = $existingTaskId;
  9571. }
  9572. // 继承现有项的 task_status
  9573. $existingTaskStatus = getProp($existingMap[$itemKey], 'task_status');
  9574. if (!empty($existingTaskStatus)) {
  9575. $mergedItem['task_status'] = $existingTaskStatus;
  9576. }
  9577. // 保留生成项的音色字段
  9578. $voiceName = getProp($item, 'voice_name');
  9579. if (!empty($voiceName)) {
  9580. $mergedItem['voice_name'] = $voiceName;
  9581. }
  9582. $voiceType = getProp($item, 'voice_type');
  9583. if (!empty($voiceType)) {
  9584. $mergedItem['voice_type'] = $voiceType;
  9585. }
  9586. $audioUrl = getProp($item, 'voice_audio_url');
  9587. if (!empty($audioUrl)) {
  9588. $mergedItem['voice_audio_url'] = $audioUrl;
  9589. }
  9590. $merged[] = $mergedItem;
  9591. } else {
  9592. // nameKey 或 description 不一致,重置 URL、task_id 和 task_status,但保留音色字段
  9593. $mergedItem = [
  9594. $nameKey => trim((string)getProp($item, $nameKey)),
  9595. 'description' => trim((string)getProp($item, 'description')),
  9596. ];
  9597. // 如果有 pic_prompt,添加到合并项中
  9598. $picPrompt = getProp($item, 'pic_prompt');
  9599. if (!empty($picPrompt)) {
  9600. $mergedItem['pic_prompt'] = trim((string)$picPrompt);
  9601. }
  9602. // 保留生成项的音色字段
  9603. $voiceName = getProp($item, 'voice_name');
  9604. if (!empty($voiceName)) {
  9605. $mergedItem['voice_name'] = $voiceName;
  9606. }
  9607. $voiceType = getProp($item, 'voice_type');
  9608. if (!empty($voiceType)) {
  9609. $mergedItem['voice_type'] = $voiceType;
  9610. }
  9611. $audioUrl = getProp($item, 'voice_audio_url');
  9612. if (!empty($audioUrl)) {
  9613. $mergedItem['voice_audio_url'] = $audioUrl;
  9614. }
  9615. // 不继承 URL、task_id 和 task_status(重置状态)
  9616. $merged[] = $mergedItem;
  9617. }
  9618. } else {
  9619. // 新增项,保留生成项的所有字段
  9620. $merged[] = $this->resetEpisodeResourceItem($item, $nameKey, ['url', 'task_id', 'task_status', 'voice_name', 'voice_type', 'voice_audio_url', 'pic_prompt']);
  9621. }
  9622. }
  9623. return $merged ?: array_values($existingItems);
  9624. }
  9625. private function resetEpisodeResourceItem(array $item, string $nameKey, array $preserveFields = []): array
  9626. {
  9627. $resetItem = [
  9628. $nameKey => trim((string)getProp($item, $nameKey)),
  9629. 'description' => trim((string)getProp($item, 'description')),
  9630. ];
  9631. // 保留指定的字段
  9632. foreach ($preserveFields as $field) {
  9633. $value = getProp($item, $field);
  9634. if (!empty($value)) {
  9635. $resetItem[$field] = $value;
  9636. }
  9637. }
  9638. return $resetItem;
  9639. }
  9640. private function isEpisodeResourceChanged(array $existingItem, array $generatedItem, string $nameKey): bool
  9641. {
  9642. $existingKey = $this->normalizeEpisodeResourceKey(getProp($existingItem, $nameKey));
  9643. $generatedKey = $this->normalizeEpisodeResourceKey(getProp($generatedItem, $nameKey));
  9644. if ($existingKey !== $generatedKey) {
  9645. return true;
  9646. }
  9647. // 比较 description 是否变化
  9648. if (trim((string)getProp($existingItem, 'description')) !== trim((string)getProp($generatedItem, 'description'))) {
  9649. return true;
  9650. }
  9651. // 比较 pic_prompt 是否变化
  9652. return trim((string)getProp($existingItem, 'pic_prompt')) !== trim((string)getProp($generatedItem, 'pic_prompt'));
  9653. }
  9654. private function normalizeEpisodeResourceKey($value): string
  9655. {
  9656. $value = trim((string)$value);
  9657. if ($value === '') {
  9658. return '';
  9659. }
  9660. return strtolower((string)preg_replace('/\s+/u', '', $value));
  9661. }
  9662. // 生成全局角色图片
  9663. private function batchSetGlobalRoleImg($data, $is_force=false) {
  9664. $anime_id = getProp($data, 'anime_id');
  9665. if (!$anime_id) Utils::throwError('1002:请选择对话');
  9666. $anime = DB::table('mp_animes')->where('id', $anime_id)->where('is_deleted', 0)->first();
  9667. $roles = json_decode(getProp($anime, 'roles'), true) ?: [];
  9668. $art_style = getProp($anime, 'art_style');
  9669. $update_flag = false;
  9670. foreach ($roles as &$role) {
  9671. $role_name = getProp($role, 'role');
  9672. if ($role_name == '旁白') continue;
  9673. // 优先使用 pic_prompt,如果没有则使用 description
  9674. $pic_prompt = getProp($role, 'pic_prompt');
  9675. $description = !empty($pic_prompt) ? trim((string)$pic_prompt) : getProp($role, 'description');
  9676. // if ($art_style) $description = "美术风格:\n{$art_style}\n主体描述:{$description}\n图片要求:图片侧重点是人物,不要出现场景或背景,并且只能生成一个人物";
  9677. if ($art_style) $description = "主体描述:{$description}\n图片要求:图片侧重点是人物,不要出现场景或背景,并且只能生成一个人物";
  9678. // 参考图地址
  9679. $ref_img_url = getProp($role, 'url');
  9680. if (!$is_force && $ref_img_url) continue;
  9681. $update_flag = true;
  9682. // 此处调用AIImageGenerationService的生成图片任务接口: createImageGenerationTask(注意使用默认参数,prompt使用角色描述),并返回任务id
  9683. try {
  9684. $params = [
  9685. 'prompt' => $description,
  9686. // 'ref_img_urls' => !empty($ref_img_url) ? [$ref_img_url] : []
  9687. 'ref_img_urls' => []
  9688. ];
  9689. $task = $this->aiImageGenerationService->createImageGenerationTask($params);
  9690. $task_id = $task->id;
  9691. if (!$task_id) {
  9692. Utils::throwError("20003: 角色({$role_name})生成图片失败");
  9693. }
  9694. $role['task_id'] = $task_id;
  9695. $role['task_status'] = 'processing';
  9696. } catch (\Exception $e) {
  9697. Utils::throwError("20003:" . $e->getMessage());
  9698. }
  9699. }
  9700. if (!$update_flag) return true;
  9701. // 保存角色信息
  9702. $boolen = DB::table('mp_animes')->where('id', $anime_id)->update([
  9703. 'roles' => json_encode($roles, 256),
  9704. 'generate_status' => '执行中',
  9705. 'generate_at' => date('Y-m-d H:i:s'),
  9706. 'updated_at' => date('Y-m-d H:i:s')
  9707. ]);
  9708. if (!$boolen) {
  9709. Utils::throwError('20003:保存角色信息失败');
  9710. }
  9711. return $boolen;
  9712. }
  9713. // 生成全局场景图片
  9714. private function batchSetGlobalSceneImg($data, $is_force=false) {
  9715. $anime_id = getProp($data, 'anime_id');
  9716. if (!$anime_id) Utils::throwError('1002:请选择对话');
  9717. $anime = DB::table('mp_animes')->where('id', $anime_id)->where('is_deleted', 0)->first();
  9718. $scenes = json_decode(getProp($anime, 'scenes'), true) ?: [];
  9719. $art_style = getProp($anime, 'art_style');
  9720. $update_flag = false;
  9721. foreach ($scenes as &$scene) {
  9722. $scene_name = getProp($scene, 'scene');
  9723. // 优先使用 pic_prompt,如果没有则使用 description
  9724. $pic_prompt = getProp($scene, 'pic_prompt');
  9725. $description = !empty($pic_prompt) ? trim((string)$pic_prompt) : getProp($scene, 'description');
  9726. if ($art_style) $description = "场景描述:{$description}\n图片要求: 图片侧重点是场景,生成的是场景环境图片,不要出现人物(此为第一优先要求)";
  9727. // 参考图地址
  9728. $ref_img_url = getProp($scene, 'url');
  9729. if (!$is_force && $ref_img_url) continue;
  9730. $update_flag = true;
  9731. // 此处调用AIImageGenerationService的生成图片任务接口: createImageGenerationTask(注意使用默认参数,prompt使用角色描述),并返回任务id
  9732. try {
  9733. $params = [
  9734. 'prompt' => $description,
  9735. // 'ref_img_urls' => !empty($ref_img_url) ? [$ref_img_url] : [],
  9736. 'ref_img_urls' => []
  9737. ];
  9738. $task = $this->aiImageGenerationService->createImageGenerationTask($params);
  9739. $task_id = $task->id;
  9740. if (!$task_id) {
  9741. Utils::throwError("20003: 场景({$scene_name})生成图片失败");
  9742. }
  9743. $scene['task_id'] = $task_id;
  9744. $scene['task_status'] = 'processing';
  9745. } catch (\Exception $e) {
  9746. Utils::throwError("20003:" . $e->getMessage());
  9747. }
  9748. }
  9749. if (!$update_flag) return true;
  9750. // 保存角色信息
  9751. $boolen = DB::table('mp_animes')->where('id', $anime_id)->update([
  9752. 'scenes' => json_encode($scenes, 256),
  9753. 'generate_status' => '执行中',
  9754. 'generate_at' => date('Y-m-d H:i:s'),
  9755. 'updated_at' => date('Y-m-d H:i:s')
  9756. ]);
  9757. if (!$boolen) {
  9758. Utils::throwError('20003:保存角色信息失败');
  9759. }
  9760. return $boolen;
  9761. }
  9762. // 生成分镜主体、场景和道具图片
  9763. private function batchSetEpisodeRolesAndScenesImg($anime_id, $episode_number, $roles, $scenes, $props = [], $ref_products=[], $art_style_type='', $is_force=false) {
  9764. $character_prefix = '';
  9765. $scene_prefix = '';
  9766. $prop_prefix = '';
  9767. if ($art_style_type) {
  9768. $art_style_arr = $this->getArtStyleShortPromptByInput($art_style_type);
  9769. $character_prefix = isset($art_style_arr['character_prefix']) ? $art_style_arr['character_prefix'] : '';
  9770. $scene_prefix = isset($art_style_arr['scene_prefix']) ? $art_style_arr['scene_prefix'] : '';
  9771. $prop_prefix = isset($art_style_arr['scene_prefix']) ? $art_style_arr['scene_prefix'] : ''; // 道具使用场景的前缀
  9772. }
  9773. $update_flag = false;
  9774. // 获取上一集的roles、scenes和props数据
  9775. $prev_roles = [];
  9776. $prev_scenes = [];
  9777. $prev_props = [];
  9778. if ($episode_number > 1) {
  9779. $prev_episode = DB::table('mp_anime_episodes')
  9780. ->where('anime_id', $anime_id)
  9781. ->where('episode_number', $episode_number - 1)
  9782. ->where('is_default', 1)
  9783. ->first();
  9784. if ($prev_episode) {
  9785. $prev_roles = json_decode(getProp($prev_episode, 'roles'), true) ?: [];
  9786. $prev_scenes = json_decode(getProp($prev_episode, 'scenes'), true) ?: [];
  9787. $prev_props = json_decode(getProp($prev_episode, 'props'), true) ?: [];
  9788. dLog('deepseek')->info("获取上一集数据成功", [
  9789. 'anime_id' => $anime_id,
  9790. 'prev_episode_number' => $episode_number - 1,
  9791. 'prev_roles_count' => count($prev_roles),
  9792. 'prev_scenes_count' => count($prev_scenes),
  9793. 'prev_props_count' => count($prev_props)
  9794. ]);
  9795. }
  9796. }
  9797. // 处理角色图片生成
  9798. foreach ($roles as &$role) {
  9799. $role_name = getProp($role, 'role');
  9800. if ($role_name == '旁白') continue;
  9801. // 如果参考资产里有该角色,则直接使用该图片,不继续生成
  9802. if (isset($ref_products[$role_name])) {
  9803. $role['url'] = isset($ref_products[$role_name]['url']) ? $ref_products[$role_name]['url'] : '';
  9804. if ($role['url']) continue;
  9805. }
  9806. $role_description = getProp($role, 'description');
  9807. $role_pic_prompt = getProp($role, 'pic_prompt');
  9808. $role_url = getProp($role, 'url');
  9809. // 检查是否可以从上一集继承
  9810. $inherited = false;
  9811. if (!$is_force && !empty($prev_roles)) {
  9812. foreach ($prev_roles as $prev_role) {
  9813. $prev_role_name = getProp($prev_role, 'role');
  9814. $prev_description = getProp($prev_role, 'description');
  9815. $prev_pic_prompt = getProp($prev_role, 'pic_prompt');
  9816. $prev_url = getProp($prev_role, 'url');
  9817. $prev_task_id = getProp($prev_role, 'task_id');
  9818. $prev_task_status = getProp($prev_role, 'task_status');
  9819. // 如果role、description、pic_prompt都一致,且上一集已有url,则继承
  9820. if ($role_name == $prev_role_name
  9821. && $role_description == $prev_description
  9822. && $role_pic_prompt == $prev_pic_prompt
  9823. && !empty($prev_url)) {
  9824. $role['task_id'] = $prev_task_id;
  9825. $role['task_status'] = $prev_task_status;
  9826. $role['url'] = $prev_url;
  9827. $inherited = true;
  9828. dLog('deepseek')->info("角色({$role_name})从上一集继承图片数据", [
  9829. 'anime_id' => $anime_id,
  9830. 'episode_number' => $episode_number,
  9831. 'task_id' => $prev_task_id,
  9832. 'url' => $prev_url
  9833. ]);
  9834. break;
  9835. }
  9836. }
  9837. }
  9838. // 如果已继承或已有url,跳过生成
  9839. if ($inherited || (!$is_force && $role_url)) {
  9840. continue;
  9841. }
  9842. // 优先使用 pic_prompt,如果没有则使用 description
  9843. $pic_prompt = getProp($role, 'pic_prompt');
  9844. $description = !empty($pic_prompt) ? trim((string)$pic_prompt) : getProp($role, 'description');
  9845. $description = "{$character_prefix}。\n主体描述:{$description}\n超高清,高细节,主体突出,构图合理,画面干净,无场景或背景,单人物,无文字,无水印,无字幕,电影级光影,视觉焦点明确";
  9846. $update_flag = true;
  9847. // 调用AIImageGenerationService的生成图片任务接口
  9848. try {
  9849. $params = [
  9850. 'prompt' => $description,
  9851. 'ref_img_urls' => []
  9852. ];
  9853. $task = $this->aiImageGenerationService->createImageGenerationTask($params);
  9854. $task_id = $task->id;
  9855. if (!$task_id) {
  9856. dLog('deepseek')->error("角色({$role_name})生成图片失败");
  9857. continue; // 不中断,继续处理其他角色
  9858. }
  9859. $role['task_id'] = $task_id;
  9860. $role['task_status'] = 'processing';
  9861. dLog('deepseek')->info("角色({$role_name})创建图片生成任务成功", [
  9862. 'anime_id' => $anime_id,
  9863. 'episode_number' => $episode_number,
  9864. 'task_id' => $task_id
  9865. ]);
  9866. } catch (\Exception $e) {
  9867. dLog('deepseek')->error("角色({$role_name})创建图片生成任务失败: " . $e->getMessage());
  9868. // 不抛出异常,继续处理其他角色
  9869. }
  9870. }
  9871. // 处理场景图片生成
  9872. foreach ($scenes as &$scene) {
  9873. $scene_name = getProp($scene, 'scene');
  9874. $scene_description = getProp($scene, 'description');
  9875. $scene_pic_prompt = getProp($scene, 'pic_prompt');
  9876. $scene_url = getProp($scene, 'url');
  9877. // 如果参考资产里有该场景,则直接使用该图片,不继续生成
  9878. if (isset($ref_products[$scene_name])) {
  9879. $scene['url'] = isset($ref_products[$scene_name]['url']) ? $ref_products[$scene_name]['url'] : '';
  9880. if ($scene['url']) continue;
  9881. }
  9882. // 检查是否可以从上一集继承
  9883. $inherited = false;
  9884. if (!$is_force && !empty($prev_scenes)) {
  9885. foreach ($prev_scenes as $prev_scene) {
  9886. $prev_scene_name = getProp($prev_scene, 'scene');
  9887. $prev_description = getProp($prev_scene, 'description');
  9888. $prev_pic_prompt = getProp($prev_scene, 'pic_prompt');
  9889. $prev_url = getProp($prev_scene, 'url');
  9890. $prev_task_id = getProp($prev_scene, 'task_id');
  9891. $prev_task_status = getProp($prev_scene, 'task_status');
  9892. // 如果scene、description、pic_prompt都一致,且上一集已有url,则继承
  9893. if ($scene_name == $prev_scene_name
  9894. && $scene_description == $prev_description
  9895. && $scene_pic_prompt == $prev_pic_prompt
  9896. && !empty($prev_url)) {
  9897. $scene['task_id'] = $prev_task_id;
  9898. $scene['task_status'] = $prev_task_status;
  9899. $scene['url'] = $prev_url;
  9900. $inherited = true;
  9901. dLog('deepseek')->info("场景({$scene_name})从上一集继承图片数据", [
  9902. 'anime_id' => $anime_id,
  9903. 'episode_number' => $episode_number,
  9904. 'task_id' => $prev_task_id,
  9905. 'url' => $prev_url
  9906. ]);
  9907. break;
  9908. }
  9909. }
  9910. }
  9911. // 如果已继承或已有url,跳过生成
  9912. if ($inherited || (!$is_force && $scene_url)) {
  9913. continue;
  9914. }
  9915. // 优先使用 pic_prompt,如果没有则使用 description
  9916. $pic_prompt = getProp($scene, 'pic_prompt');
  9917. $description = !empty($pic_prompt) ? trim((string)$pic_prompt) : getProp($scene, 'description');
  9918. $description = "{$scene_prefix}。\n场景描述:{$description}\n无人物,超高清,高细节,主体突出,构图合理,画面干净,无文字,无水印,无字幕,电影级光影,视觉焦点明确";
  9919. $update_flag = true;
  9920. // 调用AIImageGenerationService的生成图片任务接口
  9921. try {
  9922. $params = [
  9923. 'prompt' => $description,
  9924. 'ref_img_urls' => []
  9925. ];
  9926. $task = $this->aiImageGenerationService->createImageGenerationTask($params);
  9927. $task_id = $task->id;
  9928. if (!$task_id) {
  9929. dLog('deepseek')->error("场景({$scene_name})生成图片失败");
  9930. continue; // 不中断,继续处理其他场景
  9931. }
  9932. $scene['task_id'] = $task_id;
  9933. $scene['task_status'] = 'processing';
  9934. dLog('deepseek')->info("场景({$scene_name})创建图片生成任务成功", [
  9935. 'anime_id' => $anime_id,
  9936. 'episode_number' => $episode_number,
  9937. 'task_id' => $task_id
  9938. ]);
  9939. } catch (\Exception $e) {
  9940. dLog('deepseek')->error("场景({$scene_name})创建图片生成任务失败: " . $e->getMessage());
  9941. // 不抛出异常,继续处理其他场景
  9942. }
  9943. }
  9944. // 处理道具图片生成(逻辑与场景一致)
  9945. foreach ($props as &$prop) {
  9946. $prop_name = getProp($prop, 'prop');
  9947. $prop_description = getProp($prop, 'description');
  9948. $prop_pic_prompt = getProp($prop, 'pic_prompt');
  9949. $prop_url = getProp($prop, 'url');
  9950. // 如果参考资产里有该道具,则直接使用该图片,不继续生成
  9951. if (isset($ref_products[$prop_name])) {
  9952. $prop['url'] = isset($ref_products[$prop_name]['url']) ? $ref_products[$prop_name]['url'] : '';
  9953. if ($prop['url']) continue;
  9954. }
  9955. // 检查是否可以从上一集继承
  9956. $inherited = false;
  9957. if (!$is_force && !empty($prev_props)) {
  9958. foreach ($prev_props as $prev_prop) {
  9959. $prev_prop_name = getProp($prev_prop, 'prop');
  9960. $prev_description = getProp($prev_prop, 'description');
  9961. $prev_pic_prompt = getProp($prev_prop, 'pic_prompt');
  9962. $prev_url = getProp($prev_prop, 'url');
  9963. $prev_task_id = getProp($prev_prop, 'task_id');
  9964. $prev_task_status = getProp($prev_prop, 'task_status');
  9965. // 如果prop、description、pic_prompt都一致,且上一集已有url,则继承
  9966. if ($prop_name == $prev_prop_name
  9967. && $prop_description == $prev_description
  9968. && $prop_pic_prompt == $prev_pic_prompt
  9969. && !empty($prev_url)) {
  9970. $prop['task_id'] = $prev_task_id;
  9971. $prop['task_status'] = $prev_task_status;
  9972. $prop['url'] = $prev_url;
  9973. $inherited = true;
  9974. dLog('deepseek')->info("道具({$prop_name})从上一集继承图片数据", [
  9975. 'anime_id' => $anime_id,
  9976. 'episode_number' => $episode_number,
  9977. 'task_id' => $prev_task_id,
  9978. 'url' => $prev_url
  9979. ]);
  9980. break;
  9981. }
  9982. }
  9983. }
  9984. // 如果已继承或已有url,跳过生成
  9985. if ($inherited || (!$is_force && $prop_url)) {
  9986. continue;
  9987. }
  9988. // 优先使用 pic_prompt,如果没有则使用 description
  9989. $pic_prompt = getProp($prop, 'pic_prompt');
  9990. $description = !empty($pic_prompt) ? trim((string)$pic_prompt) : getProp($prop, 'description');
  9991. $description = "{$prop_prefix}。\n道具描述:{$description}\n无人物,超高清,高细节,主体突出,构图合理,画面干净,无文字,无水印,无字幕,电影级光影,视觉焦点明确";
  9992. $update_flag = true;
  9993. // 调用AIImageGenerationService的生成图片任务接口
  9994. try {
  9995. $params = [
  9996. 'prompt' => $description,
  9997. 'ref_img_urls' => []
  9998. ];
  9999. $task = $this->aiImageGenerationService->createImageGenerationTask($params);
  10000. $task_id = $task->id;
  10001. if (!$task_id) {
  10002. dLog('deepseek')->error("道具({$prop_name})生成图片失败");
  10003. continue; // 不中断,继续处理其他道具
  10004. }
  10005. $prop['task_id'] = $task_id;
  10006. $prop['task_status'] = 'processing';
  10007. dLog('deepseek')->info("道具({$prop_name})创建图片生成任务成功", [
  10008. 'anime_id' => $anime_id,
  10009. 'episode_number' => $episode_number,
  10010. 'task_id' => $task_id
  10011. ]);
  10012. } catch (\Exception $e) {
  10013. dLog('deepseek')->error("道具({$prop_name})创建图片生成任务失败: " . $e->getMessage());
  10014. // 不抛出异常,继续处理其他道具
  10015. }
  10016. }
  10017. if (!$update_flag) return ['roles' => $roles, 'scenes' => $scenes, 'props' => $props];
  10018. // 将处理完毕的roles、scenes和props与全局(mp_animes)数据对比并合并
  10019. $this->syncRolesAndScenesToGlobal($anime_id, $roles, $scenes, $props);
  10020. // // 保存剧集的角色和场景信息
  10021. // $boolen = DB::table('mp_anime_episodes')->where('id', $episode_id)->update([
  10022. // 'roles' => json_encode($roles, 256),
  10023. // 'scenes' => json_encode($scenes, 256),
  10024. // 'generate_status' => '执行中',
  10025. // 'generate_at' => date('Y-m-d H:i:s'),
  10026. // 'updated_at' => date('Y-m-d H:i:s')
  10027. // ]);
  10028. // if (!$boolen) {
  10029. // dLog('deepseek')->error('保存剧集角色和场景信息失败', ['episode_id' => $episode_id]);
  10030. // Utils::throwError('20003:保存剧集角色和场景信息失败');
  10031. // }
  10032. dLog('deepseek')->info('剧集角色、场景和道具图片任务创建完成', [
  10033. // 'episode_id' => $episode_id,
  10034. 'roles_count' => count($roles),
  10035. 'scenes_count' => count($scenes),
  10036. 'props_count' => count($props)
  10037. ]);
  10038. return ['roles' => $roles, 'scenes' => $scenes, 'props' => $props];
  10039. }
  10040. /**
  10041. * 将剧集的roles、scenes和props同步到全局(mp_animes)
  10042. * 如果全局中不存在相同的role/scene/prop(名称、description、pic_prompt三项一致),则添加到全局
  10043. *
  10044. * @param int $anime_id 动漫对话ID
  10045. * @param array $episode_roles 剧集角色数据
  10046. * @param array $episode_scenes 剧集场景数据
  10047. * @param array $episode_props 剧集道具数据
  10048. * @return bool
  10049. */
  10050. private function syncRolesAndScenesToGlobal($anime_id, $episode_roles, $episode_scenes, $episode_props = []) {
  10051. // 获取全局数据
  10052. $anime = DB::table('mp_animes')->where('id', $anime_id)->where('is_deleted', 0)->first();
  10053. if (!$anime) {
  10054. dLog('deepseek')->warning('动漫对话不存在', ['anime_id' => $anime_id]);
  10055. return false;
  10056. }
  10057. $global_roles = json_decode(getProp($anime, 'roles'), true) ?: [];
  10058. $global_scenes = json_decode(getProp($anime, 'scenes'), true) ?: [];
  10059. $global_props = json_decode(getProp($anime, 'props'), true) ?: [];
  10060. $roles_added = false;
  10061. $roles_updated = false;
  10062. $scenes_added = false;
  10063. $props_added = false;
  10064. // 处理角色同步
  10065. foreach ($episode_roles as $episode_role) {
  10066. $episode_role_name = getProp($episode_role, 'role');
  10067. $episode_description = getProp($episode_role, 'description');
  10068. $episode_pic_prompt = getProp($episode_role, 'pic_prompt');
  10069. $episode_voice_prompt = getProp($episode_role, 'voice_prompt');
  10070. // 跳过旁白
  10071. if ($episode_role_name == '旁白') {
  10072. continue;
  10073. }
  10074. // 检查全局中是否已存在相同的角色
  10075. $exists = false;
  10076. $global_role_index = -1;
  10077. foreach ($global_roles as $index => $global_role) {
  10078. $global_role_name = getProp($global_role, 'role');
  10079. $global_description = getProp($global_role, 'description');
  10080. $global_pic_prompt = getProp($global_role, 'pic_prompt');
  10081. // 如果role、description、pic_prompt三项都一致,则认为已存在
  10082. if ($episode_role_name == $global_role_name
  10083. && $episode_description == $global_description
  10084. && $episode_pic_prompt == $global_pic_prompt) {
  10085. $exists = true;
  10086. $global_role_index = $index;
  10087. break;
  10088. }
  10089. }
  10090. // 如果不存在,则添加到全局
  10091. if (!$exists) {
  10092. $global_roles[] = $episode_role;
  10093. $roles_added = true;
  10094. dLog('deepseek')->info("角色({$episode_role_name})添加到全局", [
  10095. 'anime_id' => $anime_id,
  10096. 'role' => $episode_role_name
  10097. ]);
  10098. } else {
  10099. // 如果存在,且剧集有voice_prompt字段,将voice_prompt更新到全局
  10100. if (!empty($episode_voice_prompt)) {
  10101. $global_voice_prompt = getProp($global_roles[$global_role_index], 'voice_prompt');
  10102. // 如果全局没有voice_prompt或与剧集不一致,则更新
  10103. if ($global_voice_prompt !== $episode_voice_prompt) {
  10104. $global_roles[$global_role_index]['voice_prompt'] = $episode_voice_prompt;
  10105. $roles_updated = true;
  10106. dLog('deepseek')->info("角色({$episode_role_name})的voice_prompt更新到全局", [
  10107. 'anime_id' => $anime_id,
  10108. 'role' => $episode_role_name,
  10109. 'voice_prompt' => $episode_voice_prompt
  10110. ]);
  10111. }
  10112. }
  10113. }
  10114. }
  10115. // 处理场景同步
  10116. foreach ($episode_scenes as $episode_scene) {
  10117. $episode_scene_name = getProp($episode_scene, 'scene');
  10118. $episode_description = getProp($episode_scene, 'description');
  10119. $episode_pic_prompt = getProp($episode_scene, 'pic_prompt');
  10120. // 检查全局中是否已存在相同的场景
  10121. $exists = false;
  10122. foreach ($global_scenes as $global_scene) {
  10123. $global_scene_name = getProp($global_scene, 'scene');
  10124. $global_description = getProp($global_scene, 'description');
  10125. $global_pic_prompt = getProp($global_scene, 'pic_prompt');
  10126. // 如果scene、description、pic_prompt三项都一致,则认为已存在
  10127. if ($episode_scene_name == $global_scene_name
  10128. && $episode_description == $global_description
  10129. && $episode_pic_prompt == $global_pic_prompt) {
  10130. $exists = true;
  10131. break;
  10132. }
  10133. }
  10134. // 如果不存在,则添加到全局
  10135. if (!$exists) {
  10136. $global_scenes[] = $episode_scene;
  10137. $scenes_added = true;
  10138. dLog('deepseek')->info("场景({$episode_scene_name})添加到全局", [
  10139. 'anime_id' => $anime_id,
  10140. 'scene' => $episode_scene_name
  10141. ]);
  10142. }
  10143. }
  10144. // 处理道具同步(逻辑与场景一致)
  10145. foreach ($episode_props as $episode_prop) {
  10146. $episode_prop_name = getProp($episode_prop, 'prop');
  10147. $episode_description = getProp($episode_prop, 'description');
  10148. $episode_pic_prompt = getProp($episode_prop, 'pic_prompt');
  10149. // 检查全局中是否已存在相同的道具
  10150. $exists = false;
  10151. foreach ($global_props as $global_prop) {
  10152. $global_prop_name = getProp($global_prop, 'prop');
  10153. $global_description = getProp($global_prop, 'description');
  10154. $global_pic_prompt = getProp($global_prop, 'pic_prompt');
  10155. // 如果prop、description、pic_prompt三项都一致,则认为已存在
  10156. if ($episode_prop_name == $global_prop_name
  10157. && $episode_description == $global_description
  10158. && $episode_pic_prompt == $global_pic_prompt) {
  10159. $exists = true;
  10160. break;
  10161. }
  10162. }
  10163. // 如果不存在,则添加到全局
  10164. if (!$exists) {
  10165. $global_props[] = $episode_prop;
  10166. $props_added = true;
  10167. dLog('deepseek')->info("道具({$episode_prop_name})添加到全局", [
  10168. 'anime_id' => $anime_id,
  10169. 'prop' => $episode_prop_name
  10170. ]);
  10171. }
  10172. }
  10173. // 如果有新增或更新,则更新全局数据
  10174. if ($roles_added || $roles_updated || $scenes_added || $props_added) {
  10175. $update_data = ['updated_at' => date('Y-m-d H:i:s')];
  10176. if ($roles_added || $roles_updated) {
  10177. $update_data['roles'] = json_encode($global_roles, 256);
  10178. }
  10179. if ($scenes_added) {
  10180. $update_data['scenes'] = json_encode($global_scenes, 256);
  10181. }
  10182. if ($props_added) {
  10183. $update_data['props'] = json_encode($global_props, 256);
  10184. }
  10185. DB::table('mp_animes')->where('id', $anime_id)->update($update_data);
  10186. dLog('deepseek')->info('剧集数据同步到全局完成', [
  10187. 'anime_id' => $anime_id,
  10188. 'roles_added' => $roles_added,
  10189. 'roles_updated' => $roles_updated,
  10190. 'scenes_added' => $scenes_added,
  10191. 'props_added' => $props_added,
  10192. 'global_roles_count' => count($global_roles),
  10193. 'global_scenes_count' => count($global_scenes),
  10194. 'global_props_count' => count($global_props)
  10195. ]);
  10196. }
  10197. return true;
  10198. }
  10199. /**
  10200. * 将全局角色和场景的task_id、task_status和url继承给指定分集
  10201. *
  10202. * @param int $anime_id 动漫对话ID
  10203. * @param int $episode_id 分集ID
  10204. * @return bool
  10205. */
  10206. private function inheritGlobalImagesToEpisodes($anime_id, $episode_id) {
  10207. // 获取全局角色和场景数据
  10208. $anime = DB::table('mp_animes')->where('id', $anime_id)->where('is_deleted', 0)->first();
  10209. if (!$anime) {
  10210. dLog('deepseek')->warning('动漫对话不存在', ['anime_id' => $anime_id]);
  10211. return false;
  10212. }
  10213. $global_roles = json_decode(getProp($anime, 'roles'), true) ?: [];
  10214. $global_scenes = json_decode(getProp($anime, 'scenes'), true) ?: [];
  10215. // 获取指定的分集
  10216. $episode = DB::table('mp_anime_episodes')->where('id', $episode_id)->where('anime_id', $anime_id)->first();
  10217. if (!$episode) {
  10218. dLog('deepseek')->warning('分集不存在', ['anime_id' => $anime_id, 'episode_id' => $episode_id]);
  10219. return false;
  10220. }
  10221. $episode_roles = json_decode(getProp($episode, 'roles'), true) ?: [];
  10222. $episode_scenes = json_decode(getProp($episode, 'scenes'), true) ?: [];
  10223. $roles_updated = false;
  10224. $scenes_updated = false;
  10225. // 继承角色的task_id、task_status和url
  10226. foreach ($episode_roles as &$episode_role) {
  10227. $episode_role_name = getProp($episode_role, 'role');
  10228. $episode_description = getProp($episode_role, 'description');
  10229. $episode_pic_prompt = getProp($episode_role, 'pic_prompt');
  10230. $episode_url = getProp($episode_role, 'url');
  10231. // 跳过旁白角色
  10232. if ($episode_role_name == '旁白') {
  10233. continue;
  10234. }
  10235. // 如果剧集中已有URL,跳过
  10236. if (!empty($episode_url)) {
  10237. continue;
  10238. }
  10239. // 遍历全局角色数据,查找匹配的角色
  10240. foreach ($global_roles as $global_role) {
  10241. $global_role_name = getProp($global_role, 'role');
  10242. $global_description = getProp($global_role, 'description');
  10243. $global_pic_prompt = getProp($global_role, 'pic_prompt');
  10244. $global_url = getProp($global_role, 'url');
  10245. $global_task_id = getProp($global_role, 'task_id');
  10246. $global_task_status = getProp($global_role, 'task_status');
  10247. // 满足条件:1.角色名相同 2.描述相同 3.pic_prompt相同 4.剧集中URL为空
  10248. if ($episode_role_name == $global_role_name
  10249. && $episode_description == $global_description
  10250. && $episode_pic_prompt == $global_pic_prompt) {
  10251. // 继承 task_id、task_status 和 url
  10252. if (!empty($global_task_id)) {
  10253. $episode_role['task_id'] = $global_task_id;
  10254. $episode_role['task_status'] = $global_task_status;
  10255. $roles_updated = true;
  10256. dLog('deepseek')->info('剧集角色继承全局task_id', [
  10257. 'episode_id' => $episode_id,
  10258. 'role' => $episode_role_name,
  10259. 'task_id' => $global_task_id,
  10260. 'task_status' => $global_task_status
  10261. ]);
  10262. }
  10263. if (!empty($global_url)) {
  10264. $episode_role['url'] = $global_url;
  10265. $roles_updated = true;
  10266. dLog('deepseek')->info('剧集角色继承全局url', [
  10267. 'episode_id' => $episode_id,
  10268. 'role' => $episode_role_name,
  10269. 'url' => $global_url
  10270. ]);
  10271. }
  10272. break;
  10273. }
  10274. }
  10275. }
  10276. // 继承场景的task_id、task_status和url
  10277. foreach ($episode_scenes as &$episode_scene) {
  10278. $episode_scene_name = getProp($episode_scene, 'scene');
  10279. $episode_description = getProp($episode_scene, 'description');
  10280. $episode_pic_prompt = getProp($episode_scene, 'pic_prompt');
  10281. $episode_url = getProp($episode_scene, 'url');
  10282. // 如果剧集中已有URL,跳过
  10283. if (!empty($episode_url)) {
  10284. continue;
  10285. }
  10286. // 遍历全局场景数据,查找匹配的场景
  10287. foreach ($global_scenes as $global_scene) {
  10288. $global_scene_name = getProp($global_scene, 'scene');
  10289. $global_description = getProp($global_scene, 'description');
  10290. $global_pic_prompt = getProp($global_scene, 'pic_prompt');
  10291. $global_url = getProp($global_scene, 'url');
  10292. $global_task_id = getProp($global_scene, 'task_id');
  10293. $global_task_status = getProp($global_scene, 'task_status');
  10294. // 满足条件:1.场景名相同 2.描述相同 3.pic_prompt相同 4.剧集中URL为空
  10295. if ($episode_scene_name == $global_scene_name
  10296. && $episode_description == $global_description
  10297. && $episode_pic_prompt == $global_pic_prompt) {
  10298. // 继承 task_id、task_status 和 url
  10299. if (!empty($global_task_id)) {
  10300. $episode_scene['task_id'] = $global_task_id;
  10301. $episode_scene['task_status'] = $global_task_status;
  10302. $scenes_updated = true;
  10303. dLog('deepseek')->info('剧集场景继承全局task_id', [
  10304. 'episode_id' => $episode_id,
  10305. 'scene' => $episode_scene_name,
  10306. 'task_id' => $global_task_id,
  10307. 'task_status' => $global_task_status
  10308. ]);
  10309. }
  10310. if (!empty($global_url)) {
  10311. $episode_scene['url'] = $global_url;
  10312. $scenes_updated = true;
  10313. dLog('deepseek')->info('剧集场景继承全局url', [
  10314. 'episode_id' => $episode_id,
  10315. 'scene' => $episode_scene_name,
  10316. 'url' => $global_url
  10317. ]);
  10318. }
  10319. break;
  10320. }
  10321. }
  10322. }
  10323. // 如果有更新,则保存到数据库
  10324. if ($roles_updated || $scenes_updated) {
  10325. $update_data = ['updated_at' => date('Y-m-d H:i:s')];
  10326. if ($roles_updated) {
  10327. $update_data['roles'] = json_encode($episode_roles, 256);
  10328. }
  10329. if ($scenes_updated) {
  10330. $update_data['scenes'] = json_encode($episode_scenes, 256);
  10331. }
  10332. DB::table('mp_anime_episodes')->where('id', $episode_id)->update($update_data);
  10333. dLog('deepseek')->info('剧集继承全局图片数据完成', [
  10334. 'episode_id' => $episode_id,
  10335. 'roles_updated' => $roles_updated,
  10336. 'scenes_updated' => $scenes_updated
  10337. ]);
  10338. }
  10339. return true;
  10340. }
  10341. public function chatChangeImg($data) {
  10342. $segment = getProp($data, 'segment');
  10343. $segment_content = getProp($segment, 'segment_content');
  10344. $prompt = getProp($data, 'prompt');
  10345. $ref_img = getProp($data, 'ref_img');
  10346. $question = "请修改已有的内容{$segment_content}\n修改要求如下:\n{$prompt}";
  10347. // 处理文本模型
  10348. $model = getProp($data, 'model');
  10349. if (!$model) {
  10350. // 用户没有输入,从anime表获取
  10351. $segment_id = getProp($segment, 'segment_id');
  10352. if ($segment_id) {
  10353. $episode_id = DB::table('mp_episode_segments')->where('segment_id', $segment_id)->value('episode_id');
  10354. if ($episode_id) {
  10355. $anime_id = DB::table('mp_anime_episodes')->where('id', $episode_id)->value('anime_id');
  10356. if ($anime_id) {
  10357. $model = DB::table('mp_animes')->where('id', $anime_id)->value('model');
  10358. }
  10359. }
  10360. }
  10361. if (!$model) {
  10362. // anime表也没有,使用默认值
  10363. $model = 'doubao-seed-2-0-mini-260215';
  10364. }
  10365. }
  10366. // 验证模型是否在可用模型表中
  10367. if (!DB::table('mp_text_models')->where('model', $model)->where('is_enabled', 1)->exists()) {
  10368. $model = 'doubao-seed-2-0-mini-260215';
  10369. }
  10370. $messages[] =
  10371. [
  10372. 'role' => 'user',
  10373. 'content' => $question
  10374. ];
  10375. $post_data = [
  10376. 'model' => $model,
  10377. 'messages' => $messages,
  10378. // 'max_tokens' => 8192,
  10379. 'temperature' => 0.7,
  10380. 'frequency_penalty' => 0,
  10381. 'presence_penalty' => 0,
  10382. 'response_format' => ['type' => 'text'],
  10383. 'stream' => false // 是否启用流式输出
  10384. ];
  10385. if (in_array($model, ['deepseek-reasoner', 'deepseek-chat', 'deepseek-v4-flash', 'deepseek-v4-pro'])) {
  10386. // DeepSeek 官方模型使用 DeepSeek API
  10387. $chatResult = $this->chatOnly($post_data);
  10388. } else if (in_array($model, $this->gpt_text_models)) {
  10389. // GPT-5.4 模型使用 TokenRouter API
  10390. $chatResult = $this->gpt54ChatOnly($post_data);
  10391. } else {
  10392. // 其他模型使用火山引擎API
  10393. $chatResult = $this->volcEngineChatCompletion($post_data);
  10394. }
  10395. if (is_array($chatResult)) {
  10396. extract($chatResult);
  10397. }else {
  10398. Utils::throwError('20003: 接口调用失败!');
  10399. }
  10400. return [
  10401. 'type' => 'done',
  10402. // 'episode' => $episode,
  10403. 'answer' => $fullContent,
  10404. 'reasoning' => $fullReasoningContent,
  10405. 'usage' => $usage
  10406. ];
  10407. }
  10408. // 仅调用deepseek对话接口
  10409. private function chatOnly($post_data) {
  10410. $post_data['max_tokens'] = 300000;
  10411. $client = new Client(['timeout' => 1800, 'verify' => false]);
  10412. $result = $client->post($this->url, ['json' => $post_data, 'headers' => $this->headers]);
  10413. $response = $result->getBody()->getContents();
  10414. $response_arr = json_decode($response, true);
  10415. dLog('deepseek')->info('deepseek请求完成', ['response' => $response_arr]);
  10416. $fullContent = '';
  10417. $fullReasoningContent = [];
  10418. $usage = isset($response_arr['usage']) ? $response_arr['usage'] : [];
  10419. if (isset($response_arr['choices']) && count($response_arr['choices']) > 0) {
  10420. $fullContent = isset($response_arr['choices'][0]['message']['content']) ? $response_arr['choices'][0]['message']['content'] : '';
  10421. $fullReasoningContent = isset($response_arr['choices'][0]['message']['reasoning_content']) ? $response_arr['choices'][0]['message']['reasoning_content'] : '';
  10422. }
  10423. return [
  10424. 'fullContent' => $fullContent,
  10425. 'fullReasoningContent' => $fullReasoningContent,
  10426. 'usage' => $usage
  10427. ];
  10428. }
  10429. // 仅调用 GPT-5.4 对话接口(非流式)
  10430. private function gpt54ChatOnly($post_data) {
  10431. $apiKey = env('GPT_54_API_KEY');
  10432. if (empty($apiKey)) {
  10433. Utils::throwError('20003:GPT-5.4 API Key未配置');
  10434. }
  10435. $client = new Client(['timeout' => 1800, 'verify' => false]);
  10436. $headers = [
  10437. 'Authorization' => 'Bearer ' . $apiKey,
  10438. 'Content-Type' => 'application/json'
  10439. ];
  10440. // 移除 thinking 参数,GPT-5.4 不支持
  10441. if (isset($post_data['thinking'])) {
  10442. unset($post_data['thinking']);
  10443. }
  10444. if (isset($post_data['reasoning_effort'])) {
  10445. unset($post_data['reasoning_effort']);
  10446. }
  10447. $post_data['max_completion_tokens'] = 100000;
  10448. // 确保 stream 为 false
  10449. $post_data['stream'] = false;
  10450. // 备用中转站地址: https://token.ithinkai.cn/v1/chat/completions
  10451. // 备用中转站地址2: https://api.nonelinear.com/v1/chat/completions
  10452. $result = $client->post('https://ai-api.kkidc.com/v1/chat/completions', [
  10453. 'json' => $post_data,
  10454. 'headers' => $headers
  10455. ]);
  10456. $response = $result->getBody()->getContents();
  10457. $response_arr = json_decode($response, true);
  10458. dLog('deepseek')->info('GPT-5.4请求完成', ['response' => $response_arr]);
  10459. $fullContent = '';
  10460. $usage = isset($response_arr['usage']) ? $response_arr['usage'] : [];
  10461. if (isset($response_arr['choices']) && count($response_arr['choices']) > 0) {
  10462. $fullContent = isset($response_arr['choices'][0]['message']['content']) ? $response_arr['choices'][0]['message']['content'] : '';
  10463. }
  10464. return [
  10465. 'fullContent' => $fullContent,
  10466. 'fullReasoningContent' => '', // GPT-5.4 不支持思考链
  10467. 'usage' => $usage
  10468. ];
  10469. }
  10470. private function splitContent($content) {
  10471. // 根据章节标题来拆分章节内容,返回包含标题和内容的二维数组
  10472. $chapters = [];
  10473. // 匹配章节标题格式:
  10474. // 1. 标题独占一行
  10475. // 2. 格式支持:
  10476. // - **第一集**
  10477. // - 第一集:
  10478. // - ###第1章 重生
  10479. // - ##第2章 相救
  10480. // - 第三章 共处一室
  10481. // 3. 支持阿拉伯数字(0-9)和中文数字(一二两三四五六七八九十百千万)
  10482. // 4. 支持章节关键字:章、节、集、幕、场、回、话
  10483. // 5. "第"字与数字、数字与章节关键字之间可能有空格
  10484. // 6. 前后可能有#、*、·等修饰符号,后面可能有冒号、空格、标题文字等
  10485. $pattern = '/^[#*·\s\-_=]*第\s*([一二两三四五六七八九十百千万0-9]+)\s*([章节集幕场回话]).*$/mu';
  10486. // 先用正则找出所有章节标题的位置
  10487. if (preg_match_all($pattern, $content, $matches, PREG_OFFSET_CAPTURE)) {
  10488. $titleCount = count($matches[0]);
  10489. for ($i = 0; $i < $titleCount; $i++) {
  10490. // 获取当前章节标题
  10491. $titleLine = trim($matches[0][$i][0]);
  10492. // 去除标题开头的特殊符号
  10493. $cleanTitle = preg_replace('/^[#*·\-_=\s]+/', '', $titleLine);
  10494. // 去除标题结尾的冒号
  10495. $cleanTitle = preg_replace('/[#*·::\s]+$/', '', $cleanTitle);
  10496. $cleanTitle = trim($cleanTitle);
  10497. // 获取当前标题的结束位置
  10498. $titleEndPos = $matches[0][$i][1] + strlen($matches[0][$i][0]);
  10499. // 获取下一个章节标题的开始位置(如果存在)
  10500. $nextTitlePos = ($i < $titleCount - 1) ? $matches[0][$i + 1][1] : strlen($content);
  10501. // 提取章节内容(从标题结束到下一个标题开始)
  10502. $chapterContent = substr($content, $titleEndPos, $nextTitlePos - $titleEndPos);
  10503. // 清理内容:去除前后空白和分隔线
  10504. $chapterContent = trim($chapterContent);
  10505. $chapterContent = preg_replace('/^[\-\s]+/u', '', $chapterContent);
  10506. $chapterContent = preg_replace('/[\-\s]+$/u', '', $chapterContent);
  10507. if (!empty($cleanTitle) && !empty($chapterContent)) {
  10508. $chapters[] = [
  10509. 'title' => $cleanTitle,
  10510. 'content' => $chapterContent
  10511. ];
  10512. }
  10513. }
  10514. } else {
  10515. // 如果没有匹配到章节,返回整个内容
  10516. $chapters[] = [
  10517. 'title' => '',
  10518. 'content' => $content
  10519. ];
  10520. }
  10521. return $chapters;
  10522. }
  10523. public function getContentByBid($bid) {
  10524. $chapters = DB::table('chapters as c')->select('c.id', 'c.name', 'cc.content')
  10525. ->leftJoin('chapter_contents as cc', 'c.chapter_content_id', '=', 'cc.id')
  10526. ->where('c.bid', $bid)->where('c.is_check', 1)->where('c.is_deleted', 0)->orderBy('c.sequence')->get();
  10527. $content = '';
  10528. foreach ($chapters as $chapter) {
  10529. $content .= $chapter->content . PHP_EOL . PHP_EOL;
  10530. }
  10531. return $content;
  10532. }
  10533. public function getContentByScriptId($script_id) {
  10534. $content = '';
  10535. $contents = DB::table('mp_script_episode_group')->where('script_id', $script_id)->pluck('content')->toArray();
  10536. if ($contents) {
  10537. $content = implode(PHP_EOL, $contents);
  10538. }else {
  10539. $content = DB::table('mp_scripts')->where('id', $script_id)->value('content');
  10540. }
  10541. return $content;
  10542. }
  10543. /**
  10544. * 根据文件类型提取文本内容
  10545. *
  10546. * @param mixed $file 文件对象或文件路径
  10547. * @return string
  10548. */
  10549. public function extractFileContent($file) {
  10550. // 获取文件路径和扩展名
  10551. if (is_string($file)) {
  10552. $filePath = $file;
  10553. $extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
  10554. } else {
  10555. // Laravel UploadedFile 对象
  10556. $filePath = $file->getRealPath();
  10557. $extension = strtolower($file->getClientOriginalExtension());
  10558. }
  10559. $content = '';
  10560. switch ($extension) {
  10561. case 'txt':
  10562. $content = $this->extractTxtContent($filePath);
  10563. break;
  10564. case 'pdf':
  10565. $content = $this->extractPdfContent($filePath);
  10566. break;
  10567. case 'doc':
  10568. case 'docx':
  10569. $content = $this->extractWordContent($filePath);
  10570. break;
  10571. // case 'jpg':
  10572. // case 'jpeg':
  10573. // case 'png':
  10574. // case 'gif':
  10575. // case 'bmp':
  10576. // case 'webp':
  10577. // $content = $this->extractImageContent($filePath);
  10578. // break;
  10579. default:
  10580. Utils::throwError('20003:不支持的文件格式,支持:txt、pdf');
  10581. }
  10582. return $content;
  10583. }
  10584. /**
  10585. * 提取 TXT 文件内容
  10586. */
  10587. private function extractTxtContent($filePath) {
  10588. if (!file_exists($filePath)) {
  10589. Utils::throwError('20003:文件不存在');
  10590. }
  10591. $content = file_get_contents($filePath);
  10592. // 尝试检测并转换编码
  10593. $encoding = mb_detect_encoding($content, ['UTF-8', 'GBK', 'GB2312', 'BIG5'], true);
  10594. if ($encoding && $encoding !== 'UTF-8') {
  10595. $content = mb_convert_encoding($content, 'UTF-8', $encoding);
  10596. }
  10597. return trim($content);
  10598. }
  10599. /**
  10600. * 提取 PDF 文件内容
  10601. */
  10602. private function extractPdfContent($filePath) {
  10603. if (!file_exists($filePath)) {
  10604. Utils::throwError('20003:文件不存在');
  10605. }
  10606. try {
  10607. $parser = new PdfParser();
  10608. $pdf = $parser->parseFile($filePath);
  10609. $content = $pdf->getText();
  10610. return trim($content);
  10611. } catch (\Exception $e) {
  10612. dLog('deepseek')->error('PDF解析失败: ' . $e->getMessage());
  10613. logDB('deepseek', 'error', 'PDF解析失败', ['error' => $e->getMessage()]);
  10614. Utils::throwError('20003:PDF解析失败,请确保文件格式正确');
  10615. }
  10616. }
  10617. /**
  10618. * 提取 Word 文件内容(支持 doc 和 docx)
  10619. */
  10620. private function extractWordContent($filePath) {
  10621. if (!file_exists($filePath)) {
  10622. Utils::throwError('20003:文件不存在');
  10623. }
  10624. try {
  10625. $phpWord = WordIOFactory::load($filePath);
  10626. $content = '';
  10627. foreach ($phpWord->getSections() as $section) {
  10628. foreach ($section->getElements() as $element) {
  10629. $content .= $this->extractWordElementText($element);
  10630. }
  10631. }
  10632. return trim($content);
  10633. } catch (\Exception $e) {
  10634. dLog('deepseek')->error('Word解析失败: ' . $e->getMessage());
  10635. logDB('deepseek', 'error', 'Word解析失败', ['error' => $e->getMessage()]);
  10636. Utils::throwError('20003:Word解析失败,请确保文件格式正确');
  10637. }
  10638. }
  10639. /**
  10640. * 递归提取 Word 元素中的文本
  10641. */
  10642. private function extractWordElementText($element) {
  10643. $text = '';
  10644. if (method_exists($element, 'getText')) {
  10645. $text .= $element->getText() . "\n";
  10646. } elseif (method_exists($element, 'getElements')) {
  10647. foreach ($element->getElements() as $childElement) {
  10648. $text .= $this->extractWordElementText($childElement);
  10649. }
  10650. }
  10651. return $text;
  10652. }
  10653. /**
  10654. * 提取图片内容(使用火山引擎 OCR)
  10655. */
  10656. private function extractImageContent($filePath) {
  10657. if (!file_exists($filePath)) {
  10658. Utils::throwError('20003:文件不存在');
  10659. }
  10660. try {
  10661. // 读取图片并转换为 base64
  10662. $imageData = file_get_contents($filePath);
  10663. $base64Image = base64_encode($imageData);
  10664. // 调用火山引擎 OCR 服务
  10665. $content = $this->callVolcEngineOCR($base64Image);
  10666. return trim($content);
  10667. } catch (\Exception $e) {
  10668. dLog('deepseek')->error('图片OCR识别失败: ' . $e->getMessage());
  10669. logDB('deepseek', 'error', '图片OCR识别失败', ['error' => $e->getMessage()]);
  10670. Utils::throwError('20003:图片识别失败');
  10671. }
  10672. }
  10673. /**
  10674. * 调用火山引擎 OCR 服务识别图片文字
  10675. *
  10676. * @param string $base64Image base64 编码的图片数据
  10677. * @return string 识别的文字内容
  10678. */
  10679. private function callVolcEngineOCR($base64Image) {
  10680. $method = 'POST';
  10681. $service = 'cv';
  10682. $host = 'visual.volcengineapi.com';
  10683. $region = env('VOLC_REGION', 'cn-north-1');
  10684. $access_key = env('VOLC_AK');
  10685. $secret_key = env('VOLC_SK');
  10686. $action = 'OCRNormal';
  10687. $version = '2020-08-26';
  10688. if (!$access_key || !$secret_key) {
  10689. Utils::throwError('20003:请配置火山引擎 AK/SK');
  10690. }
  10691. // 请求体
  10692. $body = json_encode([
  10693. 'image_base64' => $base64Image
  10694. ]);
  10695. // 生成签名
  10696. $headers = $this->getVolcEngineSignHeaders(
  10697. $method, $service, $host, $region,
  10698. "Action={$action}&Version={$version}",
  10699. $access_key, $secret_key, $body
  10700. );
  10701. $client = new Client(['timeout' => 1800, 'verify' => false]);
  10702. $response = $client->post("https://{$host}/?Action={$action}&Version={$version}", [
  10703. 'headers' => $headers,
  10704. 'body' => $body
  10705. ]);
  10706. $response_arr = json_decode($response->getBody()->getContents(), true);
  10707. // 提取识别的文字
  10708. $textLines = [];
  10709. if (isset($response_arr['data']['line_texts'])) {
  10710. $textLines = $response_arr['data']['line_texts'];
  10711. } elseif (isset($response_arr['data']['ocr_infos'])) {
  10712. foreach ($response_arr['data']['ocr_infos'] as $info) {
  10713. if (isset($info['text'])) {
  10714. $textLines[] = $info['text'];
  10715. }
  10716. }
  10717. }
  10718. if (empty($textLines)) {
  10719. dLog('deepseek')->warning('OCR识别结果为空', ['response' => $response_arr]);
  10720. return '';
  10721. }
  10722. return implode("\n", $textLines);
  10723. }
  10724. /**
  10725. * 生成火山引擎 API 签名头(用于 OCR 等 POST 请求)
  10726. */
  10727. private function getVolcEngineSignHeaders($method, $service, $host, $region, $queryString, $access_key, $secret_key, $body = '') {
  10728. $contentType = 'application/json';
  10729. $accept = 'application/json';
  10730. // 获取当前UTC时间
  10731. $t = new DateTime('now', new DateTimeZone('UTC'));
  10732. $xDate = $t->format('Ymd\THis\Z');
  10733. $dateStamp = $t->format('Ymd');
  10734. // 计算 body 的 sha256 哈希
  10735. $payloadHash = hash('sha256', $body);
  10736. // 1. 拼接规范请求串
  10737. $canonicalUri = '/';
  10738. $canonicalQueryString = $queryString;
  10739. $canonicalHeaders = "content-type:{$contentType}\nhost:{$host}\nx-content-sha256:{$payloadHash}\nx-date:{$xDate}\n";
  10740. $signedHeaders = 'content-type;host;x-content-sha256;x-date';
  10741. $canonicalRequest = implode("\n", [
  10742. $method,
  10743. $canonicalUri,
  10744. $canonicalQueryString,
  10745. $canonicalHeaders,
  10746. $signedHeaders,
  10747. $payloadHash
  10748. ]);
  10749. // 2. 拼接待签名字符串
  10750. $algorithm = 'HMAC-SHA256';
  10751. $credentialScope = implode('/', [$dateStamp, $region, $service, 'request']);
  10752. $hashedCanonicalRequest = hash('sha256', $canonicalRequest);
  10753. $stringToSign = implode("\n", [
  10754. $algorithm,
  10755. $xDate,
  10756. $credentialScope,
  10757. $hashedCanonicalRequest
  10758. ]);
  10759. // 3. 计算签名
  10760. $signingKey = $this->getSignatureKey($secret_key, $dateStamp, $region, $service);
  10761. $signature = hash_hmac('sha256', $stringToSign, $signingKey);
  10762. // 4. 添加签名到请求头
  10763. $authorizationHeader = sprintf(
  10764. '%s Credential=%s/%s, SignedHeaders=%s, Signature=%s',
  10765. $algorithm,
  10766. $access_key,
  10767. $credentialScope,
  10768. $signedHeaders,
  10769. $signature
  10770. );
  10771. return [
  10772. 'Accept' => $accept,
  10773. 'Content-Type' => $contentType,
  10774. 'Host' => $host,
  10775. 'X-Date' => $xDate,
  10776. 'X-Content-Sha256'=> $payloadHash,
  10777. 'Authorization' => $authorizationHeader
  10778. ];
  10779. }
  10780. public function generateScriptWords($cid, $model = 'r1') {
  10781. ini_set('max_execution_time', 0);
  10782. if (!$cid) Utils::throwError('20003: 请选择章节!');
  10783. $content = DB::table('chapters as c')->leftJoin('chapter_contents as cc', 'c.chapter_content_id', '=', 'cc.id')->where('c.id', $cid)->value('cc.content');
  10784. // 模型兼容性处理和思考模式设置
  10785. $thinkingMode = 'disabled';
  10786. $reasoningEffort = 'high';
  10787. if ($model == 'r1') {
  10788. $model = 'deepseek-v4-flash'; // r1 映射到 deepseek-v4-flash 思考模式
  10789. $thinkingMode = 'enabled';
  10790. } else {
  10791. $model = 'deepseek-v4-flash'; // 默认映射到 deepseek-v4-flash 非思考模式
  10792. $thinkingMode = 'disabled';
  10793. }
  10794. $messages = [
  10795. [
  10796. 'role' => 'system',
  10797. 'content' => '下面有一段小说文本,请帮我将文本中的每句话按从上到下的顺序拆分成角色不同的剧本文稿(不得更改上下文顺序和内容),文稿形式严格按照“角色名(男或女):台词{情感}”输出,需要注意以下几点要求:
  10798. 1.角色名后不要加入任何其他词语,只能加不包括旁白的性别,在男或女中选
  10799. 2.非对话部分请全部用旁白角色代替
  10800. 3.情感必须在【开心、悲伤、生气、惊讶、恐惧、厌恶、激动、冷漠、中性、沮丧、撒娇、害羞、安慰鼓励、咆哮、温柔、自然讲述、情感电台、磁性、广告营销、气泡音、新闻播报、娱乐八卦】中选一个,不得使用其他词语'
  10801. ],
  10802. [
  10803. 'role' => 'user',
  10804. 'content' => $content
  10805. ]
  10806. ];
  10807. $post_data = [
  10808. 'model' => $model,
  10809. 'messages' => $messages,
  10810. // 'max_tokens' => 8192,
  10811. 'temperature' => 1,
  10812. 'frequency_penalty' => 0,
  10813. 'presence_penalty' => 0,
  10814. 'thinking' => ['type' => $thinkingMode],
  10815. 'response_format' => [
  10816. 'type' => 'text'
  10817. ],
  10818. 'stream' => false
  10819. ];
  10820. if ($thinkingMode == 'enabled') $post_data['reasoning_effort'] = $reasoningEffort;
  10821. // 根据模型类型选择调用方法
  10822. $fullContent = '';
  10823. if (in_array($model, ['deepseek-reasoner', 'deepseek-chat', 'deepseek-v4-flash', 'deepseek-v4-pro'])) {
  10824. // DeepSeek 官方模型使用 DeepSeek API
  10825. $chatResult = $this->chatOnly($post_data);
  10826. } else if (in_array($model, $this->gpt_text_models)) {
  10827. // GPT-5.4 模型使用 TokenRouter API
  10828. $chatResult = $this->gpt54ChatOnly($post_data);
  10829. } else {
  10830. // 其他模型使用火山引擎API
  10831. $chatResult = $this->volcEngineChatCompletion($post_data);
  10832. }
  10833. if (is_array($chatResult)) {
  10834. $fullContent = $chatResult['fullContent'];
  10835. }
  10836. return $fullContent;
  10837. }
  10838. /**
  10839. * 智能化解析集数信息
  10840. * 支持多种表达方式,如:改成3集、调整成5集、修改为10集等
  10841. *
  10842. * @param string $prompt 用户输入的提示词
  10843. * @return int|null 解析出的集数,如果没有找到则返回null
  10844. */
  10845. private function extractEpisodeNumber($prompt)
  10846. {
  10847. if (empty($prompt)) {
  10848. return null;
  10849. }
  10850. // 定义各种可能的表达模式
  10851. $patterns = [
  10852. // 基本模式:改成N集、调整成N集、修改成N集
  10853. '/(?:改成|调整成|修改成|变成|设置成|设为|改为|调为|修改为|变为)\s*(\d+)\s*集/u',
  10854. // 扩展模式:分成N集、拆分成N集、分割成N集
  10855. '/(?:分成|拆分成|分割成|划分成|分为)\s*(\d+)\s*集/u',
  10856. // 数量模式:N集、共N集、总共N集
  10857. '/(?:共|总共|一共|总计)?\s*(\d+)\s*集/u',
  10858. // 制作模式:制作N集、生成N集、创建N集
  10859. '/(?:制作|生成|创建|产出|输出)\s*(\d+)\s*集/u',
  10860. // 计划模式:计划N集、预计N集、打算N集
  10861. '/(?:计划|预计|打算|准备|想要)\s*(\d+)\s*集/u',
  10862. // 需要模式:需要N集、要N集
  10863. '/(?:需要|要|需)\s*(\d+)\s*集/u',
  10864. // 中文数字模式:改成三集、调整成五集等
  10865. '/(?:改成|调整成|修改成|变成|设置成|设为|改为|调为|修改为|变为|分成|拆分成|分割成|划分成|分为|制作|生成|创建|产出|输出|计划|预计|打算|准备|想要|需要|要|需)\s*([一二三四五六七八九十]+)\s*集/u',
  10866. // 英文数字模式
  10867. '/(?:change\s+to|adjust\s+to|modify\s+to|set\s+to|make\s+it|split\s+into|divide\s+into)\s*(\d+)\s*(?:episodes?|eps?)/i',
  10868. ];
  10869. // 中文数字转阿拉伯数字的映射
  10870. $chineseNumbers = [
  10871. '一' => 1, '二' => 2, '三' => 3, '四' => 4, '五' => 5,
  10872. '六' => 6, '七' => 7, '八' => 8, '九' => 9, '十' => 10,
  10873. '十一' => 11, '十二' => 12, '十三' => 13, '十四' => 14, '十五' => 15,
  10874. '十六' => 16, '十七' => 17, '十八' => 18, '十九' => 19, '二十' => 20
  10875. ];
  10876. // 逐个尝试匹配模式
  10877. foreach ($patterns as $pattern) {
  10878. if (preg_match($pattern, $prompt, $matches)) {
  10879. $number = trim($matches[1]);
  10880. // 如果是中文数字,转换为阿拉伯数字
  10881. if (isset($chineseNumbers[$number])) {
  10882. return $chineseNumbers[$number];
  10883. }
  10884. // 如果是阿拉伯数字,直接返回
  10885. if (is_numeric($number)) {
  10886. $episodeNum = intval($number);
  10887. // 合理性检查:集数应该在1-100之间
  10888. if ($episodeNum >= 1 && $episodeNum <= 100) {
  10889. return $episodeNum;
  10890. }
  10891. }
  10892. }
  10893. }
  10894. return null;
  10895. }
  10896. /**
  10897. * 调用火山引擎对话(Chat) API
  10898. *
  10899. * @param array $params 包含以下参数:
  10900. * - model: 模型ID(必填)
  10901. * - messages: 消息列表(必填)
  10902. * - stream: 是否流式输出(可选,默认false)
  10903. * - max_tokens: 最大输出token数(可选)
  10904. * - temperature: 采样温度(可选,默认1)
  10905. * - top_p: 核采样概率(可选,默认0.7)
  10906. * - frequency_penalty: 频率惩罚系数(可选,默认0)
  10907. * - presence_penalty: 存在惩罚系数(可选,默认0)
  10908. * - stop: 停止词(可选)
  10909. * - tools: 工具列表(可选)
  10910. * @return array|Generator 非流式返回数组,流式返回生成器
  10911. */
  10912. public function volcEngineChatCompletion(array $params)
  10913. {
  10914. $apiKey = env('VOLC_AI_API_KEY');
  10915. if (empty($apiKey)) {
  10916. Utils::throwError('20003:火山引擎API Key未配置,请检查环境变量VOLC_AI_API_KEY');
  10917. }
  10918. // 构建请求参数
  10919. $originalModel = $params['model'] ?? 'deepseek-v3-2-251201';
  10920. $requestData = [
  10921. 'model' => isset(\App\Consts\BaseConst::MODELS_MAP[$originalModel]) ? \App\Consts\BaseConst::MODELS_MAP[$originalModel] : $originalModel,
  10922. 'messages' => $params['messages'] ?? [],
  10923. ];
  10924. if (!in_array($originalModel, $this->valid_text_models)) {
  10925. Utils::throwError('20003:该模型不支持!');
  10926. }
  10927. // 添加可选参数
  10928. if (isset($params['stream'])) {
  10929. $requestData['stream'] = $params['stream'];
  10930. }
  10931. if (isset($params['stream_options'])) {
  10932. $requestData['stream_options'] = $params['stream_options'];
  10933. }
  10934. if (isset($params['max_tokens'])) {
  10935. $requestData['max_tokens'] = $params['max_tokens'];
  10936. }else if (isset($params['max_completion_tokens'])) {
  10937. $requestData['max_completion_tokens'] = $params['max_completion_tokens'];
  10938. }else {
  10939. $requestData['max_completion_tokens'] = 100000;
  10940. }
  10941. if (isset($params['temperature'])) {
  10942. $requestData['temperature'] = $params['temperature'];
  10943. }
  10944. if (isset($params['top_p'])) {
  10945. $requestData['top_p'] = $params['top_p'];
  10946. }
  10947. if (isset($params['frequency_penalty'])) {
  10948. $requestData['frequency_penalty'] = $params['frequency_penalty'];
  10949. }
  10950. if (isset($params['presence_penalty'])) {
  10951. $requestData['presence_penalty'] = $params['presence_penalty'];
  10952. }
  10953. if (isset($params['stop'])) {
  10954. $requestData['stop'] = $params['stop'];
  10955. }
  10956. if (isset($params['tools'])) {
  10957. $requestData['tools'] = $params['tools'];
  10958. }
  10959. if (isset($params['tool_choice'])) {
  10960. $requestData['tool_choice'] = $params['tool_choice'];
  10961. }
  10962. if (isset($params['response_format'])) {
  10963. $requestData['response_format'] = $params['response_format'];
  10964. }
  10965. if (isset($params['thinking'])) {
  10966. // 如果 thinking 已经是数组格式,直接使用;否则转换为正确格式
  10967. if (is_array($params['thinking'])) {
  10968. $requestData['thinking'] = $params['thinking'];
  10969. if (isset($params['reasoning_effort']) && $params['thinking']['type'] == 'enabled') {
  10970. $requestData['reasoning_effort'] = $params['reasoning_effort'];
  10971. }
  10972. } else {
  10973. $requestData['thinking'] = ['type' => $params['thinking']];
  10974. if (isset($params['reasoning_effort']) && $params['thinking'] == 'enabled') {
  10975. $requestData['reasoning_effort'] = $params['reasoning_effort'];
  10976. }
  10977. }
  10978. } else {
  10979. $requestData['thinking'] = ['type' => 'disabled'];
  10980. }
  10981. dLog('deepseek')->info('火山引擎对话API请求参数: ', $requestData);
  10982. $client = new Client(['verify' => false, 'timeout' => 1800]);
  10983. try {
  10984. // 判断是否为流式输出
  10985. $isStream = isset($params['stream']) && $params['stream'] === true;
  10986. if ($isStream) {
  10987. // 流式输出
  10988. return $this->volcEngineChatCompletionStream($client, $apiKey, $requestData);
  10989. } else {
  10990. // 非流式输出
  10991. $response = $client->post('https://ark.cn-beijing.volces.com/api/v3/chat/completions', [
  10992. 'headers' => [
  10993. 'Authorization' => 'Bearer ' . $apiKey,
  10994. 'Content-Type' => 'application/json',
  10995. ],
  10996. 'json' => $requestData
  10997. ]);
  10998. $responseData = json_decode($response->getBody(), true);
  10999. dLog('deepseek')->info('火山引擎对话API响应: ', $responseData);
  11000. $usage = isset($responseData['usage']) ? $responseData['usage'] : [];
  11001. if (isset($responseData['choices']) && count($responseData['choices']) > 0) {
  11002. $fullContent = isset($responseData['choices'][0]['message']['content']) ? $responseData['choices'][0]['message']['content'] : '';
  11003. $fullReasoningContent = isset($responseData['choices'][0]['message']['reasoning_content']) ? $responseData['choices'][0]['message']['reasoning_content'] : '';
  11004. }
  11005. return [
  11006. 'fullContent' => $fullContent,
  11007. 'fullReasoningContent' => $fullReasoningContent,
  11008. 'usage' => $usage
  11009. ];
  11010. }
  11011. } catch (\Exception $e) {
  11012. dLog('deepseek')->error('火山引擎对话API请求异常: ' . $e->getMessage());
  11013. logDB('deepseek', 'error', '火山引擎对话API请求失败', ['error' => $e->getMessage(), 'model' => $requestData['model']]);
  11014. Utils::throwError('20003:火山引擎对话API请求失败: ' . $e->getMessage());
  11015. }
  11016. }
  11017. /**
  11018. * 火山引擎对话API流式输出
  11019. *
  11020. * @param Client $client HTTP客户端
  11021. * @param string $apiKey API密钥
  11022. * @param array $requestData 请求数据
  11023. * @return Generator
  11024. */
  11025. private function volcEngineChatCompletionStream($client, $apiKey, $requestData)
  11026. {
  11027. try {
  11028. $response = $client->post('https://ark.cn-beijing.volces.com/api/v3/chat/completions', [
  11029. 'headers' => [
  11030. 'Authorization' => 'Bearer ' . $apiKey,
  11031. 'Content-Type' => 'application/json',
  11032. ],
  11033. 'json' => $requestData,
  11034. 'stream' => true
  11035. ]);
  11036. $body = $response->getBody();
  11037. $buffer = '';
  11038. $fullContent = '';
  11039. $fullReasoningContent = '';
  11040. $usage = [];
  11041. dLog('deepseek')->info('开始读取火山引擎对话API流式响应');
  11042. // 逐行读取流式响应
  11043. while (!$body->eof()) {
  11044. $chunk = $body->read(1024);
  11045. $buffer .= $chunk;
  11046. // 按行分割
  11047. $lines = explode("\n", $buffer);
  11048. $buffer = array_pop($lines);
  11049. foreach ($lines as $line) {
  11050. $line = trim($line);
  11051. if (empty($line)) {
  11052. continue;
  11053. }
  11054. // 检查是否是SSE数据行
  11055. if (strpos($line, 'data: ') !== 0) {
  11056. continue;
  11057. }
  11058. $data = substr($line, 6);
  11059. if ($data === '[DONE]') {
  11060. dLog('deepseek')->info('收到结束标记');
  11061. break 2;
  11062. }
  11063. try {
  11064. $json = json_decode($data, true);
  11065. if (json_last_error() !== JSON_ERROR_NONE) {
  11066. dLog('deepseek')->warning('JSON解析错误: ' . json_last_error_msg());
  11067. continue;
  11068. }
  11069. if (isset($json['choices'][0]['delta'])) {
  11070. $delta = $json['choices'][0]['delta'];
  11071. // 处理思维链内容
  11072. if (isset($delta['reasoning_content'])) {
  11073. $fullReasoningContent .= $delta['reasoning_content'];
  11074. yield [
  11075. 'type' => 'reasoning',
  11076. 'content' => $delta['reasoning_content'],
  11077. 'full_reasoning' => $fullReasoningContent
  11078. ];
  11079. }
  11080. // 处理回答内容
  11081. if (isset($delta['content'])) {
  11082. $fullContent .= $delta['content'];
  11083. yield [
  11084. 'type' => 'content',
  11085. 'content' => $delta['content'],
  11086. ];
  11087. }
  11088. }
  11089. // 获取使用统计信息
  11090. if (isset($json['usage'])) {
  11091. $usage = $json['usage'];
  11092. dLog('deepseek')->info('收到使用统计', ['usage' => $usage]);
  11093. }
  11094. } catch (\Exception $e) {
  11095. dLog('deepseek')->error('解析流式响应失败: ' . $e->getMessage());
  11096. continue;
  11097. }
  11098. }
  11099. }
  11100. dLog('deepseek')->info('流式读取完成', [
  11101. 'content_length' => strlen($fullContent),
  11102. 'reasoning_length' => strlen($fullReasoningContent)
  11103. ]);
  11104. yield [
  11105. 'type' => 'done',
  11106. 'full_content' => $fullContent,
  11107. 'full_reasoning' => $fullReasoningContent,
  11108. 'usage' => $usage
  11109. ];
  11110. } catch (\Exception $e) {
  11111. dLog('deepseek')->error('火山引擎对话API流式请求异常: ' . $e->getMessage());
  11112. logDB('deepseek', 'error', '火山引擎对话API流式请求失败', ['error' => $e->getMessage()]);
  11113. throw $e;
  11114. }
  11115. }
  11116. /**
  11117. * 检查并创建图片生成任务
  11118. *
  11119. * @param array $merged_items 合并后的资源列表(roles 或 scenes)
  11120. * @param array $global_items 全局资源列表(来自 mp_animes 表)
  11121. * @param string $nameKey 资源名称字段('role' 或 'scene')
  11122. * @param string $art_style 美术风格
  11123. * @return array 更新后的资源列表
  11124. */
  11125. private function checkAndCreateImageTasks(array $merged_items, array $global_items, string $nameKey, string $art_style = ''): array
  11126. {
  11127. // 确保参数为数组
  11128. $merged_items = is_array($merged_items) ? $merged_items : [];
  11129. $global_items = is_array($global_items) ? $global_items : [];
  11130. // 构建全局资源映射表,用于快速查找
  11131. $global_map = [];
  11132. foreach ($global_items as $item) {
  11133. $itemKey = $this->normalizeEpisodeResourceKey(getProp($item, $nameKey));
  11134. if ($itemKey === '') {
  11135. continue;
  11136. }
  11137. $global_map[$itemKey] = $item;
  11138. }
  11139. // 遍历合并后的资源列表,检查是否需要创建图片生成任务
  11140. foreach ($merged_items as &$item) {
  11141. $item_name = getProp($item, $nameKey);
  11142. $item_description = getProp($item, 'description');
  11143. // $item_url = getProp($item, 'url');
  11144. // // 如果已有图片URL,跳过
  11145. // if (!empty($item_url)) {
  11146. // continue;
  11147. // }
  11148. // 如果是旁白角色,跳过
  11149. if ($nameKey === 'role' && $item_name === '旁白') {
  11150. continue;
  11151. }
  11152. $itemKey = $this->normalizeEpisodeResourceKey($item_name);
  11153. if ($itemKey === '') {
  11154. continue;
  11155. }
  11156. $need_create_task = false;
  11157. // 条件1:在全局资源中找不到匹配的名称
  11158. if (!isset($global_map[$itemKey])) {
  11159. $need_create_task = true;
  11160. } else {
  11161. // 条件2:名称匹配但描述或 pic_prompt 不一致
  11162. $global_description = trim((string)getProp($global_map[$itemKey], 'description'));
  11163. $current_description = trim((string)$item_description);
  11164. $global_pic_prompt = trim((string)getProp($global_map[$itemKey], 'pic_prompt'));
  11165. $current_pic_prompt = trim((string)getProp($item, 'pic_prompt'));
  11166. // 如果 description 或 pic_prompt 有变化,需要重新生成
  11167. if ($global_description !== $current_description || $global_pic_prompt !== $current_pic_prompt) {
  11168. $need_create_task = true;
  11169. }
  11170. }
  11171. // 如果需要创建任务,调用图片生成服务
  11172. if ($need_create_task) {
  11173. try {
  11174. // 优先使用 pic_prompt,如果没有则使用 description
  11175. $pic_prompt = getProp($item, 'pic_prompt');
  11176. $description = !empty($pic_prompt) ? trim((string)$pic_prompt) : $item_description;
  11177. // 添加美术风格前缀
  11178. if ($art_style) {
  11179. $prefix = $nameKey === 'role' ? '主体描述' : '场景描述';
  11180. // $description = "美术风格:\n{$art_style}\n\n{$prefix}:{$description}";
  11181. $description = "{$prefix}:{$description}";
  11182. }
  11183. $params = [
  11184. 'prompt' => $description,
  11185. 'ref_img_urls' => []
  11186. ];
  11187. $task = $this->aiImageGenerationService->createImageGenerationTask($params);
  11188. $task_id = $task->id;
  11189. if ($task_id) {
  11190. $item['task_id'] = $task_id;
  11191. $item['task_status'] = 'processing';
  11192. $resource_type = $nameKey === 'role' ? '角色' : '场景';
  11193. dLog('deepseek')->info("{$resource_type}({$item_name})创建图片生成任务成功", ['task_id' => $task_id]);
  11194. }
  11195. } catch (\Exception $e) {
  11196. $resource_type = $nameKey === 'role' ? '角色' : '场景';
  11197. dLog('deepseek')->error("{$resource_type}({$item_name})创建图片生成任务失败: " . $e->getMessage());
  11198. logDB('deepseek', 'error', "{$resource_type}创建图片生成任务失败", ['name' => $item_name, 'error' => $e->getMessage()]);
  11199. // 不抛出异常,继续处理其他资源
  11200. }
  11201. }
  11202. }
  11203. return $merged_items;
  11204. }
  11205. /**
  11206. * 同步新出现的主体和场景到全局
  11207. *
  11208. * @param int $anime_id 动漫ID
  11209. * @param array $merged_roles 合并后的角色列表
  11210. * @param array $merged_scenes 合并后的场景列表
  11211. * @param array $global_roles 全局角色列表
  11212. * @param array $global_scenes 全局场景列表
  11213. * @return void
  11214. */
  11215. private function syncNewResourcesToGlobal(
  11216. int $anime_id,
  11217. array $merged_roles,
  11218. array $merged_scenes,
  11219. array $global_roles,
  11220. array $global_scenes
  11221. ): void {
  11222. // 构建全局角色映射表
  11223. $global_role_map = [];
  11224. foreach ($global_roles as $role) {
  11225. $roleKey = $this->normalizeEpisodeResourceKey(getProp($role, 'role'));
  11226. if ($roleKey !== '') {
  11227. $global_role_map[$roleKey] = $role;
  11228. }
  11229. }
  11230. // 构建全局场景映射表
  11231. $global_scene_map = [];
  11232. foreach ($global_scenes as $scene) {
  11233. $sceneKey = $this->normalizeEpisodeResourceKey(getProp($scene, 'scene'));
  11234. if ($sceneKey !== '') {
  11235. $global_scene_map[$sceneKey] = $scene;
  11236. }
  11237. }
  11238. $need_update = false;
  11239. $new_global_roles = $global_roles;
  11240. $new_global_scenes = $global_scenes;
  11241. // 检查并添加新角色到全局
  11242. foreach ($merged_roles as $role) {
  11243. $role_name = getProp($role, 'role');
  11244. // 跳过旁白
  11245. if ($role_name === '旁白') {
  11246. continue;
  11247. }
  11248. $roleKey = $this->normalizeEpisodeResourceKey($role_name);
  11249. if ($roleKey === '') {
  11250. continue;
  11251. }
  11252. // 如果全局中不存在该角色,添加到全局
  11253. if (!isset($global_role_map[$roleKey])) {
  11254. $new_global_roles[] = [
  11255. 'role' => $role_name,
  11256. 'description' => getProp($role, 'description', ''),
  11257. 'pic_prompt' => getProp($role, 'pic_prompt', ''),
  11258. 'voice_name' => getProp($role, 'voice_name', ''),
  11259. 'voice_type' => getProp($role, 'voice_type', ''),
  11260. 'voice_audio_url' => getProp($role, 'voice_audio_url', ''),
  11261. 'url' => getProp($role, 'url', ''),
  11262. 'task_id' => getProp($role, 'task_id', ''),
  11263. 'task_status' => getProp($role, 'task_status', ''),
  11264. ];
  11265. $global_role_map[$roleKey] = true; // 标记已添加
  11266. $need_update = true;
  11267. dLog('deepseek')->info("新角色同步到全局", [
  11268. 'anime_id' => $anime_id,
  11269. 'role' => $role_name,
  11270. 'task_id' => getProp($role, 'task_id', ''),
  11271. ]);
  11272. }
  11273. // else {
  11274. // // 如果角色已存在,但描述、pic_prompt或图片信息有更新,则更新全局数据
  11275. // $global_role = $global_role_map[$roleKey];
  11276. // if (is_array($global_role)) {
  11277. // $updated = false;
  11278. // $update_fields = [];
  11279. // // 检查描述是否更新
  11280. // $current_description = trim((string)getProp($role, 'description'));
  11281. // $global_description = trim((string)getProp($global_role, 'description'));
  11282. // if ($current_description !== '' && $current_description !== $global_description) {
  11283. // $updated = true;
  11284. // $update_fields[] = 'description';
  11285. // }
  11286. // // 检查pic_prompt是否更新
  11287. // $current_pic_prompt = trim((string)getProp($role, 'pic_prompt'));
  11288. // $global_pic_prompt = trim((string)getProp($global_role, 'pic_prompt'));
  11289. // if ($current_pic_prompt !== '' && $current_pic_prompt !== $global_pic_prompt) {
  11290. // $updated = true;
  11291. // $update_fields[] = 'pic_prompt';
  11292. // }
  11293. // // 检查图片任务信息是否更新
  11294. // $current_task_id = getProp($role, 'task_id');
  11295. // $global_task_id = getProp($global_role, 'task_id');
  11296. // if ($current_task_id && $current_task_id !== $global_task_id) {
  11297. // $updated = true;
  11298. // $update_fields[] = 'task_id';
  11299. // }
  11300. // // 如果有更新,同步到全局
  11301. // if ($updated) {
  11302. // foreach ($new_global_roles as &$global_role_item) {
  11303. // $global_role_key = $this->normalizeEpisodeResourceKey(getProp($global_role_item, 'role'));
  11304. // if ($global_role_key === $roleKey) {
  11305. // if (in_array('description', $update_fields)) {
  11306. // $global_role_item['description'] = $current_description;
  11307. // }
  11308. // if (in_array('pic_prompt', $update_fields)) {
  11309. // $global_role_item['pic_prompt'] = $current_pic_prompt;
  11310. // }
  11311. // if (in_array('task_id', $update_fields)) {
  11312. // $global_role_item['task_id'] = $current_task_id;
  11313. // $global_role_item['task_status'] = getProp($role, 'task_status', '');
  11314. // $global_role_item['url'] = getProp($role, 'url', '');
  11315. // }
  11316. // break;
  11317. // }
  11318. // }
  11319. // $need_update = true;
  11320. // dLog('deepseek')->info("角色信息更新到全局", [
  11321. // 'anime_id' => $anime_id,
  11322. // 'role' => $role_name,
  11323. // 'update_fields' => implode(',', $update_fields),
  11324. // ]);
  11325. // }
  11326. // }
  11327. // }
  11328. }
  11329. // 检查并添加新场景到全局
  11330. foreach ($merged_scenes as $scene) {
  11331. $scene_name = getProp($scene, 'scene');
  11332. $sceneKey = $this->normalizeEpisodeResourceKey($scene_name);
  11333. if ($sceneKey === '') {
  11334. continue;
  11335. }
  11336. // 如果全局中不存在该场景,添加到全局
  11337. if (!isset($global_scene_map[$sceneKey])) {
  11338. $new_global_scenes[] = [
  11339. 'scene' => $scene_name,
  11340. 'description' => getProp($scene, 'description', ''),
  11341. 'pic_prompt' => getProp($scene, 'pic_prompt', ''),
  11342. 'url' => getProp($scene, 'url', ''),
  11343. 'task_id' => getProp($scene, 'task_id', ''),
  11344. 'task_status' => getProp($scene, 'task_status', ''),
  11345. ];
  11346. $global_scene_map[$sceneKey] = true; // 标记已添加
  11347. $need_update = true;
  11348. dLog('deepseek')->info("新场景同步到全局", [
  11349. 'anime_id' => $anime_id,
  11350. 'scene' => $scene_name,
  11351. 'task_id' => getProp($scene, 'task_id', ''),
  11352. ]);
  11353. }
  11354. // else {
  11355. // // 如果场景已存在,但描述、pic_prompt或图片信息有更新,则更新全局数据
  11356. // $global_scene = $global_scene_map[$sceneKey];
  11357. // if (is_array($global_scene)) {
  11358. // $updated = false;
  11359. // $update_fields = [];
  11360. // // 检查描述是否更新
  11361. // $current_description = trim((string)getProp($scene, 'description'));
  11362. // $global_description = trim((string)getProp($global_scene, 'description'));
  11363. // if ($current_description !== '' && $current_description !== $global_description) {
  11364. // $updated = true;
  11365. // $update_fields[] = 'description';
  11366. // }
  11367. // // 检查pic_prompt是否更新
  11368. // $current_pic_prompt = trim((string)getProp($scene, 'pic_prompt'));
  11369. // $global_pic_prompt = trim((string)getProp($global_scene, 'pic_prompt'));
  11370. // if ($current_pic_prompt !== '' && $current_pic_prompt !== $global_pic_prompt) {
  11371. // $updated = true;
  11372. // $update_fields[] = 'pic_prompt';
  11373. // }
  11374. // // 检查图片任务信息是否更新
  11375. // $current_task_id = getProp($scene, 'task_id');
  11376. // $global_task_id = getProp($global_scene, 'task_id');
  11377. // if ($current_task_id && $current_task_id !== $global_task_id) {
  11378. // $updated = true;
  11379. // $update_fields[] = 'task_id';
  11380. // }
  11381. // // 如果有更新,同步到全局
  11382. // if ($updated) {
  11383. // foreach ($new_global_scenes as &$global_scene_item) {
  11384. // $global_scene_key = $this->normalizeEpisodeResourceKey(getProp($global_scene_item, 'scene'));
  11385. // if ($global_scene_key === $sceneKey) {
  11386. // if (in_array('description', $update_fields)) {
  11387. // $global_scene_item['description'] = $current_description;
  11388. // }
  11389. // if (in_array('pic_prompt', $update_fields)) {
  11390. // $global_scene_item['pic_prompt'] = $current_pic_prompt;
  11391. // }
  11392. // if (in_array('task_id', $update_fields)) {
  11393. // $global_scene_item['task_id'] = $current_task_id;
  11394. // $global_scene_item['task_status'] = getProp($scene, 'task_status', '');
  11395. // $global_scene_item['url'] = getProp($scene, 'url', '');
  11396. // }
  11397. // break;
  11398. // }
  11399. // }
  11400. // $need_update = true;
  11401. // dLog('deepseek')->info("场景信息更新到全局", [
  11402. // 'anime_id' => $anime_id,
  11403. // 'scene' => $scene_name,
  11404. // 'update_fields' => implode(',', $update_fields),
  11405. // ]);
  11406. // }
  11407. // }
  11408. // }
  11409. }
  11410. // 如果有新增或更新,更新数据库
  11411. if ($need_update) {
  11412. $update_data = [
  11413. 'roles' => json_encode($new_global_roles, 256),
  11414. 'scenes' => json_encode($new_global_scenes, 256),
  11415. 'updated_at' => date('Y-m-d H:i:s'),
  11416. ];
  11417. $result = DB::table('mp_animes')->where('id', $anime_id)->update($update_data);
  11418. if ($result !== false) {
  11419. dLog('deepseek')->info("全局角色和场景更新成功", [
  11420. 'anime_id' => $anime_id,
  11421. 'roles_count' => count($new_global_roles),
  11422. 'scenes_count' => count($new_global_scenes),
  11423. ]);
  11424. } else {
  11425. dLog('deepseek')->error("全局角色和场景更新失败", ['anime_id' => $anime_id]);
  11426. logDB('deepseek', 'error', '全局角色和场景更新失败', ['anime_id' => $anime_id]);
  11427. }
  11428. }
  11429. }
  11430. }