DeepSeekService.php 721 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937
  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. 'updated_at' => $now
  5944. ]);
  5945. if ($boolen5 === false) {
  5946. Utils::throwError('20003:单剧集主表资源同步失败');
  5947. }
  5948. $table_data['roles'] = json_encode($saveResult['merged_roles'], 256);
  5949. $table_data['scenes'] = json_encode($saveResult['merged_scenes'], 256);
  5950. $episode_record_content = '确认分镜大纲';
  5951. if ($content !== '') {
  5952. // $episode_record_content .= "\n\n###上传内容\n" . $content;
  5953. }
  5954. $episode_records = [
  5955. [
  5956. 'uid' => $uid,
  5957. 'anime_id' => $anime_id,
  5958. 'role' => 'user',
  5959. 'content' => $episode_record_content,
  5960. 'sequence' => 1,
  5961. 'episode_id' => $episode_id,
  5962. 'created_at' => $now,
  5963. 'updated_at' => $now
  5964. ],
  5965. [
  5966. 'uid' => $uid,
  5967. 'anime_id' => $anime_id,
  5968. 'role' => 'assistant',
  5969. 'content' => $fullContent,
  5970. // 'content' => '我已按照您的要求完成策划并将内容更新到您右侧的策划文档。',
  5971. 'sequence' => 1,
  5972. 'episode_id' => $episode_id,
  5973. 'created_at' => $now,
  5974. 'updated_at' => $now
  5975. ]
  5976. ];
  5977. $boolen4 = DB::table('mp_anime_records')->insert($episode_records);
  5978. if (!$boolen4) {
  5979. Utils::throwError('20003:单剧集分镜记录保存失败');
  5980. }
  5981. }
  5982. }catch (\Exception $e) {
  5983. DB::rollBack();
  5984. dLog('deepseek')->info('新建对话错误信息: '.$e->getMessage());
  5985. yield [
  5986. 'type' => 'done',
  5987. 'answer' => $fullContent,
  5988. 'reasoning' => $fullReasoningContent,
  5989. 'usage' => $usage,
  5990. 'error' => $e->getMessage(),
  5991. ];
  5992. return;
  5993. }
  5994. DB::commit();
  5995. dLog('deepseek')->info('保存完毕');
  5996. if ($is_single && !empty($single_episode)) {
  5997. // $this->batchSetGlobalRoleImg([
  5998. // 'anime_id' => $anime_id,
  5999. // ]);
  6000. // $this->batchSetGlobalSceneImg([
  6001. // 'anime_id' => $anime_id,
  6002. // ]);
  6003. // // 将全局角色和场景的task_id、task_status和url继承给当前分集
  6004. // $this->inheritGlobalImagesToEpisodes($anime_id, $episode_id);
  6005. }
  6006. $table_data['anime_id'] = $anime_id;
  6007. $table_data['roles'] = json_decode($table_data['roles'], true);
  6008. $table_data['scenes'] = json_decode($table_data['scenes'], true);
  6009. // $table_data['episodes'] = $script_arr['episodes'];
  6010. unset($table_data['created_at']);
  6011. unset($table_data['updated_at']);
  6012. unset($table_data['status']);
  6013. dLog('deepseek')->info('开始返回');
  6014. yield [
  6015. 'type' => 'done',
  6016. 'script' => $table_data,
  6017. 'episode' => $single_episode,
  6018. 'answer' => $fullContent,
  6019. 'reasoning' => $fullReasoningContent,
  6020. 'usage' => $usage
  6021. ];
  6022. }
  6023. public function reGenerateAnimeForAce($data) {
  6024. $uid = Site::getUid();
  6025. $file = getProp($data, 'file');
  6026. $content = getProp($data, 'content', '');
  6027. $bid = getProp($data, 'bid', 0);
  6028. $script_id = getProp($data, 'script_id', 0);
  6029. $anime_id = getProp($data, 'anime_id');
  6030. $anime = DB::table('mp_animes')->where('id', $anime_id)->first();
  6031. if (!$anime) {
  6032. Utils::throwError('20003:该对话不存在');
  6033. }
  6034. $is_multi = getProp($anime, 'is_multi', 1);
  6035. $is_single = (int)$is_multi !== 1;
  6036. $extra_products = getProp($data, 'products', []);
  6037. if (!$extra_products) {
  6038. $extra_products = getProp($anime, 'extra_products');
  6039. if ($extra_products) {
  6040. $extra_products = json_decode($extra_products, true);
  6041. }else {
  6042. $extra_products = [];
  6043. }
  6044. }else {
  6045. if (is_string($extra_products)) $extra_products = json_decode($extra_products, true);
  6046. }
  6047. // 如果anime有关联的script_id,则从mp_script_product_mappings中查询资产并合并到extra_products
  6048. $anime_script_id = getProp($anime, 'script_id');
  6049. if ($anime_script_id && $is_single) {
  6050. $cpid = Site::getCpid();
  6051. $script_products_mappings = DB::table('mp_script_product_mappings as mapping')
  6052. ->join('mp_products as product', 'mapping.product_id', '=', 'product.id')
  6053. ->where('mapping.script_id', $anime_script_id)
  6054. ->where('product.cpid', $cpid)
  6055. ->where('product.is_deleted', 0);
  6056. $mappings = $script_products_mappings->select(
  6057. 'mapping.product_id',
  6058. 'mapping.episode_number',
  6059. 'product.product_name',
  6060. 'product.type',
  6061. 'product.product',
  6062. 'product.pic_prompt',
  6063. 'product.url',
  6064. 'product.pic_task_id',
  6065. 'product.pic_task_status',
  6066. 'product.three_view_image_url'
  6067. )
  6068. ->get();
  6069. // 按 product_id 分组,合并 episode_number
  6070. $productMap = [];
  6071. foreach ($mappings as $item) {
  6072. $product_id = $item->product_id;
  6073. $product_name = $item->product_name;
  6074. // 处理product_name两边的符号
  6075. $product_name = preg_replace('/^[\s*\[\]【】[]{}{}\x{3000}]+|[\s*\[\]【】[]{}{}\x{3000}]+$/u', '', $product_name);
  6076. if (!isset($productMap[$product_id])) {
  6077. $productMap[$product_id] = [
  6078. 'product_id' => $product_id,
  6079. 'product_name' => $product_name,
  6080. 'type' => (int)$item->type,
  6081. 'product' => (int)$item->product,
  6082. 'pic_prompt' => $item->pic_prompt,
  6083. 'url' => $item->url,
  6084. 'pic_task_id' => $item->pic_task_id,
  6085. 'pic_task_status' => $item->pic_task_status,
  6086. 'episode_numbers' => [],
  6087. ];
  6088. }
  6089. // 添加 episode_number(去重)
  6090. if (!in_array($item->episode_number, $productMap[$product_id]['episode_numbers'])) {
  6091. $productMap[$product_id]['episode_numbers'][] = $item->episode_number;
  6092. }
  6093. }
  6094. // 将查询到的剧本资产合并到extra_products中
  6095. // 先构建extra_products的索引(以product_id为key,用于去重)
  6096. $extraProductsMap = [];
  6097. foreach ($extra_products as $item) {
  6098. $key = getProp($item, 'product_id');
  6099. if ($key) {
  6100. $extraProductsMap[$key] = $item;
  6101. }
  6102. }
  6103. // 合并剧本资产(去重:仅按product_id去重,不考虑episode_number)
  6104. foreach ($productMap as $product) {
  6105. $product_id = $product['product_id'];
  6106. // 如果不存在则添加(不带episode_number信息)
  6107. if (!isset($extraProductsMap[$product_id])) {
  6108. $extraProductsMap[$product_id] = [
  6109. 'product_id' => $product['product_id'],
  6110. 'product_name' => $product['product_name'],
  6111. 'type' => $product['type'],
  6112. 'product' => $product['product'],
  6113. 'pic_prompt' => $product['pic_prompt'],
  6114. 'url' => $product['url'],
  6115. 'pic_task_id' => $product['pic_task_id'],
  6116. 'pic_task_status' => $product['pic_task_status'],
  6117. ];
  6118. }
  6119. }
  6120. // 重新赋值给extra_products
  6121. $extra_products = array_values($extraProductsMap);
  6122. }
  6123. $user_anime_name = getProp($anime, 'anime_name');
  6124. $prompt = getProp($data, 'prompt');
  6125. // 处理提示词
  6126. if ($prompt && $extra_products) {
  6127. // 遍历extra_products,将prompt中的{product_name}替换为实际的product_name
  6128. foreach($extra_products as $item) {
  6129. $product_name = getProp($item, 'product_name');
  6130. if ($product_name) {
  6131. // 替换 {product_name} 为 product_name
  6132. $prompt = str_replace('{' . $product_name . '}', $product_name, $prompt);
  6133. }
  6134. }
  6135. }
  6136. // 处理文本模型
  6137. $model = getProp($data, 'model');
  6138. if (!$model) {
  6139. // 用户没有输入,从anime表获取
  6140. $model = getProp($anime, 'model');
  6141. if (!$model) {
  6142. // anime表也没有,使用默认值
  6143. $model = 'deepseek-v4-pro';
  6144. }
  6145. }
  6146. // 验证模型是否在可用模型表中
  6147. if (!DB::table('mp_text_models')->where('model', $model)->where('is_enabled', 1)->exists()) {
  6148. $model = 'deepseek-v4-pro';
  6149. }
  6150. // 单剧集模式下,如果已经生成了剧集,则不允许继续调整大纲
  6151. if ($is_single) {
  6152. $episode_exists = DB::table('mp_anime_episodes')
  6153. ->where('anime_id', $anime_id)
  6154. ->where('sequence', 1)
  6155. ->exists();
  6156. if ($episode_exists) {
  6157. Utils::throwError('20003:单剧集模式下已生成剧集,不可继续调整大纲,请去策划剧集');
  6158. }
  6159. }
  6160. $question = getProp($data, 'question', $is_single ? "请根据实例格式完成单剧集剧本及分段\n" : "请根据实例格式完成剧本大纲\n");
  6161. if ($prompt) {
  6162. $question .= "本次修改要求如下:\n{$prompt}";
  6163. }
  6164. // 提取文件内容
  6165. if ($file) {
  6166. $content = $this->extractFileContent($file);
  6167. if (!$content) {
  6168. Utils::throwError('20003:无法提取文件内容,请检查文件格式');
  6169. }
  6170. } elseif ($script_id) {
  6171. $content = $this->getContentByScriptId($script_id);
  6172. if (!$content) {
  6173. Utils::throwError('20003:无法获取剧本内容,请检查剧本');
  6174. }
  6175. } elseif ($bid) {
  6176. $content = $this->getContentByBid($bid);
  6177. if (!$content) {
  6178. Utils::throwError('20003:无法获取书籍内容,请检查书籍');
  6179. }
  6180. } elseif ($content) {
  6181. $content = filterContent($content);
  6182. }else {
  6183. $content = filterContent(getProp($anime, 'content'));
  6184. if (!$content && $prompt) { // 如果动漫记录中没有内容则根据提示词生成
  6185. $need_generate_content = true;
  6186. }
  6187. }
  6188. // 判断是否需要生成原文内容
  6189. $need_generate_content = isset($need_generate_content) && $need_generate_content;
  6190. // 如果需要生成原文内容,添加额外的要求
  6191. $content_generation_requirement = $need_generate_content
  6192. ? "\n5.如果用户只提供了创作要求而没有提供具体文档内容,你需要根据要求创作完整的原文内容(小说形式,包含对话和叙述),并将原文内容放在最后一个板块###原文内容中输出。原文内容应该详细、生动,如果用户没有明确生成的字数,则字数不少于1000字。"
  6193. : "";
  6194. // 美术风格
  6195. $input_art_style = getProp($data, 'art_style');
  6196. if (!$input_art_style) {
  6197. $mappedArtStyle = getProp($anime, 'art_style');
  6198. }else {
  6199. $mappedArtStyle = $this->getArtStylePromptByInput($input_art_style);
  6200. }
  6201. if (!$mappedArtStyle) {
  6202. $mappedArtStyle_prompt = '美术风格:根据剧本内容自定义美术风格';
  6203. }else {
  6204. if (strstr($mappedArtStyle, '基础画风风格词')) {
  6205. $mappedArtStyle_prompt = "美术风格强制设置如下(原样输出,不得更改任何内容):\n{$mappedArtStyle}\n";
  6206. }else {
  6207. $art_style_type = $input_art_style ? $input_art_style : getProp($anime, 'art_style_type');
  6208. $mappedArtStyle_prompt = "美术风格强制设置如下(原样输出,不得更改任何内容):\n基础画风风格词: {$art_style_type}\n{$mappedArtStyle}\n";
  6209. }
  6210. }
  6211. // 构建主体、场景和道具提示词
  6212. $extra_role_prompt = "\n";
  6213. $extra_scene_prompt = "\n";
  6214. $extra_prop_prompt = "\n";
  6215. $ref_products = []; // 参考资产
  6216. if ($extra_products) {
  6217. foreach($extra_products as $item) {
  6218. $product = getProp($item, 'product');
  6219. $product_name = getProp($item, 'product_name');
  6220. if ($product_name == '旁白') continue;
  6221. $ref_products[$product_name] = $item;
  6222. $pic_prompt = getProp($item, 'pic_prompt');
  6223. if ((int)$product === 1) {
  6224. // 拼接角色信息
  6225. $extra_role_prompt .= $product_name . ":" . $pic_prompt . "\n";
  6226. } elseif ((int)$product === 2) {
  6227. // 拼接场景信息
  6228. $extra_scene_prompt .= $product_name . ":" . $pic_prompt . "\n";
  6229. } elseif ((int)$product === 3) {
  6230. // 拼接道具信息
  6231. $extra_prop_prompt .= $product_name . ":" . $pic_prompt . "\n";
  6232. }
  6233. }
  6234. // 优化提示词,融入剧本
  6235. if (!empty(trim($extra_role_prompt))) {
  6236. $extra_role_prompt = "\n2.5【强制主体设定-不可修改】\n以下主体必须全部出现在<主体列表>中,必须包含:\n" . $extra_role_prompt . "\n格式要求:\n1. 主体名称:必须保持不变\n2. 主体描述:你必须根据剧情为每个主体生成主体描述(分析人物在整部剧中的姓名、年龄、性格特征、社会身份或职业背景、个人成长经历或核心故事动机,以及在剧情中的目标与行为驱动力,用一句话进行总结)\n3. 人物提示词:{}中的内容必须完整保留,不得修改\n4. 音色提示词:你必须根据角色特征生成音色提示词(描述角色的整体音色特征,包含:性别、年龄、音质、表达形式、情绪氛围,字数控制在80字以内),用{{}}框起来\n\n最终格式必须为:主体名:[你生成的主体描述]{人物提示词-保持不变}{{你生成的音色提示词}}\n\n【重要】以上每个主体在<主体列表>中的完整格式必须是:主体名:主体描述{人物提示词}{{音色提示词}},其中主体描述和音色提示词是你必须生成的内容,{}中的人物提示词必须原样保留。";
  6237. }
  6238. if (!empty(trim($extra_scene_prompt))) {
  6239. $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【重要提示】以上每个场景在<场景列表>中的完整格式必须是:场景名:场景描述{场景提示词},其中场景描述是你必须生成的内容,{}中的场景提示词必须原样保留。";
  6240. }
  6241. if (!empty(trim($extra_prop_prompt))) {
  6242. $extra_prop_prompt = "\n4.4【强制道具设定-不可修改】\n以下道具必须全部出现在<道具列表>中,必须包含:\n" . $extra_prop_prompt . "\n格式要求:\n1. 道具名称:必须保持不变\n2. 道具描述:你必须根据剧情为每个道具生成道具描述(分析在整部剧中出现过的道具,道具描述尽量简洁贴合原文)\n3. 道具提示词:{}中的内容必须完整保留,不得修改\n\n最终格式必须为:道具名:[你生成的道具描述]{道具提示词-保持不变}\n\n必须包含的道具列表:\n" . $extra_prop_prompt . "\n【重要提示】以上每个道具在<道具列表>中的完整格式必须是:道具名:道具描述{道具提示词},其中道具描述是你必须生成的内容,{}中的道具提示词必须原样保留。";
  6243. }
  6244. }
  6245. $system_message = ['role'=>'system', 'content'=>$is_single
  6246. ? "你是一个专业的文档分析助手及资深编剧,请根据用户提供的文档内容输出单剧集完整策划稿(剧本大纲包括: <故事梗概><剧本亮点><人物关系><核心矛盾><主体列表><美术风格><场景列表><道具列表><分段剧本>,每个板块之间必须使用###分隔,#不能多也不能少)。单剧集模式下,必须一次性输出可直接保存的大纲与完整分镜,不允许只输出大纲。
  6247. 强制要求:
  6248. 1.你只负责与剧本相关的内容,如果用户输入与剧本无关的内容,请告知用户并拒绝继续回答,此为第一优先原则,不满足此原则就忽略其他要求并停止继续输出
  6249. 2.如果用户输入修改美术风格、修改画风这类内容,请告知用户不可修改美术风格,并拒绝继续回答
  6250. 3.以上两条要求对修改主体角色或场景不生效,允许用户调整主体角色说明和场景描述等
  6251. 4.用户如果要求你帮忙写一个故事或者剧本,此类要求不属于与剧本无关,请帮助用户完成请求{$content_generation_requirement}
  6252. 5.强制按照示例格式返回内容,不要更改格式或随意增加符号\n
  6253. 普通要求:
  6254. 1.剧本名(必须生成)和剧集标题必须与文档内容高度相关,(其中剧集序号是1)。
  6255. 2.<主体列表>中出现的“主体名”必须尽可能统一,严禁出现角色-科学家、角色-复仇者、角色-领导者等带有身份、职业、标签类的命名,只能使用服装差异(如校服装、卫衣装、西装)来区分同一角色,如角色-校服装等。
  6256. 2.1<主体列表>分以下三部分(强制格式):主体描述、人物提示词和音色提示词,格式必须为:主体名:主体描述{人物提示词}{{音色提示词}}
  6257. 2.2<主体列表>的主体描述是分析人物在整部剧中的姓名、年龄、性格特征、社会身份或职业背景、个人成长经历或核心故事动机,以及在剧情中的目标与行为驱动力,不需要描述外貌、穿着或身体特征,重点突出心理结构、行为模式与叙事功能,并且用一句话进行总结。
  6258. 2.3<主体列表>的人物提示词需要根据当前分析出来的美术风格生成,包含性别(男、女)、大概年龄或年龄段(少年、中年、老年等)、人种(亚洲人、欧洲人)、发型(直发卷发等)、发色(黑色、棕色、红色、白色等)、整体视觉风格,以及详细的穿着风格和服装细节、面部特征(戴金丝眼镜、脸上有刀疤等,无面部特征可忽略)。描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合人物身份设定。该提示词(字数50-100字左右)在主体描述后用{}框起来。
  6259. 2.4<主体列表>的音色提示词放在描述的最后用{{}}框起来。音色提示词需描述角色的整体音色特征,包含:性别、年龄(约XX岁)、音质(如清冷、沉稳、磁性等)、表达形式(如叙述感、旁白式等)、情绪氛围(如空灵韵味、看透世事的清冷等),字数控制在80字以内。举例:成年女性(约25-35岁),音色清冷且富有叙述感,语速平缓,带有仙侠剧特有的空灵韵味。
  6260. {$extra_role_prompt}
  6261. 3.<场景列表>与主体列表同理,需尽可能的对场景环境进行详细描述,必须严格注意不要出现任何角色和人物。
  6262. 3.1<场景列表>分以下两部分(强制格式):场景描述和场景提示词,格式必须为:场景描述{场景提示词}
  6263. 3.2<场景列表>的场景描述是分析在整部剧中出现过的场景,场景描述尽量简洁贴合原文。
  6264. 3.3<场景列表>的场景提示词用于视觉呈现的场景环境描述,该场景需包含环境氛围、光线效果、空间布局、色彩基调、视觉风格等,描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合剧情设定(可参考以下示例的场景列表中使用{}框起来的提示词,字数150-200字,需带上提炼出来的美术风格)。该提示词在场景描述后用{}框起来。\n
  6265. {$extra_scene_prompt}
  6266. 4<道具列表>与场景列表同理,需尽可能的对道具环境进行详细描述,必须严格注意不要出现任何角色和人物。
  6267. 4.1<道具列表>分以下两部分(强制格式):道具描述和道具提示词,格式必须为:道具描述{道具提示词}
  6268. 4.2<道具列表>的道具描述是分析在整部剧中出现过的道具,道具描述尽量简洁贴合原文。
  6269. 4.3<道具列表>的道具提示词用于视觉呈现的道具环境描述,该道具需包含环境氛围、光线效果、空间布局、色彩基调、视觉风格等,描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合剧情设定(可参考以下示例的道具列表中使用{}框起来的提示词,字数150-200字,需带上提炼出来的美术风格)。该提示词在道具描述后用{}框起来。\n
  6270. {$extra_scene_prompt}
  6271. 5.<分段剧本>按照“分段模式”进行拆分和组织,要求如下:
  6272. 5.1片段分割逻辑(优先级从高到低):
  6273. - 第一优先级:时长上限(单个片段不超过15秒,超过必须切为新片段)
  6274. - 第二优先级:叙事节拍Beat(一个完整的事件/动作/情绪起落单元,如一段对话的来回、一个动作序列:推门→看见→震惊、一次闪回/回忆、旁白+画面组合)
  6275. - 第三优先级:语义块(按“谁、在哪、做什么、情绪怎样”聚合,如同一段旁白/独白、同一个角色的连续动作、同一个视角的连续镜头)
  6276. - 只要事件没结束、情绪没断,哪怕换场景,也留在同一个片段里
  6277. 5.2片段结构:片段数量+旁白音色+多个片段(每个片段由多个分镜组合而成(片段序号格式:片段1、片段2...))
  6278. - 片段数量格式为: 片段数量:8
  6279. - 旁白音色需描述旁白的整体音色特征,包含:性别、年龄(约XX岁)、音质(如清冷、沉稳、磁性等)、表达形式(如叙述感、旁白式等)、情绪氛围(如空灵韵味、看透世事的清冷等),字数控制在80字以内。举例:成年女性(约25-35岁),音色清冷且富有叙述感,语速平缓,带有仙侠剧特有的空灵韵味。
  6280. - 片段序号格式必须如下: ##片段+片段序号\n时长:xxs,例如: ##片段1\n时长:12s
  6281. 5.3分镜结构:每个分镜包含以下字段:
  6282. - 分镜序号在片段内用1、2、3显示,如分镜1、分镜2,并且分镜序号需连续(跨片段中的分镜序号需连续,如片段2中的分镜序号从片段1的最大序号+1开始),分镜序号前后不要带任何其他符号,特别是*符号。
  6283. - 出场角色: 如果有主体出现在本镜,则必须选择本集中的<主体列表>中的一个(如必须使用“沈秋岚-程家居家围裙造型”,而不是“沈秋岚”,不得缺少“-”前后和“:”前面的任何内容,不能自创)
  6284. - 场景:必须选择本集中的<场景列表>中的一个(如需使用“酒店会客室-景池筹备会版”,而不是“酒店会客室”,不得缺少“-”前后和“:”前面的任何内容,不能自创)
  6285. - 出场道具:如果有道具出现在本镜,则必须选择本集中的<道具列表>中的一个(如需使用“酒杯-高脚杯”,而不是“酒杯”或“高脚杯”,不得缺少“-”前后和“:”前面的任何内容,不能自创)
  6286. - 画面:【构图/视角】+画面描述(越简单越好,对视觉场景/实景的描述,不描述氛围,比如紧张、压抑的氛围)。举例:[近景特写,浅景深]季潇握着手机贴在耳边,指关节因为用力而泛白。她的眼眶微红,睫毛在狂风中颤抖,嘴唇紧抿,眼神中闪烁着最后一点希冀的微光。
  6287. - 画面中如果出现主体,只能选择上述<主体列表>中的一个,主体的名称也必须保持一致(如必须使用“沈秋岚-程家居家围裙造型”,而不是“沈秋岚”,不得缺少“-”前后和“:”前面的任何内容,不能自创)
  6288. - 画面中如果出现道具,只能选择上述<道具列表>中的一个,道具的名称也必须保持一致(如必须使用“酒杯-高脚杯”,而不是“酒杯”或“高脚杯”,不得缺少“-”前后和“:”前面的任何内容,不能自创)
  6289. - 运镜:场景+画面描述+运镜
  6290. - 运镜描述中如果出现主体或道具,逻辑需同画面一直,必须选择<主体列表>或<道具列表>中的一个,名称也必须保持一致
  6291. - 配音台词:格式为“中文配音:[角色]台词”,角色配音台词和语言由模型匹配生成,角色必须对应本集中的<主体列表>中的一个(非对话内容则统一固定为“旁白”角色,[角色]的角色名必须完整使用主体列表的主体名,如必须使用“沈秋岚-程家居家围裙造型”,而不是“沈秋岚”,不得缺少“-”前后和“:”前面的任何内容,不能自创,不能自创)
  6292. - 台词内容需满足以下要求: (1. 台词来源规则:台词内容必须逐字摘录自小说原文中的人物直接对话(带引号的内容),不允许改写、总结、压缩、扩写或拼接原文对白,不允许生成原文中不存在的对话。2. 非对话过滤:旁白、心理描写、环境描写、动作描写等内容,禁止出现在台词内容中。3. 空台词规则:如果该分镜中没有人物对话,则“台词内容”必须返回“无”,严禁输出“无台词”、“没有”等任何替代文本。4. 禁止推测:不得根据语境补全人物可能说的话。5.如果没有原文,直接返回空)
  6293. - 背景音效:背景音效需根据内容和画面生成,贴合当前画面描述和动作,例如:背景音效:高跟鞋的脚本声\n
  6294. 6.{$mappedArtStyle_prompt}\n\n
  6295. 示例如下:\n
  6296. ###剧本名:西昆仑
  6297. ###故事梗概
  6298. 内容梗概:许芸归家遭徐母毒打,她冷静周旋并编造谎言掩盖行踪,引得一旁阴沉的卞大伟愈发急躁。半个月后,徐母离家,卞大伟兽性大发欲行不轨。生死关头,隐形的程景现身肉搏,屋内家具横飞,卞大伟被“空气”重创。许芸趁乱逃出,却惊见地面凭空浮现一串血脚印。意识到守护者为救自己负伤,许芸不再逃避,对着虚无的空气执拗告白,誓要与这看不见的爱人共进退。
  6299. ###剧本亮点
  6300. 亮点1:绝境奇药,生死一线
  6301. 画面开场即是阿雪苍白无力的脸庞,梁萧眼中泪光闪烁,急促的呼吸声与心跳声烘托紧张气氛。特写梁萧将“阴阳球”塞入阿雪舌底,球体散发微光,阿雪气息从若有若无到逐渐平稳,生死逆转的瞬间,视觉与听觉冲击力十足。
  6302. 亮点2:结拜兄妹,化解尴尬
  6303. 阿雪因解衣拔刺而羞愤欲绝的特写,梁萧眼神中的挣扎与无奈。在紧迫的背景音下,梁萧急中生智,牵起阿雪颤抖的手,跪地匆匆结拜,阿雪眼神从惊惶到迷茫,最终一句“我……我愿意的”带着哭腔,将救命与羞耻的矛盾推向高潮,又以出人意料的方式解决。
  6304. 亮点3:纤发夺命,情愫暗涌
  6305. 拔刺过程的紧张特写,梁萧指尖小心翼翼地捏住细丝,缓慢抽出。当“问心刺”被完全拔出,特写它竟是一根细长、泛着寒光的女子秀发,诡异而令人震惊。阿雪苏醒后,两人眼神交汇,阿雪低头羞涩,梁萧眼中复杂的情绪,无声地传递出超越兄妹的微妙情愫。
  6306. ###人物关系
  6307. 阿雪与梁萧之间存在兄妹之情。
  6308. ###核心矛盾
  6309. 阿雪与梁萧之间存在兄妹之情,但因情愫暗生而产生微妙的矛盾。阿雪羞涩难当,梁萧情急之下提出结拜兄妹,以化解尴尬。两人匆匆结拜后,梁萧小心翼翼地拔出“问心刺”,竟发现它是一根诡异的女子秀发。阿雪转危为安,两人之间,在生死与羞涩交织的瞬间,情愫暗生,却又被兄妹之名束缚,留下无尽遐想。
  6310. ###主体列表
  6311. 阿雪:身中奇毒“问心刺”,气息奄奄,面色苍白,身体虚弱。{全景,正面拍摄,青年女性,亚洲人。
  6312. 黑色短发扎成利落的高马尾,五官精致且轮廓清晰。
  6313. 服饰穿搭:身着红白相间的专业田径运动背心与超短跑步裤,皮肤呈现健康的小麦色,四肢肌肉线条紧致有力。
  6314. 姿态:站立。}{{青年女性(约25-30岁),音色亲切、温柔,语速平稳缓慢}}
  6315. 梁萧:少年侠士,心系阿雪安危,沉着冷静,内心挣扎于情义之间。{8k超高清,电影级光效,大气透视,中国传统艺术美学,空灵光泽,深度景深,复杂精细的纹理,史诗级构图,电影胶片颗粒。
  6316. 全景,正面拍摄,青年女性,亚洲人。
  6317. 佩戴精致繁复的金丝凤冠,黑色长发整齐盘起。
  6318. 服饰穿搭:身着大红色正装嫁衣,上面绣有精细的金色凤凰纹样,质感厚重华贵。
  6319. 姿态:站立。}{{青年男性(约25-30岁),音色冷峻、低沉,语速平稳缓慢,透着一股拒人于千里之外的孤傲与肃杀。}}
  6320. ###美术风格
  6321. 基础画风风格词:厚涂古风
  6322. 整体采用精致细腻的厚涂古风,色彩浓郁而富有层次感,注重光影对比,营造出古典而富有戏剧张力的氛围。色调以深沉的蓝绿、灰调为主,衬托阿雪病弱的苍白,同时用温暖的金色、琥珀色光晕突出“阴阳球”的生命力。拔刺与结拜场景则以柔和的暖色调为主,辅以人物脸颊的红晕,烘托羞涩与情愫暗生的微妙感。人物造型飘逸,服饰细节考究,背景虚实结合,既有古韵,又不失奇幻色彩。
  6323. ###场景列表
  6324. 林间疗伤地:幽静的古风林间空地,一棵高大古树下,是梁萧为阿雪疗伤的地点。环境清幽,光影斑驳,既有古韵又不失奇幻色彩。{影视质感。广角。
  6325. 宽敞的天然山洞内部,石壁上错落有致地镶嵌着发光的萤石,散发出冷色调的微光,照亮了整个空间。洞顶有钟乳石垂下。光影层次丰富,营造出一种宁静而神圣的氛围。
  6326. 无人物。}
  6327. ###分段剧本
  6328. 片段数量:8
  6329. 旁白音色:成年男性(约35-50岁),音色厚重且富有磁性,带有老电影叙事般的沧桑感,语调低沉平稳,富有江湖气息。
  6330. ##片段1
  6331. 时长:12s
  6332. 分镜1
  6333. 出场角色:刀疤脸
  6334. 场景:边境小镇街道
  6335. 出场道具:酒杯-高脚杯
  6336. 画面:[大远景,冷色调,高对比度] 漫天黄沙在狂风中如怒龙翻滚,遮蔽了微弱的星光。悦来酒馆残破的木门在风中剧烈摇晃,铁链撞击木板发出沉重的闷响,四周建筑尽显荒凉。
  6337. 运镜:大远景固定镜头,随后风沙席卷向镜头形成遮挡转场。
  6338. 配音台词:
  6339. 背景音效:
  6340. 分镜2
  6341. 出场角色:刀疤脸
  6342. 场景:悦来酒馆
  6343. 出场道具:酒杯-高脚杯
  6344. 画面:[全景,暖色调阴影构图] 酒馆内部昏暗,几盏油灯在风中疯狂摇曳,投射出扭曲的长影。角落里的食客低头缩颈,双手紧紧攥着粗瓷碗,空气中漂浮着细小的尘埃。
  6345. 运镜:缓慢横移,掠过阴影中瑟缩的食客。
  6346. 配音台词:
  6347. 背景音效:
  6348. 分镜3
  6349. 出场角色:刀疤脸
  6350. 场景:悦来酒馆
  6351. 出场道具:酒杯-高脚杯
  6352. 画面:[中景,三分法构图] 三名大汉围坐在正中央的圆桌旁,腰间挂着的“武林盟”金色令牌在灯火下闪烁。桌上残羹冷炙横陈,一名大汉正张狂地大笑,脚踩在长凳上。
  6353. 运镜:从中景推向令牌特写。
  6354. 配音台词:
  6355. 背景音效:
  6356. 分镜4
  6357. 出场角色:刀疤脸
  6358. 场景:悦来酒馆
  6359. 出场道具:酒杯-高脚杯
  6360. 画面:[特写,低角度仰拍] 刀疤脸横肉颤动,嘴角挂着轻蔑的笑。他随手倾覆手中的酒碗,琥珀色的液体在空中划过弧线,重重溅在老李打着补丁的布鞋和裤脚上。
  6361. 运镜:极速推向酒水溅起的瞬间。
  6362. 配音台词:
  6363. 背景音效:
  6364. 分镜5
  6365. 出场角色:刀疤脸
  6366. 场景:悦来酒馆
  6367. 出场道具:酒杯-高脚杯
  6368. 画面:[近景,荷兰角倾斜构图] 刀疤脸斜睨着眼,昏暗的灯光勾勒出他脸上狰狞的伤疤。他身体前倾,阴影完全笼罩了对面的老李,眼神如毒蛇般阴冷。
  6369. 运镜:固定机位,利用倾斜构图制造压迫感。
  6370. 配音台词:中文配音:[刀疤脸]老李头,这酒里掺水了吧?
  6371. 背景音效:
  6372. \n\n"
  6373. : "你是一个专业的文档分析助手及资深编剧,请根据用户提供的文档内容及下面提供的示例格式完成剧本大纲(需通过以下几个板块进行回复: <故事梗概><剧本亮点><人物关系><核心矛盾><主体列表><美术风格><场景列表><道具列表>,每个板块之间必须使用###分隔,#不能多也不能少;同时板块之间需满足以下要求:\n
  6374. 强制要求:\n
  6375. 1.你只负责与剧本相关的内容,如果用户输入与剧本无关的内容,请告知用户并拒绝继续回答,此为第一优先原则,不满足此原则就忽略其他要求并停止继续输出
  6376. 2.如果用户输入修改美术风格、修改画风这类内容,请告知用户不可修改美术风格,并拒绝继续回答
  6377. 3.以上两条要求对修改主体角色或场景不生效,允许用户调整主体角色说明和场景描述等
  6378. 4.用户如果要求你帮忙写一个故事或者剧本,此类要求不属于与剧本无关,请帮助用户完成请求{$content_generation_requirement}
  6379. 5.强制按照示例格式返回内容,不要更改格式或随意增加符号\n
  6380. 普通要求:\n
  6381. 1.剧本名(必须生成)必须与文档内容高度相关
  6382. 2.<主体列表>中出现的“主体名”必须尽可能统一,严禁出现角色-科学家、角色-复仇者、角色-领导者等带有身份、职业、标签类的命名,只能使用服装差异(如校服装、卫衣装、西装)来区分同一角色,如角色-校服装等。
  6383. 2.1<主体列表>分以下三部分(强制格式):主体描述、人物提示词和音色提示词,格式必须为:主体名:主体描述{人物提示词}{{音色提示词}}
  6384. 2.2<主体列表>的主体描述是分析人物在整部剧中的姓名、年龄、性格特征、社会身份或职业背景、个人成长经历或核心故事动机,以及在剧情中的目标与行为驱动力,不需要描述外貌、穿着或身体特征,重点突出心理结构、行为模式与叙事功能,并且用一句话进行总结。
  6385. 2.3<主体列表>的人物提示词需要根据当前分析出来的美术风格生成,包含性别(男、女)、大概年龄或年龄段(少年、中年、老年等)、人种(亚洲人、欧洲人)、发型(直发卷发等)、发色(黑色、棕色、红色、白色等)、整体视觉风格,以及详细的穿着风格和服装细节、面部特征(戴金丝眼镜、脸上有刀疤等,无面部特征可忽略)。描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合人物身份设定。该提示词(字数50-100字左右)在主体描述后用{}框起来。
  6386. 2.4<主体列表>的音色提示词放在描述的最后用{{}}框起来。音色提示词需描述角色的整体音色特征,包含:性别、年龄(约XX岁)、音质(如清冷、沉稳、磁性等)、表达形式(如叙述感、旁白式等)、情绪氛围(如空灵韵味、看透世事的清冷等),字数控制在80字以内。举例:成年女性(约25-35岁),音色清冷且富有叙述感,语速平缓,带有仙侠剧特有的空灵韵味。
  6387. {$extra_role_prompt}
  6388. 3.<场景列表>与主体列表同理,需尽可能的对场景环境进行详细描述,必须严格注意不要出现任何角色和人物。
  6389. 3.1<场景列表>分以下两部分(强制格式):场景描述和场景提示词,格式必须为:场景描述{场景提示词}
  6390. 3.2<场景列表>的场景描述是分析在整部剧中出现过的场景,场景描述尽量简洁贴合原文。
  6391. 3.3<场景列表>的场景提示词用于视觉呈现的场景环境描述,该场景需包含环境氛围、光线效果、空间布局、色彩基调、视觉风格等,描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合剧情设定(可参考以下示例的场景列表中使用{}框起来的提示词,字数150-200字,需带上提炼出来的美术风格)。该提示词在场景描述后用{}框起来。\n
  6392. {$extra_scene_prompt}
  6393. 4.<道具列表>与场景列表同理,需尽可能的对道具环境进行详细描述,必须严格注意不要出现任何角色和人物。
  6394. 3.1<道具列表>分以下两部分(强制格式):道具描述和道具提示词,格式必须为:道具描述{道具提示词}
  6395. 3.2<道具列表>的道具描述是分析在整部剧中出现过的道具,道具描述尽量简洁贴合原文。
  6396. 3.3<道具列表>的道具提示词用于视觉呈现的道具环境描述,该道具需包含环境氛围、光线效果、空间布局、色彩基调、视觉风格等,描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合剧情设定(可参考以下示例的道具列表中使用{}框起来的提示词,字数150-200字,需带上提炼出来的美术风格)。该提示词在道具描述后用{}框起来。\n
  6397. {$extra_prop_prompt}
  6398. 5.{$mappedArtStyle_prompt}\n\n
  6399. 示例如下:\n
  6400. ###剧本名:西昆仑
  6401. ###故事梗概
  6402. 内容梗概:许芸归家遭徐母毒打,她冷静周旋并编造谎言掩盖行踪,引得一旁阴沉的卞大伟愈发急躁。半个月后,徐母离家,卞大伟兽性大发欲行不轨。生死关头,隐形的程景现身肉搏,屋内家具横飞,卞大伟被“空气”重创。许芸趁乱逃出,却惊见地面凭空浮现一串血脚印。意识到守护者为救自己负伤,许芸不再逃避,对着虚无的空气执拗告白,誓要与这看不见的爱人共进退。
  6403. ###剧本亮点
  6404. 亮点1:绝境奇药,生死一线
  6405. 画面开场即是阿雪苍白无力的脸庞,梁萧眼中泪光闪烁,急促的呼吸声与心跳声烘托紧张气氛。特写梁萧将“阴阳球”塞入阿雪舌底,球体散发微光,阿雪气息从若有若无到逐渐平稳,生死逆转的瞬间,视觉与听觉冲击力十足。
  6406. 亮点2:结拜兄妹,化解尴尬
  6407. 阿雪因解衣拔刺而羞愤欲绝的特写,梁萧眼神中的挣扎与无奈。在紧迫的背景音下,梁萧急中生智,牵起阿雪颤抖的手,跪地匆匆结拜,阿雪眼神从惊惶到迷茫,最终一句“我……我愿意的”带着哭腔,将救命与羞耻的矛盾推向高潮,又以出人意料的方式解决。
  6408. 亮点3:纤发夺命,情愫暗涌
  6409. 拔刺过程的紧张特写,梁萧指尖小心翼翼地捏住细丝,缓慢抽出。当“问心刺”被完全拔出,特写它竟是一根细长、泛着寒光的女子秀发,诡异而令人震惊。阿雪苏醒后,两人眼神交汇,阿雪低头羞涩,梁萧眼中复杂的情绪,无声地传递出超越兄妹的微妙情愫。
  6410. ###人物关系
  6411. 阿雪与梁萧之间存在兄妹之情。
  6412. ###核心矛盾
  6413. 阿雪与梁萧之间存在兄妹之情,但因情愫暗生而产生微妙的矛盾。阿雪羞涩难当,梁萧情急之下提出结拜兄妹,以化解尴尬。两人匆匆结拜后,梁萧小心翼翼地拔出“问心刺”,竟发现它是一根诡异的女子秀发。阿雪转危为安,两人之间,在生死与羞涩交织的瞬间,情愫暗生,却又被兄妹之名束缚,留下无尽遐想。
  6414. ###主体列表
  6415. 阿雪:身中奇毒“问心刺”,气息奄奄,面色苍白,身体虚弱。{全景,正面拍摄,青年女性,亚洲人。
  6416. 黑色短发扎成利落的高马尾,五官精致且轮廓清晰。
  6417. 服饰穿搭:身着红白相间的专业田径运动背心与超短跑步裤,皮肤呈现健康的小麦色,四肢肌肉线条紧致有力。
  6418. 姿态:站立。}{{青年女性(约25-30岁),音色亲切、温柔,语速平稳缓慢}}
  6419. 梁萧:少年侠士,心系阿雪安危,沉着冷静,内心挣扎于情义之间。{8k超高清,电影级光效,大气透视,中国传统艺术美学,空灵光泽,深度景深,复杂精细的纹理,史诗级构图,电影胶片颗粒。
  6420. 全景,正面拍摄,青年女性,亚洲人。
  6421. 佩戴精致繁复的金丝凤冠,黑色长发整齐盘起。
  6422. 服饰穿搭:身着大红色正装嫁衣,上面绣有精细的金色凤凰纹样,质感厚重华贵。
  6423. 姿态:站立。}{{青年男性(约25-30岁),音色冷峻、低沉,语速平稳缓慢,透着一股拒人于千里之外的孤傲与肃杀。}}
  6424. ###美术风格
  6425. 基础画风风格词:厚涂古风
  6426. 整体采用精致细腻的厚涂古风,色彩浓郁而富有层次感,注重光影对比,营造出古典而富有戏剧张力的氛围。色调以深沉的蓝绿、灰调为主,衬托阿雪病弱的苍白,同时用温暖的金色、琥珀色光晕突出“阴阳球”的生命力。拔刺与结拜场景则以柔和的暖色调为主,辅以人物脸颊的红晕,烘托羞涩与情愫暗生的微妙感。人物造型飘逸,服饰细节考究,背景虚实结合,既有古韵,又不失奇幻色彩。
  6427. ###场景列表
  6428. 林间疗伤地:幽静的古风林间空地,一棵高大古树下,是梁萧为阿雪疗伤的地点。环境清幽,光影斑驳,既有古韵又不失奇幻色彩。{影视质感。广角。
  6429. 宽敞的天然山洞内部,石壁上错落有致地镶嵌着发光的萤石,散发出冷色调的微光,照亮了整个空间。洞顶有钟乳石垂下。光影层次丰富,营造出一种宁静而神圣的氛围。
  6430. 无人物。}
  6431. ###道具列表
  6432. 酒杯-高脚杯:高脚杯,盛有少量酒液。{高脚酒杯,道具多角度视角图,正面、背面、侧面、局部细节,透明玻璃材质,细长杯柄,尺寸标注高24厘米杯口6厘米,白底干净展示,无人物}"];
  6433. // 如果提示词$prompt有类似于改成N集或调整成N集这类的文字信息,则提取出N这个集数
  6434. $episode_count = $this->extractEpisodeNumber($prompt);
  6435. if ((int)getProp($anime, 'is_multi') !== 1) {
  6436. yield [
  6437. 'type' => 'done',
  6438. 'answer' => '当前剧本是单剧集,不支持修改剧集数',
  6439. 'reasoning' => '',
  6440. 'usage' => []
  6441. ];
  6442. return;
  6443. }
  6444. if ($episode_count) {
  6445. $question = "请根据实例格式完成{$episode_count}集剧本大纲";
  6446. $system_message = [
  6447. 'role' => 'system',
  6448. 'content' => "你是一个专业的文档分析助手及资深编剧,请根据用户提供的文档内容及下面提供的示例格式完成剧本大纲(需通过以下几个板块进行回复: <故事梗概><剧本亮点><人物关系><核心矛盾><主体列表><美术风格><场景列表><分集详细内容>,每个板块之间必须使用###分隔,#不能多也不能少;同时板块之间需满足以下要求:\n
  6449. 强制要求:\n
  6450. 1.你只负责与剧本相关的内容,如果用户输入与剧本无关的内容,请告知用户并拒绝继续回答,此为第一优先原则,不满足此原则就忽略其他要求并停止继续输出
  6451. 2.如果用户输入修改美术风格、修改画风这类内容,请告知用户不可修改美术风格,并拒绝继续回答
  6452. 3.以上两条要求对修改主体角色或场景不生效,允许用户调整主体角色说明和场景描述等
  6453. 4.用户如果要求你帮忙写一个故事或者剧本,此类要求不属于与剧本无关,请帮助用户完成请求{$content_generation_requirement}
  6454. 5.强制按照示例格式返回内容,不要更改格式或随意增加符号\n
  6455. 普通要求:\n
  6456. 1.<故事梗概>控制在200-300字左右
  6457. 2.<主体列表>中出现的“主体名”必须尽可能统一,严禁出现角色-科学家、角色-复仇者、角色-领导者等带有身份、职业、标签类的命名,只能使用服装差异(如校服装、卫衣装、西装)来区分同一角色,如角色-校服装等。
  6458. 2.1<主体列表>分以下三部分:主体描述、人物提示词和音色提示词,格式必须为:主体名:主体描述{人物提示词}{{音色提示词}}
  6459. 2.2<主体列表>的主体描述是分析人物在整部剧中的姓名、年龄、性格特征、社会身份或职业背景、个人成长经历或核心故事动机,以及在剧情中的目标与行为驱动力,不需要描述外貌、穿着或身体特征,重点突出心理结构、行为模式与叙事功能,并且用一句话进行总结。
  6460. 2.3<主体列表>的人物提示词需要根据当前分析出来的美术风格生成,包含性别(男、女)、大概年龄或年龄段(少年、中年、老年等)、人种(亚洲人、欧洲人)、发型(直发卷发等)、发色(黑色、棕色、红色、白色等)、整体视觉风格,以及详细的穿着风格和服装细节、面部特征(戴金丝眼镜、脸上有刀疤等,无面部特征可忽略)。描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合人物身份设定。该提示词(字数50-100字左右)在主体描述后用{}框起来。
  6461. 2.4<主体列表>的音色提示词放在描述的最后用{{}}框起来。音色提示词需描述角色的整体音色特征,包含:性别、年龄(约XX岁)、音质(如清冷、沉稳、磁性等)、表达形式(如叙述感、旁白式等)、情绪氛围(如空灵韵味、看透世事的清冷等),字数控制在80字以内。举例:成年女性(约25-35岁),音色清冷且富有叙述感,语速平缓,带有仙侠剧特有的空灵韵味。
  6462. 3.<场景列表>与主体列表同理,需尽可能的对场景环境进行详细描述,必须严格注意不要出现任何角色和人物。
  6463. 3.1<场景列表>分以下两部分:场景描述和场景提示词,格式必须为:场景描述{场景提示词},注意不要出现{{}}这样的双大括号。
  6464. 3.2<场景列表>的场景描述是分析在整部剧中出现过的场景,场景描述尽量简洁贴合原文。
  6465. 3.3<场景列表>的场景提示词用于视觉呈现的场景环境描述,该场景需包含环境氛围、光线效果、空间布局、色彩基调、视觉风格等,描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合剧情设定(可参考以下示例的场景列表中使用{}框起来的提示词,字数150-200字,需带上提炼出来的美术风格)。该提示词在场景描述后用{}框起来。\n
  6466. 4.<分集详细内容>中每一集的内容需尽可能的详细,保留对话内容(使用原文内容,不得自己生成),一般每集字数尽量不低于500字,每集结尾需以##第N章 作为章节标题\n\n
  6467. 示例如下:\n
  6468. ###剧本名:西昆仑
  6469. ###故事梗概
  6470. 内容梗概:许芸归家遭徐母毒打,她冷静周旋并编造谎言掩盖行踪,引得一旁阴沉的卞大伟愈发急躁。半个月后,徐母离家,卞大伟兽性大发欲行不轨。生死关头,隐形的程景现身肉搏,屋内家具横飞,卞大伟被“空气”重创。许芸趁乱逃出,却惊见地面凭空浮现一串血脚印。意识到守护者为救自己负伤,许芸不再逃避,对着虚无的空气执拗告白,誓要与这看不见的爱人共进退。
  6471. ###剧本亮点
  6472. 亮点1:绝境奇药,生死一线
  6473. 画面开场即是阿雪苍白无力的脸庞,梁萧眼中泪光闪烁,急促的呼吸声与心跳声烘托紧张气氛。特写梁萧将“阴阳球”塞入阿雪舌底,球体散发微光,阿雪气息从若有若无到逐渐平稳,生死逆转的瞬间,视觉与听觉冲击力十足。
  6474. 亮点2:结拜兄妹,化解尴尬
  6475. 阿雪因解衣拔刺而羞愤欲绝的特写,梁萧眼神中的挣扎与无奈。在紧迫的背景音下,梁萧急中生智,牵起阿雪颤抖的手,跪地匆匆结拜,阿雪眼神从惊惶到迷茫,最终一句“我……我愿意的”带着哭腔,将救命与羞耻的矛盾推向高潮,又以出人意料的方式解决。
  6476. 亮点3:纤发夺命,情愫暗涌
  6477. 拔刺过程的紧张特写,梁萧指尖小心翼翼地捏住细丝,缓慢抽出。当“问心刺”被完全拔出,特写它竟是一根细长、泛着寒光的女子秀发,诡异而令人震惊。阿雪苏醒后,两人眼神交汇,阿雪低头羞涩,梁萧眼中复杂的情绪,无声地传递出超越兄妹的微妙情愫。
  6478. ###人物关系
  6479. 阿雪与梁萧之间存在兄妹之情。
  6480. ###核心矛盾
  6481. 阿雪与梁萧之间存在兄妹之情,但因情愫暗生而产生微妙的矛盾。阿雪羞涩难当,梁萧情急之下提出结拜兄妹,以化解尴尬。两人匆匆结拜后,梁萧小心翼翼地拔出“问心刺”,竟发现它是一根诡异的女子秀发。阿雪转危为安,两人之间,在生死与羞涩交织的瞬间,情愫暗生,却又被兄妹之名束缚,留下无尽遐想。
  6482. ###主体列表
  6483. 阿雪:身中奇毒“问心刺”,气息奄奄,面色苍白,身体虚弱。{全景,正面拍摄,青年女性,亚洲人。
  6484. 黑色短发扎成利落的高马尾,五官精致且轮廓清晰。
  6485. 服饰穿搭:身着红白相间的专业田径运动背心与超短跑步裤,皮肤呈现健康的小麦色,四肢肌肉线条紧致有力。
  6486. 姿态:站立。}{{青年女性(约25-30岁),音色亲切、温柔,语速平稳缓慢}}
  6487. 梁萧:少年侠士,心系阿雪安危,沉着冷静,内心挣扎于情义之间。{8k超高清,电影级光效,大气透视,中国传统艺术美学,空灵光泽,深度景深,复杂精细的纹理,史诗级构图,电影胶片颗粒。
  6488. 全景,正面拍摄,青年女性,亚洲人。
  6489. 佩戴精致繁复的金丝凤冠,黑色长发整齐盘起。
  6490. 服饰穿搭:身着大红色正装嫁衣,上面绣有精细的金色凤凰纹样,质感厚重华贵。
  6491. 姿态:站立。}{{青年男性(约25-30岁),音色冷峻、低沉,语速平稳缓慢,透着一股拒人于千里之外的孤傲与肃杀。}}
  6492. ###美术风格
  6493. 基础画风风格词:厚涂古风
  6494. 整体采用精致细腻的厚涂古风,色彩浓郁而富有层次感,注重光影对比,营造出古典而富有戏剧张力的氛围。色调以深沉的蓝绿、灰调为主,衬托阿雪病弱的苍白,同时用温暖的金色、琥珀色光晕突出“阴阳球”的生命力。拔刺与结拜场景则以柔和的暖色调为主,辅以人物脸颊的红晕,烘托羞涩与情愫暗生的微妙感。人物造型飘逸,服饰细节考究,背景虚实结合,既有古韵,又不失奇幻色彩。
  6495. ###场景列表
  6496. 林间疗伤地:幽静的古风林间空地,一棵高大古树下,是梁萧为阿雪疗伤的地点。环境清幽,光影斑驳,既有古韵又不失奇幻色彩。{影视质感。广角。
  6497. 宽敞的天然山洞内部,石壁上错落有致地镶嵌着发光的萤石,散发出冷色调的微光,照亮了整个空间。洞顶有钟乳石垂下。光影层次丰富,营造出一种宁静而神圣的氛围。
  6498. 无人物。}
  6499. ###分集详细内容
  6500. ##第1章 重生
  6501. 我重生了。
  6502. 源于一个男人偏执的暗恋。
  6503. ##第2章 相救
  6504. 我一到家,徐母的扫帚就落在我身上,嘴里一个劲地骂我,问我夜不归宿死哪儿去了。
  6505. 我眼睛扫向站在锅炉后的卞大伟。"
  6506. ];
  6507. // 构建消息
  6508. $messages = [
  6509. $system_message,
  6510. [
  6511. 'role' => 'user',
  6512. 'content' => "以下是文档内容:
  6513. {$content}
  6514. 用户问题:
  6515. {$question}"
  6516. ]
  6517. ];
  6518. }else {
  6519. if (strstr($prompt, '重新生成') || strstr($prompt, '再生成')) {
  6520. // 构建消息
  6521. $messages = [
  6522. $system_message,
  6523. [
  6524. 'role' => 'user',
  6525. 'content' => "以下是文档内容:
  6526. {$content}
  6527. 用户问题:
  6528. {$question}"
  6529. ]
  6530. ];
  6531. }else {
  6532. $messages = DB::table('mp_anime_records')->where('anime_id', $anime_id)->where('sequence', 0)
  6533. ->orderBy('id')->select('role', 'content')->get()->map(function ($value) {
  6534. return (array)$value;
  6535. })->toArray();
  6536. array_unshift($messages, $system_message);
  6537. $messages[] = [
  6538. 'role' => 'user',
  6539. 'content' => $prompt
  6540. ];
  6541. }
  6542. }
  6543. // 模型兼容性处理:deepseek-chat 和 deepseek-reasoner 映射到 deepseek-v4-flash
  6544. $thinkingMode = getProp($data, 'thinking', 'disabled'); // 默认非思考模式
  6545. $reasoningEffort = getProp($data, 'reasoning_effort', 'high'); // 默认思考强度
  6546. if ($model === 'deepseek-chat') {
  6547. // deepseek-chat 映射到 deepseek-v4-flash,非思考模式
  6548. $model = 'deepseek-v4-flash';
  6549. $thinkingMode = 'disabled';
  6550. } elseif ($model === 'deepseek-reasoner') {
  6551. // deepseek-reasoner 映射到 deepseek-v4-flash,思考模式
  6552. $model = 'deepseek-v4-flash';
  6553. $thinkingMode = 'enabled';
  6554. }
  6555. $post_data = [
  6556. 'model' => $model,
  6557. 'messages' => $messages,
  6558. // 'max_tokens' => 8192,
  6559. 'temperature' => 0.7,
  6560. 'frequency_penalty' => 0,
  6561. 'presence_penalty' => 0,
  6562. 'response_format' => ['type' => 'text'],
  6563. 'thinking' => ['type' => $thinkingMode],
  6564. 'stream' => true // 启用流式输出
  6565. ];
  6566. if ($thinkingMode == 'enabled') $post_data['reasoning_effort'] = $reasoningEffort;
  6567. // 根据模型类型选择调用方法
  6568. $fullContent = '';
  6569. $fullReasoningContent = '';
  6570. $usage = [];
  6571. // dd($post_data);
  6572. // dLog('deepseek')->info('请求参数: '.json_encode($post_data, 256));
  6573. try {
  6574. if (in_array($model, ['deepseek-reasoner', 'deepseek-chat', 'deepseek-v4-flash', 'deepseek-v4-pro'])) {
  6575. $streamGenerator = $this->deepSeekStreamResponse($post_data);
  6576. } else if (in_array($model, $this->gpt_text_models)) {
  6577. // GPT-5.4 模型使用 TokenRouter API
  6578. $streamGenerator = $this->gpt54StreamResponse($post_data);
  6579. } else {
  6580. $streamGenerator = $this->volcEngineChatCompletion($post_data);
  6581. }
  6582. // 验证返回值类型
  6583. if (!is_iterable($streamGenerator)) {
  6584. $errorType = is_object($streamGenerator) ? get_class($streamGenerator) : gettype($streamGenerator);
  6585. dLog('deepseek')->error('方法名流式生成器无效', [
  6586. 'generator_type' => $errorType,
  6587. 'model' => $model,
  6588. // 添加其他上下文信息
  6589. ]);
  6590. logDB('deepseek', 'error', '方法名流式生成器类型错误', [
  6591. 'type' => $errorType,
  6592. 'model' => $model
  6593. ]);
  6594. yield [
  6595. 'type' => 'error',
  6596. // 根据方法返回相应的空数据结构
  6597. 'answer' => '',
  6598. 'reasoning' => '',
  6599. 'usage' => [],
  6600. 'error' => '接口返回数据异常,请重新请求'
  6601. ];
  6602. return;
  6603. }
  6604. // 处理流式输出
  6605. foreach ($streamGenerator as $chunk) {
  6606. if (isset($chunk['type'])) {
  6607. if ($chunk['type'] === 'done') {
  6608. $fullContent = $chunk['full_content'];
  6609. $fullReasoningContent = $chunk['full_reasoning'];
  6610. $usage = isset($chunk['usage']) ? $chunk['usage'] : [];
  6611. } else {
  6612. yield $chunk;
  6613. }
  6614. }
  6615. }
  6616. } catch (\Exception $e) {
  6617. dLog('deepseek')->error('方法名流式处理异常: ' . $e->getMessage(), [
  6618. 'model' => $model,
  6619. 'trace' => $e->getTraceAsString()
  6620. ]);
  6621. logDB('deepseek', 'error', '方法名流式处理失败', [
  6622. 'error' => $e->getMessage(),
  6623. 'model' => $model
  6624. ]);
  6625. yield [
  6626. 'type' => 'error',
  6627. // 根据方法返回相应的空数据结构
  6628. 'answer' => '',
  6629. 'reasoning' => '',
  6630. 'usage' => [],
  6631. 'error' => '处理请求时发生异常: ' . $e->getMessage()
  6632. ];
  6633. return;
  6634. }
  6635. dLog('deepseek')->info('完整内容: '.$fullContent);
  6636. logDB('deepseek', 'info', '完整内容', ['content' => $fullContent, 'anime_id' => $anime_id]);
  6637. // 处理完整内容并返回最终结果
  6638. $script_arr = [];
  6639. if ($fullContent) {
  6640. $script_arr = handleScriptContentForAce($fullContent);
  6641. dLog('deepseek')->info('解析内容', $script_arr);
  6642. logDB('deepseek', 'info', '解析内容', $script_arr);
  6643. }
  6644. if (empty($script_arr['roles'])) {
  6645. // 未生成剧本相关内容,保存对话记录并返回提示
  6646. dLog('deepseek')->info('未生成剧本相关的内容');
  6647. try {
  6648. $now = date('Y-m-d H:i:s');
  6649. // 保存对话记录
  6650. $records = [
  6651. [
  6652. 'uid' => $uid,
  6653. 'anime_id' => $anime_id,
  6654. 'sequence' => 0,
  6655. 'role' => 'user',
  6656. 'content' => $prompt,
  6657. 'created_at' => $now,
  6658. 'updated_at' => $now
  6659. ],
  6660. [
  6661. 'uid' => $uid,
  6662. 'anime_id' => $anime_id,
  6663. 'sequence' => 0,
  6664. 'role' => 'assistant',
  6665. 'content' => '未生成剧本相关的内容,请调整提示词后重试',
  6666. 'created_at' => $now,
  6667. 'updated_at' => $now
  6668. ]
  6669. ];
  6670. DB::table('mp_anime_records')->insert($records);
  6671. } catch (\Exception $e) {
  6672. dLog('deepseek')->error('保存对话记录失败', ['error' => $e->getMessage()]);
  6673. }
  6674. yield [
  6675. 'type' => 'done',
  6676. 'script' => [],
  6677. 'episode' => [],
  6678. 'answer' => $fullContent,
  6679. 'reasoning' => $fullReasoningContent,
  6680. 'usage' => $usage,
  6681. 'error' => '未生成剧本相关的内容,请调整提示词再试'
  6682. ];
  6683. return;
  6684. }
  6685. // 替换美术风格
  6686. if ($mappedArtStyle !== '') {
  6687. $script_arr['art_style'] = $mappedArtStyle;
  6688. $fullContent = $this->replaceArtStyleSection($fullContent, $mappedArtStyle);
  6689. }
  6690. if ($user_anime_name != '新剧本策划') {
  6691. $anime_name = isset($script_arr['script_name']) ? $script_arr['script_name'] : '';
  6692. // 确认对话名称唯一性
  6693. if ($anime_name && $id = DB::table('mp_animes')->where('anime_name', $anime_name)->value('id')) {
  6694. if ($id != $anime_id) $anime_name = $anime_name . '_' . date('YmdHis');
  6695. }
  6696. }else {
  6697. $anime_name = $user_anime_name;
  6698. }
  6699. $table_data = [
  6700. 'user_id' => $uid,
  6701. 'model' => $model,
  6702. 'intro' => isset($script_arr['intro']) ? $script_arr['intro'] : '',
  6703. 'highlights' => isset($script_arr['highlights']) ? $script_arr['highlights'] : '',
  6704. 'role_relationship' => isset($script_arr['role_relationship']) ? $script_arr['role_relationship'] : '',
  6705. 'core_contradiction' => isset($script_arr['core_contradiction']) ? $script_arr['core_contradiction'] : '',
  6706. 'roles' => isset($script_arr['roles']) && is_array($script_arr['roles']) ? json_encode($script_arr['roles'], 256) : '[]',
  6707. 'art_style' => isset($script_arr['art_style']) ? $script_arr['art_style'] : '',
  6708. 'art_style_type' => $input_art_style,
  6709. 'scenes' => isset($script_arr['scenes']) && is_array($script_arr['scenes']) ? json_encode($script_arr['scenes'], 256) : '[]',
  6710. 'props' => isset($script_arr['props']) && is_array($script_arr['props']) ? json_encode($script_arr['props'], 256) : '[]',
  6711. 'status' => '解析完成',
  6712. 'generate_status' => '待执行',
  6713. 'updated_at' => date('Y-m-d H:i:s')
  6714. ];
  6715. if ($anime_name) $table_data['anime_name'] = $anime_name;
  6716. // 如果是修改集数,则将content内容更新(会改变原文)
  6717. if (isset($episode_count) && $episode_count > 0) {
  6718. if (isset($script_arr['content']) && $script_arr['content']) $table_data['content'] = $script_arr['content'];
  6719. $table_data['start_episode_sequence'] = 1;
  6720. $table_data['end_episode_sequence'] = $table_data['episode_num'] = $episode_count;
  6721. }else {
  6722. // 如果需要生成原文内容,从script_arr中提取
  6723. if ($need_generate_content && isset($script_arr['content']) && $script_arr['content']) {
  6724. $table_data['content'] = $script_arr['content'];
  6725. dLog('deepseek')->info('从AI生成结果中提取原文内容', ['content_length' => strlen($script_arr['content'])]);
  6726. if (!$table_data['content']) {
  6727. yield [
  6728. 'type' => 'done',
  6729. 'script' => [],
  6730. 'episode' => [],
  6731. 'answer' => $fullContent,
  6732. 'reasoning' => $fullReasoningContent,
  6733. 'usage' => $usage,
  6734. 'error' => '未生成剧本内容,请调整提示词再试'
  6735. ];
  6736. return;
  6737. }
  6738. }
  6739. if (isset($table_data['content']) && $table_data['content']) {
  6740. $chapters = $this->splitContent($table_data['content']);
  6741. $chapter_count = count($chapters);
  6742. if ($chapter_count > 0) {
  6743. $table_data['start_episode_sequence'] = 1;
  6744. $table_data['end_episode_sequence'] = $table_data['episode_num'] = $chapter_count;
  6745. }
  6746. }
  6747. }
  6748. if ($extra_products) {
  6749. $table_data['extra_products'] = json_encode($extra_products, 256);
  6750. }
  6751. $single_episode = [];
  6752. try {
  6753. DB::beginTransaction();
  6754. $now = date('Y-m-d H:i:s');
  6755. // 更新动漫大纲
  6756. $boolen = DB::table('mp_animes')->where('id', $anime_id)->update($table_data);
  6757. if (!$boolen) {
  6758. dLog('deepseek')->info('对话修改失败', $table_data);
  6759. logDB('deepseek', 'error', '对话修改失败', ['anime_id' => $anime_id]);
  6760. Utils::throwError('20003:对话修改失败');
  6761. }
  6762. // 保存对话记录
  6763. $records = [
  6764. [
  6765. 'uid' => $uid,
  6766. 'anime_id' => $anime_id,
  6767. 'sequence' => 0,
  6768. 'role' => 'user',
  6769. 'content' => $prompt,
  6770. 'created_at' => $now,
  6771. 'updated_at' => $now
  6772. ],
  6773. [
  6774. 'uid' => $uid,
  6775. 'anime_id' => $anime_id,
  6776. 'sequence' => 0,
  6777. 'role' => 'assistant',
  6778. // 'content' => '我已按照您的要求完成策划并将内容更新到您右侧的策划文档。',
  6779. 'content' => $fullContent,
  6780. 'created_at' => $now,
  6781. 'updated_at' => $now
  6782. ]
  6783. ];
  6784. // 保存对话记录
  6785. $boolen3 = DB::table('mp_anime_records')->insert($records);
  6786. if (!$boolen3) {
  6787. Utils::throwError('20003:对话记录保存失败');
  6788. }
  6789. // 单剧集模式:生成并保存剧集信息
  6790. if ($is_single) {
  6791. $anime_name = getProp($anime, 'anime_name', '未命名');
  6792. $episode_arr = $this->buildSingleEpisodeFromScript($script_arr, $fullContent, $anime_name);
  6793. if (empty($episode_arr['acts'])) {
  6794. Utils::throwError('20003:单剧集分镜内容解析失败,请确认输出格式');
  6795. }
  6796. $saveResult = $this->saveEpisodeVersionData(
  6797. $anime_id,
  6798. 1,
  6799. $episode_arr,
  6800. is_array(getProp($script_arr, 'roles')) ? getProp($script_arr, 'roles') : [],
  6801. is_array(getProp($script_arr, 'scenes')) ? getProp($script_arr, 'scenes') : [],
  6802. is_array(getProp($script_arr, 'props')) ? getProp($script_arr, 'props') : [],
  6803. null,
  6804. null,
  6805. false,
  6806. $content,
  6807. $now,
  6808. $ref_products
  6809. );
  6810. $single_episode = $saveResult['episode'];
  6811. $episode_id = $saveResult['episode_id'];
  6812. $boolen5 = DB::table('mp_animes')->where('id', $anime_id)->update([
  6813. 'roles' => json_encode($saveResult['merged_roles'], 256),
  6814. 'scenes' => json_encode($saveResult['merged_scenes'], 256),
  6815. 'updated_at' => $now
  6816. ]);
  6817. if ($boolen5 === false) {
  6818. Utils::throwError('20003:单剧集主表资源同步失败');
  6819. }
  6820. $table_data['roles'] = json_encode($saveResult['merged_roles'], 256);
  6821. $table_data['scenes'] = json_encode($saveResult['merged_scenes'], 256);
  6822. $episode_record_content = '确认分镜大纲';
  6823. if ($content !== '') {
  6824. // $episode_record_content .= "\n\n###上传内容\n" . $content;
  6825. }
  6826. $episode_records = [
  6827. [
  6828. 'uid' => $uid,
  6829. 'anime_id' => $anime_id,
  6830. 'role' => 'user',
  6831. 'content' => $episode_record_content,
  6832. 'sequence' => 1,
  6833. 'episode_id' => $episode_id,
  6834. 'created_at' => $now,
  6835. 'updated_at' => $now
  6836. ],
  6837. [
  6838. 'uid' => $uid,
  6839. 'anime_id' => $anime_id,
  6840. 'role' => 'assistant',
  6841. 'content' => $fullContent,
  6842. // 'content' => '我已按照您的要求完成策划并将内容更新到您右侧的策划文档。',
  6843. 'sequence' => 1,
  6844. 'episode_id' => $episode_id,
  6845. 'created_at' => $now,
  6846. 'updated_at' => $now
  6847. ]
  6848. ];
  6849. $boolen4 = DB::table('mp_anime_records')->insert($episode_records);
  6850. if (!$boolen4) {
  6851. Utils::throwError('20003:单剧集分镜记录保存失败');
  6852. }
  6853. }
  6854. }catch (\Exception $e) {
  6855. DB::rollBack();
  6856. dLog('deepseek')->info('修改对话错误信息: '.$e->getMessage());
  6857. logDB('deepseek', 'error', '修改对话失败', ['error' => $e->getMessage(), 'anime_id' => $anime_id]);
  6858. yield [
  6859. 'type' => 'done',
  6860. 'answer' => $fullContent,
  6861. 'reasoning' => $fullReasoningContent,
  6862. 'usage' => $usage,
  6863. 'error' => $e->getMessage(),
  6864. ];
  6865. return;
  6866. }
  6867. DB::commit();
  6868. dLog('deepseek')->info('保存完毕');
  6869. if ($is_single && !empty($single_episode)) {
  6870. // $this->batchSetGlobalRoleImg([
  6871. // 'anime_id' => $anime_id,
  6872. // ]);
  6873. // $this->batchSetGlobalSceneImg([
  6874. // 'anime_id' => $anime_id,
  6875. // ]);
  6876. // // 将全局角色和场景的task_id、task_status和url继承给当前分集
  6877. // $this->inheritGlobalImagesToEpisodes($anime_id, $episode_id);
  6878. }
  6879. $table_data['anime_id'] = $anime_id;
  6880. $table_data['roles'] = json_decode($table_data['roles'], true);
  6881. $table_data['scenes'] = json_decode($table_data['scenes'], true);
  6882. unset($table_data['updated_at']);
  6883. unset($table_data['status']);
  6884. yield [
  6885. 'type' => 'done',
  6886. 'script' => $table_data,
  6887. 'episode' => $single_episode,
  6888. 'answer' => $fullContent,
  6889. 'reasoning' => $fullReasoningContent,
  6890. 'usage' => $usage
  6891. ];
  6892. }
  6893. public function chatForAce($data) {
  6894. $uid = Site::getUid();
  6895. $anime_id = getProp($data, 'anime_id');
  6896. $file = getProp($data, 'file');
  6897. $content = getProp($data, 'content', '');
  6898. $bid = getProp($data, 'bid', 0);
  6899. $script_id = getProp($data, 'script_id', 0);
  6900. $episode_number = getProp($data, 'episode_number', 1);
  6901. $prompt = trim((string)getProp($data, 'prompt', '确认分镜大纲'));
  6902. $anime = DB::table('mp_animes')->where('id', $anime_id)->first();
  6903. if (!$anime) Utils::throwError('20003:该对话不存在!');
  6904. if ((int)getProp($anime, 'is_multi') !== 1 && (int)$episode_number !== 1) Utils::throwError('20003:单剧集不支持其他剧集序号');
  6905. $current_episode = DB::table('mp_anime_episodes')->where('anime_id', $anime_id)->where('episode_number', $episode_number)->where('is_default', 1)->first();
  6906. $base_episode = $current_episode ?: DB::table('mp_anime_episodes')->where('anime_id', $anime_id)->where('episode_number', $episode_number)->orderByDesc('id')->first();
  6907. $roles = $base_episode ? json_decode((string)getProp($base_episode, 'roles'), true) : json_decode((string)getProp($anime, 'roles'), true);
  6908. $scenes = $base_episode ? json_decode((string)getProp($base_episode, 'scenes'), true) : json_decode((string)getProp($anime, 'scenes'), true);
  6909. $roles = is_array($roles) ? $roles : [];
  6910. $scenes = is_array($scenes) ? $scenes : [];
  6911. $extra_products = getProp($data, 'products', []);
  6912. if (is_string($extra_products)) $extra_products = json_decode($extra_products, true);
  6913. // 如果anime有关联的script_id,则从mp_script_product_mappings中查询资产并合并到extra_products
  6914. $anime_script_id = getProp($anime, 'script_id');
  6915. if ($anime_script_id) {
  6916. $cpid = Site::getCpid();
  6917. $script_products_mappings = DB::table('mp_script_product_mappings as mapping')
  6918. ->join('mp_products as product', 'mapping.product_id', '=', 'product.id')
  6919. ->where('mapping.script_id', $anime_script_id)
  6920. ->where('product.cpid', $cpid)
  6921. ->where('product.is_deleted', 0);
  6922. // 如果提供了 episode_number,则过滤指定集数
  6923. if ($episode_number !== null && $episode_number !== '') {
  6924. $script_products_mappings->where('mapping.episode_number', $episode_number);
  6925. }
  6926. $mappings = $script_products_mappings->select(
  6927. 'mapping.product_id',
  6928. 'mapping.episode_number',
  6929. 'product.product_name',
  6930. 'product.type',
  6931. 'product.product',
  6932. 'product.pic_prompt',
  6933. 'product.url',
  6934. 'product.pic_task_id',
  6935. 'product.pic_task_status',
  6936. 'product.three_view_image_url'
  6937. )
  6938. ->get();
  6939. // 按 product_id 分组,合并 episode_number
  6940. $productMap = [];
  6941. foreach ($mappings as $item) {
  6942. $product_id = $item->product_id;
  6943. $product_name = $item->product_name;
  6944. // 处理product_name两边的符号
  6945. $product_name = preg_replace('/^[\s*\[\]【】[]{}{}\x{3000}]+|[\s*\[\]【】[]{}{}\x{3000}]+$/u', '', $product_name);
  6946. if (!isset($productMap[$product_id])) {
  6947. $productMap[$product_id] = [
  6948. 'product_id' => $product_id,
  6949. 'product_name' => $product_name,
  6950. 'type' => (int)$item->type,
  6951. 'product' => (int)$item->product,
  6952. 'pic_prompt' => $item->pic_prompt,
  6953. 'url' => $item->url,
  6954. 'pic_task_id' => $item->pic_task_id,
  6955. 'pic_task_status' => $item->pic_task_status,
  6956. 'episode_numbers' => [],
  6957. ];
  6958. }
  6959. // 添加 episode_number(去重)
  6960. if (!in_array($item->episode_number, $productMap[$product_id]['episode_numbers'])) {
  6961. $productMap[$product_id]['episode_numbers'][] = $item->episode_number;
  6962. }
  6963. }
  6964. // 将查询到的剧本资产合并到extra_products中
  6965. // 先构建extra_products的索引(以product_id+episode_number为key)
  6966. $extraProductsMap = [];
  6967. foreach ($extra_products as $item) {
  6968. $key = getProp($item, 'product_id') . '_' . getProp($item, 'episode_number', '');
  6969. $extraProductsMap[$key] = $item;
  6970. }
  6971. // 合并剧本资产(去重:product_id+episode_number)
  6972. foreach ($productMap as $product) {
  6973. foreach ($product['episode_numbers'] as $ep_num) {
  6974. $key = $product['product_id'] . '_' . $ep_num;
  6975. // 如果不存在则添加
  6976. if (!isset($extraProductsMap[$key])) {
  6977. $extraProductsMap[$key] = [
  6978. 'product_id' => $product['product_id'],
  6979. 'product_name' => $product['product_name'],
  6980. 'type' => $product['type'],
  6981. 'product' => $product['product'],
  6982. 'pic_prompt' => $product['pic_prompt'],
  6983. 'url' => $product['url'],
  6984. 'pic_task_id' => $product['pic_task_id'],
  6985. 'pic_task_status' => $product['pic_task_status'],
  6986. 'episode_number' => $ep_num,
  6987. ];
  6988. }
  6989. }
  6990. }
  6991. // 重新赋值给extra_products
  6992. $extra_products = array_values($extraProductsMap);
  6993. }
  6994. // 处理提示词
  6995. if ($prompt && $extra_products) {
  6996. // 遍历extra_products,将prompt中的{product_name}替换为实际的product_name
  6997. foreach($extra_products as $item) {
  6998. $product_name = getProp($item, 'product_name');
  6999. if ($product_name) {
  7000. // 替换 {product_name} 为 product_name
  7001. $prompt = str_replace('{' . $product_name . '}', $product_name, $prompt);
  7002. }
  7003. }
  7004. }
  7005. // 获取最后一集序号
  7006. $end_episode_sequence = getProp($anime, 'end_episode_sequence');
  7007. if ($end_episode_sequence && $episode_number > $end_episode_sequence) Utils::throwError("20003:总集数只有{$end_episode_sequence}集,不允许继续策划下一集!");
  7008. // 转换主体列表和场景列表的格式
  7009. // 获取参考主体或场景
  7010. if ((int)$episode_number === 1) {
  7011. $ref_data = DB::table('mp_animes')->where('id', $anime_id)->first();
  7012. // $ref_data = []; // 第一集让AI自由发挥不再使用全局主体(会影响后续分集的继承问题)
  7013. }else {
  7014. $prev_episode_number = $episode_number - 1;
  7015. $ref_data = DB::table('mp_anime_episodes')->where('anime_id', $anime_id)->where('episode_number', $prev_episode_number)->where('is_default', 1)->first();
  7016. }
  7017. $ref_roles = getProp($ref_data, 'roles') ? json_decode(getProp($ref_data, 'roles'), true) : [];
  7018. $ref_scenes = getProp($ref_data, 'scenes') ? json_decode(getProp($ref_data, 'scenes'), true) : [];
  7019. $ref_props = getProp($ref_data, 'props') ? json_decode(getProp($ref_data, 'props'), true) : [];
  7020. // 合并anime_products和extra_products(extra_products优先,去重)
  7021. $anime_products = getProp($anime, 'extra_products');
  7022. $anime_products = $anime_products ? json_decode($anime_products, true) : [];
  7023. $ref_products = [];
  7024. foreach($anime_products as $item) {
  7025. $product_name = getProp($item, 'product_name');
  7026. $ref_products[$product_name] = $item;
  7027. }
  7028. // 处理extra_products,将其合并到ref_roles和ref_scenes中(入参优先)
  7029. $extra_roles = []; // 从extra_products中提取的角色
  7030. $extra_scenes = []; // 从extra_products中提取的场景
  7031. $extra_props = []; // 从extra_products中提取的道具
  7032. if ($extra_products && is_array($extra_products)) {
  7033. foreach($extra_products as $item) {
  7034. $product = (int)getProp($item, 'product');
  7035. $product_name = getProp($item, 'product_name');
  7036. if ($product_name == '旁白') continue;
  7037. $pic_prompt = getProp($item, 'pic_prompt');
  7038. $ref_products[$product_name] = $item;
  7039. if ($product === 1) {
  7040. // 角色
  7041. $extra_roles[$product_name] = [
  7042. 'role' => $product_name,
  7043. 'pic_prompt' => $pic_prompt,
  7044. 'url' => getProp($item, 'url', ''),
  7045. ];
  7046. } elseif ($product === 2) {
  7047. // 场景
  7048. $extra_scenes[$product_name] = [
  7049. 'scene' => $product_name,
  7050. 'pic_prompt' => $pic_prompt,
  7051. 'url' => getProp($item, 'url', ''),
  7052. ];
  7053. } elseif ($product === 3) {
  7054. // 道具
  7055. $extra_props[$product_name] = [
  7056. 'prop' => $product_name,
  7057. 'pic_prompt' => $pic_prompt,
  7058. 'url' => getProp($item, 'url', ''),
  7059. ];
  7060. }
  7061. }
  7062. }
  7063. // 合并ref_roles和extra_roles(extra_roles优先,去重)
  7064. $merged_roles = [];
  7065. foreach($ref_roles as $role) {
  7066. $role_name = getProp($role, 'role');
  7067. if (!isset($extra_roles[$role_name])) {
  7068. $merged_roles[$role_name] = $role;
  7069. }
  7070. }
  7071. // 添加extra_roles
  7072. foreach($extra_roles as $role_name => $role) {
  7073. $merged_roles[$role_name] = $role;
  7074. }
  7075. $ref_roles = array_values($merged_roles);
  7076. // 合并ref_scenes和extra_scenes(extra_scenes优先,去重)
  7077. $merged_scenes = [];
  7078. foreach($ref_scenes as $scene) {
  7079. $scene_name = getProp($scene, 'scene');
  7080. if (!isset($extra_scenes[$scene_name])) {
  7081. $merged_scenes[$scene_name] = $scene;
  7082. }
  7083. }
  7084. // 添加extra_scenes
  7085. foreach($extra_scenes as $scene_name => $scene) {
  7086. $merged_scenes[$scene_name] = $scene;
  7087. }
  7088. $ref_scenes = array_values($merged_scenes);
  7089. // 合并ref_props和extra_props(extra_props优先,去重)
  7090. $merged_props = [];
  7091. foreach($ref_props as $prop) {
  7092. $prop_name = getProp($prop, 'prop');
  7093. if (!isset($extra_props[$prop_name])) {
  7094. $merged_props[$prop_name] = $prop;
  7095. }
  7096. }
  7097. // 添加extra_props
  7098. foreach($extra_props as $prop_name => $prop) {
  7099. $merged_props[$prop_name] = $prop;
  7100. }
  7101. $ref_props = array_values($merged_props);
  7102. $roles_content = $this->buildEpisodeItemContent($ref_roles, 'role');
  7103. $scenes_content = $this->buildEpisodeItemContent($ref_scenes, 'scene');
  7104. $props_content = $this->buildEpisodeItemContent($ref_props, 'prop');
  7105. // 构建强制主体、场景和道具提示词
  7106. $forced_roles_prompt = "";
  7107. $forced_scenes_prompt = "";
  7108. $forced_props_prompt = "";
  7109. if (!empty($extra_roles)) {
  7110. $forced_roles_list = "";
  7111. foreach($extra_roles as $role_name => $role) {
  7112. $forced_roles_list .= $role_name . ":{" . getProp($role, 'pic_prompt') . "}\n";
  7113. }
  7114. $forced_roles_prompt = "\n\n【强制主体设定-优先级最高-不可修改】\n以下主体必须全部出现在<主体列表>中,这些主体的优先级最高,必须包含:\n" . $forced_roles_list . "\n格式要求:\n1. 主体名称:必须保持不变\n2. 主体描述:你必须根据剧情为每个主体生成主体描述(分析人物在整部剧中的姓名、年龄、性格特征、社会身份或职业背景、个人成长经历或核心故事动机,以及在剧情中的目标与行为驱动力,用一句话进行总结)\n3. 人物提示词:{}中的内容必须完整保留,不得修改\n4. 音色提示词:你必须根据角色特征生成音色提示词(描述角色的整体音色特征,包含:性别、年龄、音质、表达形式、情绪氛围,字数控制在80字以内),用{{}}框起来\n\n最终格式必须为:主体名:[你生成的主体描述]{人物提示词-保持不变}{{你生成的音色提示词}}\n\n【重要】以上每个主体在<主体列表>中的完整格式必须是:主体名:主体描述{人物提示词}{{音色提示词}},其中主体描述和音色提示词是你必须生成的内容,{}中的人物提示词必须原样保留。";
  7115. }
  7116. if (!empty($extra_scenes)) {
  7117. $forced_scenes_list = "";
  7118. foreach($extra_scenes as $scene_name => $scene) {
  7119. $forced_scenes_list .= $scene_name . ":{" . getProp($scene, 'pic_prompt') . "}\n";
  7120. }
  7121. $forced_scenes_prompt = "\n\n【强制场景设定-优先级最高-不可修改】\n以下场景必须全部出现在<场景列表>中,这些场景的优先级最高,必须包含:\n" . $forced_scenes_list . "\n格式要求:\n1. 场景名称:必须保持不变\n2. 场景描述:你必须根据剧情为每个场景生成场景描述(分析在整部剧中出现过的场景,场景描述尽量简洁贴合原文)\n3. 场景提示词:{}中的内容必须完整保留,不得修改\n\n最终格式必须为:场景名:[你生成的场景描述]{场景提示词-保持不变}\n\n【重要】以上每个场景在<场景列表>中的完整格式必须是:场景名:场景描述{场景提示词},其中场景描述是你必须生成的内容,{}中的场景提示词必须原样保留。";
  7122. }
  7123. if (!empty($extra_props)) {
  7124. $forced_props_list = "";
  7125. foreach($extra_props as $prop_name => $prop) {
  7126. $forced_props_list .= $prop_name . ":{" . getProp($prop, 'pic_prompt') . "}\n";
  7127. }
  7128. $forced_props_prompt = "\n\n【强制道具设定-优先级最高-不可修改】\n以下道具必须全部出现在<道具列表>中,这些道具的优先级最高,必须包含:\n" . $forced_props_list . "\n格式要求:\n1. 道具名称:必须保持不变\n2. 道具描述:你必须根据剧情为每个道具生成道具描述(分析在整部剧中出现过的道具,道具描述尽量简洁贴合原文)\n3. 道具提示词:{}中的内容必须完整保留,不得修改\n\n最终格式必须为:道具名:[你生成的道具描述]{道具提示词-保持不变}\n\n【重要】以上每个道具在<道具列表>中的完整格式必须是:道具名:道具描述{道具提示词},其中道具描述是你必须生成的内容,{}中的道具提示词必须原样保留。";
  7129. }
  7130. if ($anime_script_id) {
  7131. $role_demo = "2.<主体列表>只能使用以下主体,不得自定义内容(必需带上冒号后的具体描述以及{}和{{}}中的内容,原样输出)。{$forced_roles_prompt}";
  7132. }else if ($roles_content) {
  7133. $role_demo = "2.<主体列表>可在以下主体中选择多个(根据拆分出的分镜剧本来确认具体几个,不要出现旁白角色),不得自定义内容(必需带上冒号后的具体描述以及{}和{{}}中的内容,原样输出)。
  7134. 主体范围:\n {$roles_content}\n
  7135. 2.1如果本集剧情内容中出现了和给出的<主体列表>中不存在的新主体(或原主体发生了比较大的年龄、服装或身体变化),则不用在上述主体中选择,而是按照相同的格式新增一个主体显示在<主体列表>中。(以下是格式要求和生成要求,仅适用新出现的主体):
  7136. - 新主体分以下三部分:主体描述、人物提示词和音色提示词,格式必须为:主体名:主体描述{人物提示词}{{音色提示词}}
  7137. - 新主体的主体描述是分析人物在整部剧中的姓名、年龄、性格特征、社会身份或职业背景、个人成长经历或核心故事动机,以及在剧情中的目标与行为驱动力,不需要描述外貌、穿着或身体特征,重点突出心理结构、行为模式与叙事功能(缩略成一句话讲述完毕)。
  7138. - 新主体的人物提示词需要根据当前分析出来的美术风格生成(每个人物都只允许一套服装,如果多套服装则需区分主体名),包含性别(男、女)、大概年龄或年龄段(少年、中年、老年等)、人种(亚洲人、欧洲人)、发型(直发卷发等)、发色(黑色、棕色、红色、白色等)、整体视觉风格,以及详细的穿着风格和服装细节、面部特征(戴金丝眼镜、脸上有刀疤等,无面部特征可忽略)。描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合人物身份设定。该提示词(字数50-100字左右)在主体描述后用{}框起来。
  7139. - 新主体的音色提示词放在描述的最后用{{}}框起来。音色提示词需描述角色的整体音色特征,包含:性别、年龄(约XX岁)、音质(如清冷、沉稳、磁性等)、表达形式(如叙述感、旁白式等)、情绪氛围(如空灵韵味、看透世事的清冷等),字数控制在80字以内。举例:成年女性(约25-35岁),音色清冷且富有叙述感,语速平缓,带有仙侠剧特有的空灵韵味。{$forced_roles_prompt}";
  7140. }else {
  7141. $role_demo = "2.<主体列表>中出现的“主体名”必须尽可能统一,严禁出现角色-科学家、角色-复仇者、角色-领导者等带有身份、职业、标签类的命名,只能使用服装差异(如校服装、卫衣装、西装)来区分同一角色,如角色-校服装等。
  7142. - <主体列表>分以下三部分:主体描述、人物提示词和音色提示词,格式必须为:主体名:主体描述{人物提示词}{{音色提示词}}
  7143. - <主体列表>的主体描述是分析人物在整部剧中的姓名、年龄、性格特征、社会身份或职业背景、个人成长经历或核心故事动机,以及在剧情中的目标与行为驱动力,不需要描述外貌、穿着或身体特征,重点突出心理结构、行为模式与叙事功能(缩略成一句话讲述完毕)。
  7144. - <主体列表>的人物提示词需要根据当前分析出来的美术风格生成(每个人物都只允许一套服装,如果多套服装则需区分主体名),包含性别(男、女)、大概年龄或年龄段(少年、中年、老年等)、人种(亚洲人、欧洲人)、发型(直发卷发等)、发色(黑色、棕色、红色、白色等)、整体视觉风格,以及详细的穿着风格和服装细节、面部特征(戴金丝眼镜、脸上有刀疤等,无面部特征可忽略)。描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合人物身份设定。该提示词(字数50-100字左右)在主体描述后用{}框起来。
  7145. - <主体列表>的音色提示词放在描述的最后用{{}}框起来。音色提示词需描述角色的整体音色特征,包含:性别、年龄(约XX岁)、音质(如清冷、沉稳、磁性等)、表达形式(如叙述感、旁白式等)、情绪氛围(如空灵韵味、看透世事的清冷等),字数控制在80字以内。举例:成年女性(约25-35岁),音色清冷且富有叙述感,语速平缓,带有仙侠剧特有的空灵韵味。{$forced_roles_prompt}";
  7146. }
  7147. if ($anime_script_id) {
  7148. $scene_demo = "3.<场景列表>只能使用以下场景,不得自定义内容(必需带上冒号后的具体描述以及{}和{{}}中的内容,原样输出)。{$forced_scenes_prompt}";
  7149. }else if ($scenes_content) {
  7150. $scene_demo = "3.<场景列表>可在以下场景中选择多个(需带上冒号后的具体描述),根据拆分出的分镜剧本来确认具体几个,不得自定义(必需带上冒号后的具体描述以及{}中的内容,原样输出)。
  7151. 场景范围:\n {$scenes_content}\n
  7152. 3.1如果本集剧情内容中出现了和给出的<场景列表>中不存在的新场景,则不用在上述场景中选择,而是按照相同的格式新增一个场景显示在<场景列表>中,必须严格注意不要出现任何角色和人物。(以下是格式要求和生成要求,仅适用新出现的场景):。
  7153. - 新场景分以下两部分:场景描述和场景提示词,格式必须为:场景名:场景描述{场景提示词}
  7154. - 新场景的场景描述是分析在整部剧中出现过的场景,场景描述尽量简洁贴合原文。
  7155. - 新场景的场景提示词用于视觉呈现的场景环境描述,该场景需包含环境氛围、光线效果、空间布局、色彩基调、视觉风格等,描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合剧情设定(可参考以下示例的场景列表中使用{}框起来的提示词,字数150-200字,需带上提炼出来的美术风格)。该提示词在场景描述后用{}框起来。{$forced_scenes_prompt}\n";
  7156. }else {
  7157. $scene_demo = "3.<场景列表>与主体列表同理,需尽可能的对场景环境进行详细描述,必须严格注意不要出现任何角色和人物。
  7158. - <场景列表>分以下两部分:场景描述和场景提示词,格式必须为:场景名:场景描述{场景提示词},注意不要出现{{}}这样的双大括号。
  7159. - <场景列表>的场景描述是分析在整部剧中出现过的场景,场景描述尽量简洁贴合原文。
  7160. - <场景列表>的场景提示词用于视觉呈现的场景环境描述,该场景需包含环境氛围、光线效果、空间布局、色彩基调、视觉风格等,描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合剧情设定(可参考以下示例的场景列表中使用{}框起来的提示词,字数150-200字,需带上提炼出来的美术风格)。该提示词在场景描述后用{}框起来。{$forced_scenes_prompt}\n";
  7161. }
  7162. if ($anime_script_id) {
  7163. $prop_demo = "4.<道具列表>只能使用以下道具,不得自定义内容(必需带上冒号后的具体描述以及{}和{{}}中的内容,原样输出)。{$forced_props_prompt}";
  7164. }else if ($props_content) {
  7165. $prop_demo = "4.<道具列表>可在以下道具中选择多个(需带上冒号后的具体描述),根据拆分出的分镜剧本来确认具体几个,不得自定义(必需带上冒号后的具体描述以及{}中的内容,原样输出)。
  7166. 道具范围:\n {$props_content}\n
  7167. 4.1如果本集剧情内容中出现了和给出的<道具列表>中不存在的新道具,则不用在上述道具中选择,而是按照相同的格式新增一个道具显示在<道具列表>中,必须严格注意不要出现任何角色和人物。(以下是格式要求和生成要求,仅适用新出现的道具):。
  7168. - 新道具分以下两部分:道具描述和道具提示词,格式必须为:道具名:道具描述{道具提示词}
  7169. - 新道具的道具描述是分析在整部剧中出现过的道具,道具描述尽量简洁贴合原文。
  7170. - 新道具的道具提示词用于视觉呈现的道具环境描述,该道具需包含环境氛围、光线效果、空间布局、色彩基调、视觉风格等,描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合剧情设定(可参考以下示例的道具列表中使用{}框起来的提示词,字数150-200字,需带上提炼出来的美术风格)。该提示词在道具描述后用{}框起来。{$forced_scenes_prompt}\n";
  7171. }else {
  7172. $prop_demo = "4.<道具列表>与主体列表同理,需尽可能的对道具环境进行详细描述,必须严格注意不要出现任何角色和人物。
  7173. - <道具列表>分以下两部分:道具描述和道具提示词,格式必须为:道具名:道具描述{道具提示词},注意不要出现{{}}这样的双大括号。
  7174. - <道具列表>的道具描述是分析在整部剧中出现过的道具,道具描述尽量简洁贴合原文。
  7175. - <道具列表>的道具提示词用于视觉呈现的道具环境描述,该道具需包含环境氛围、光线效果、空间布局、色彩基调、视觉风格等,描述需具有镜头感与画面表现力,适合直接用于AI图像生成模型,避免抽象词汇,强调具体可视化细节与整体视觉一致性,同时保证风格统一且符合剧情设定(可参考以下示例的道具列表中使用{}框起来的提示词,字数150-200字,需带上提炼出来的美术风格)。该提示词在道具描述后用{}框起来。{$forced_scenes_prompt}\n";
  7176. }
  7177. // 提取文件内容
  7178. if ($file) {
  7179. $uploaded_content = $this->extractFileContent($file);
  7180. if (!$uploaded_content) {
  7181. Utils::throwError('20003:无法提取文件内容,请检查文件格式');
  7182. }
  7183. } elseif ($script_id) {
  7184. $uploaded_content = $this->getContentByScriptId($script_id);
  7185. if (!$uploaded_content) {
  7186. Utils::throwError('20003:无法获取剧本内容,请检查剧本');
  7187. }
  7188. } elseif ($bid) {
  7189. $uploaded_content = $this->getContentByBid($bid);
  7190. if (!$uploaded_content) {
  7191. Utils::throwError('20003:无法获取书籍内容,请检查书籍');
  7192. }
  7193. } elseif ($content) {
  7194. $uploaded_content = filterContent((string)getProp($data, 'content', ''));
  7195. }
  7196. // elseif ($prompt) {
  7197. // $uploaded_content = filterContent($prompt);
  7198. // }
  7199. else {
  7200. $uploaded_content = '';
  7201. }
  7202. // 美术风格
  7203. $input_art_style = getProp($data, 'art_style');
  7204. if (!$input_art_style) {
  7205. $mappedArtStyle = getProp($anime, 'art_style');
  7206. }else {
  7207. $mappedArtStyle = $this->getArtStylePromptByInput($input_art_style);
  7208. }
  7209. if (!$mappedArtStyle) {
  7210. $mappedArtStyle_prompt = '美术风格:根据剧本内容自定义美术风格';
  7211. }else {
  7212. if (strstr($mappedArtStyle, '基础画风风格词')) {
  7213. $mappedArtStyle_prompt = "美术风格强制设置如下(原样输出,不得更改任何内容):\n{$mappedArtStyle}\n";
  7214. }else {
  7215. $art_style_type = $input_art_style ? $input_art_style : getProp($anime, 'art_style_type');
  7216. $mappedArtStyle_prompt = "美术风格强制设置如下(原样输出,不得更改任何内容):\n基础画风风格词: {$art_style_type}\n{$mappedArtStyle}\n";
  7217. }
  7218. }
  7219. $prompt = trim((string)getProp($data, 'prompt', '确认分镜大纲'));
  7220. // $demo = "你是一个专业的文档分析助手及资深编剧,请根据用户提供的文档内容完成示例格式的内容输出(需包含以下几个板块: <剧集标题><故事梗概><美术风格><旁白音色提示词><主体列表><场景列表><分段剧本>,每个板块之间必须使用###分隔,#不能多也不能少;同时板块之间需满足以下要求:\n
  7221. // 强制要求:
  7222. // 1.你只负责与剧本相关的内容,如果用户输入与剧本无关的内容,请告知用户并拒绝继续回答,此为第一优先原则,不满足此原则就忽略其他要求并停止继续输出
  7223. // 2.如果用户输入修改美术风格、修改画风这类内容,请告知用户不可修改美术风格,并拒绝继续回答
  7224. // 3.以上两条要求对修改主体角色或场景不生效,允许用户调整主体角色说明和场景描述等
  7225. // 4.用户如果要求你帮忙写一个故事或者剧本,此类要求不属于与剧本无关,请帮助用户完成请求
  7226. // 5.强制按照示例格式返回内容,不要更改格式或随意增加符号
  7227. // 6.如果给出的本集剧情内容是剧本格式,则不要改动剧本内容(必须强制使用原文内容),严格按照剧本的旁白和台词进行分镜的策划,不要跳过任何剧情
  7228. // 7.分镜不是二次创作,必须严格“原文拆句”。优先把原文中可直接成镜头的句子拆成分镜,禁止把两句合并后再脑补过渡。
  7229. // 8.画面描述必须直接使用,禁止新增任何原文没有写出的动作、人物、道具、地点、气氛、结果或因果。
  7230. // 9.不要把“打开衣柜”扩写成“伸手轻抚嫁衣”,不要把“管家拎着两个箱子跟在后面”改成“沈耀和管家一起拎箱子”。主语、动作、数量、先后顺序必须完全按原文。
  7231. // 10.分镜要和场景列表、人物主体保持一致\n
  7232. // 普通要求:
  7233. // 1.剧集标题必须与文档内容高度相关,剧集序号是{$episode_number}。
  7234. // {$role_demo}
  7235. // {$scene_demo}
  7236. // 4.<分段剧本>按照“分段模式”进行拆分和组织,要求如下:
  7237. // 4.1片段分割逻辑(优先级从高到低):
  7238. // - 第一优先级:时长上限(单个片段不超过15秒,超过必须切为新片段)
  7239. // - 第二优先级:叙事节拍Beat(一个完整的事件/动作/情绪起落单元,如一段对话的来回、一个动作序列:推门→看见→震惊、一次闪回/回忆、旁白+画面组合)
  7240. // - 第三优先级:语义块(按“谁、在哪、做什么、情绪怎样”聚合,如同一段旁白/独白、同一个角色的连续动作、同一个视角的连续镜头)
  7241. // - 只要事件没结束、情绪没断,哪怕换场景,也留在同一个片段里
  7242. // 4.2片段结构:片段数量+旁白音色+多个片段(每个片段由多个分镜组合而成(片段序号格式:片段1、片段2...))
  7243. // - 片段数量格式为: 片段数量:8
  7244. // - 旁白音色需描述旁白的整体音色特征,包含:性别、年龄(约XX岁)、音质(如清冷、沉稳、磁性等)、表达形式(如叙述感、旁白式等)、情绪氛围(如空灵韵味、看透世事的清冷等),字数控制在80字以内。举例:成年女性(约25-35岁),音色清冷且富有叙述感,语速平缓,带有仙侠剧特有的空灵韵味。
  7245. // - 片段序号格式必须如下: ##片段+片段序号\n时长:xxs,例如: ##片段1\n时长:12s
  7246. // 4.3分镜结构:每个分镜包含以下字段:
  7247. // - 分镜序号在片段内用1、2、3显示,如分镜1、分镜2,并且分镜序号需连续(跨片段中的分镜序号需连续,如片段2中的分镜序号从片段1的最大序号+1开始)
  7248. // - 场景:必须选择本集中的<场景列表>中的一个(不带详细表述)
  7249. // - 画面:【构图/视角】+画面描述(越简单越好,对视觉场景/实景的描述,不描述氛围,比如紧张、压抑的氛围)。举例:[近景特写,浅景深]季潇握着手机贴在耳边,指关节因为用力而泛白。她的眼眶微红,睫毛在狂风中颤抖,嘴唇紧抿,眼神中闪烁着最后一点希冀的微光。
  7250. // - 画面中如果出现主体,只能选择上述主体列表中的一个,主体的名称也需要保持一致(不带主体具体描述)
  7251. // - 运镜:场景+画面描述+运镜
  7252. // - 配音台词:格式为“中文配音:[角色]台词”,角色配音台词和语言由模型匹配生成,角色必须对应本集中的<主体列表>中的一个(非对话内容则统一固定为“旁白”角色)
  7253. // - 台词内容需满足以下要求: (1. 台词来源规则:台词内容必须逐字摘录自小说原文中的人物直接对话(带引号的内容),不允许改写、总结、压缩、扩写或拼接原文对白,不允许生成原文中不存在的对话。2. 非对话过滤:旁白、心理描写、环境描写、动作描写等内容,禁止出现在台词内容中。3. 空台词规则:如果该分镜中没有人物对话,则“台词内容”必须返回“无”,严禁输出“无台词”、“没有”等任何替代文本。4. 禁止推测:不得根据语境补全人物可能说的话。5.如果没有原文,直接返回空)
  7254. // - 背景音效:背景音效需根据内容和画面生成,贴合当前画面描述和动作,例如:背景音效:高跟鞋的脚本声
  7255. // 5.$mappedArtStyle_prompt\n\n
  7256. // 示例格式:\n
  7257. // 第1集:隐形的守护者
  7258. // ###故事梗概
  7259. // 内容梗概:许芸归家遭徐母毒打,她冷静周旋并编造谎言掩盖行踪,引得一旁阴沉的卞大伟愈发急躁。半个月后,徐母离家,卞大伟兽性大发欲行不轨。生死关头,隐形的程景现身肉搏,屋内家具横飞,卞大伟被“空气”重创。许芸趁乱逃出,却惊见地面凭空浮现一串血脚印。意识到守护者为救自己负伤,许芸不再逃避,对着虚无的空气执拗告白,誓要与这看不见的爱人共进退。
  7260. // ###美术风格
  7261. // 基础画风风格词:胡金铨武侠
  7262. // 视觉风格描述:整体采用复古武侠电影美学,以低饱和度的青灰与墨色调为主,营造冷峻、肃杀的江湖意境。光影上运用强烈的侧逆光与深邃硬阴影,完美契合“墨影剑客”神秘莫测的特质。画面带有35mm胶片的粗粝质感,构图简约且富有张力,展现出深沉的复仇叙事氛围。
  7263. // ###主体列表
  7264. // 陆沉:冷酷神秘的复仇剑客,陆家庄唯一幸存者,身负绝世武功与血海深仇。{全景,正面拍摄,青年男性,亚洲人,黑色长发束起,五官轮廓深邃。服饰穿搭:黑色连帽斗篷,深灰色交领劲装,黑色束腰,背负一柄漆黑宽大的无锋重剑。姿态:站立。}{{青年男性(约25-30岁),音色冷峻、低沉,语速平稳缓慢,透着一股拒人于千里之外的孤傲与肃杀。}}
  7265. // 刀疤脸:武林盟的嚣张爪牙,性格暴戾、欺软怕硬,是赵无极麾下的打手。{全景,正面拍摄,中年男性,亚洲人,短发,左脸有一道明显的深长刀疤。服饰穿搭:土黄色粗布武士服,腰间挂着一枚古铜色的“武林盟”令牌。姿态:站立。}{{中年男性(约35-45岁),音色粗犷、沙哑,语气嚣张跋扈,带有明显的威胁感和市井痞气。}}
  7266. // 老李:悦来酒馆的老板,胆小卑微的底层百姓,在乱世中艰难求生。{全景,正面拍摄,老年男性,亚洲人,灰白乱发,面容消瘦且布满皱纹。服饰穿搭:破旧的深蓝色粗布长袍,腰间系着一条带有污渍的灰色围裙。姿态:站立。}{{老年男性(约60岁以上),音色苍老、沙哑且带有颤抖,语气唯唯诺诺,充满了卑微与惊恐。}}
  7267. // ###场景列表
  7268. // 边境小镇街道:夜幕笼罩下的边陲小镇,风沙肆虐,街道两旁是破旧的木质建筑,透着一股荒凉与肃杀的气息,是故事发生的地理背景。{远景,夜幕下的边境小镇,破败的木质建筑错落有致,风沙在空旷的街道上飞扬,远处有微弱的灯火。无人物。}
  7269. // 悦来酒馆:酒馆内部陈设简陋,木质桌椅散乱分布,柜台上摆放着酒坛。室内光线昏暗,只有几盏摇曳的油灯提供微弱的暖光,与室外的冷调形成对比。{室内场景,昏暗的古风酒馆内部,粗糙的木质桌椅,木质柜台与酒坛,空气中浮动着尘埃,强烈的侧逆光勾勒出环境轮廓,深邃的硬阴影。无人物。}
  7270. // ###分段剧本
  7271. // 片段数量:8
  7272. // 旁白音色:成年男性(约35-50岁),音色厚重且富有磁性,带有老电影叙事般的沧桑感,语调低沉平稳,富有江湖气息。
  7273. // ##片段1
  7274. // 时长:12s
  7275. // 分镜1
  7276. // 场景:边境小镇街道
  7277. // 画面:[大远景,冷色调,高对比度] 漫天黄沙在狂风中如怒龙翻滚,遮蔽了微弱的星光。悦来酒馆残破的木门在风中剧烈摇晃,铁链撞击木板发出沉重的闷响,四周建筑尽显荒凉。
  7278. // 运镜:大远景固定镜头,随后风沙席卷向镜头形成遮挡转场。
  7279. // 配音台词:
  7280. // 背景音效:
  7281. // 分镜2
  7282. // 场景:悦来酒馆
  7283. // 画面:[全景,暖色调阴影构图] 酒馆内部昏暗,几盏油灯在风中疯狂摇曳,投射出扭曲的长影。角落里的食客低头缩颈,双手紧紧攥着粗瓷碗,空气中漂浮着细小的尘埃。
  7284. // 运镜:缓慢横移,掠过阴影中瑟缩的食客。
  7285. // 配音台词:
  7286. // 背景音效:
  7287. // 分镜3
  7288. // 场景:悦来酒馆
  7289. // 画面:[中景,三分法构图] 三名大汉围坐在正中央的圆桌旁,腰间挂着的“武林盟”金色令牌在灯火下闪烁。桌上残羹冷炙横陈,一名大汉正张狂地大笑,脚踩在长凳上。
  7290. // 运镜:从中景推向令牌特写。
  7291. // 配音台词:
  7292. // 背景音效:
  7293. // 分镜4
  7294. // 场景:悦来酒馆
  7295. // 画面:[特写,低角度仰拍] 刀疤脸横肉颤动,嘴角挂着轻蔑的笑。他随手倾覆手中的酒碗,琥珀色的液体在空中划过弧线,重重溅在老李打着补丁的布鞋和裤脚上。
  7296. // 运镜:极速推向酒水溅起的瞬间。
  7297. // 配音台词:
  7298. // 背景音效:
  7299. // 分镜5
  7300. // 镜头描述
  7301. // 场景:悦来酒馆
  7302. // 画面:[近景,荷兰角倾斜构图] 刀疤脸斜睨着眼,昏暗的灯光勾勒出他脸上狰狞的伤疤。他身体前倾,阴影完全笼罩了对面的老李,眼神如毒蛇般阴冷。
  7303. // 运镜:固定机位,利用倾斜构图制造压迫感。
  7304. // 配音台词:中文配音:[刀疤脸]老李头,这酒里掺水了吧?
  7305. // 背景音效:
  7306. // \n\n";
  7307. $demo = "你是一个国际顶尖的执行导演/编剧,熟悉各种风格的影视化落地,我需要你将这个剧本调整一下,将其变成一个高质量的影视分镜剧本(需包含以下几个板块: <剧集标题><故事梗概><美术风格><旁白音色提示词><主体列表><场景列表><道具列表><分段剧本>,每个板块之间必须使用###分隔,#不能多也不能少),要求画面和构图等进行高质量影视化改编,快节奏,改编的格式为先建立主体角色库、场景建库(其中场景建库的时候请分析故事发生的时候场景为白天还是晚上,建库的时候一起标注出来)以及关键的道具库。
  7308. 与此同时请将这个文本的情绪做一个分析,并且根据我下面提供的《情绪-视听语言映射表》进行一个文本情绪与视听语言的内容匹配。然后将每一个分镜按照「场景/场景时间(白天/晚上/傍晚等)」「画面」「运镜」「配音台词」「背景音效」的格式来划分,请按照每分钟25个左右的分镜的节奏帮我生成(这是一个整体性的标准,如果文本的情绪分析需要更多或者更少的镜头数量,请根据具体情况调整),分镜头请细腻的拆分文本中的内容,将动作、场景转换、事件的逻辑关系等内容清晰的交代出来,分镜之间需要呈现场景之间的转换逻辑,不要突然跳到下一个场景之中。于此同时保持镜头剪辑的顿挫感,节奏不要太平均。
  7309. 每一个分镜的「画面」请参考上一个分镜中人物的状态/动作和位置来写,保持人物状态以及在场景中的位置的一致性,不要断裂感,同时也不要直接在内容里写“参考上个分镜”这种话,因为视频模型无法识别,使用模型可以直接识别的文本内容。每一个分镜中需要出现的内容都请标出,例如出场角色,出场道具等等。请尽可能详细的描述画面内容。
  7310. 「背景音效」层面不要生成背景音乐,只需要生成音效和角色台词或者旁白,台词请先标注说台词的角色,旁白和角色心理活动时注明角色不要张嘴说话。\n
  7311. 普通要求:
  7312. 1.剧集标题必须与文档内容高度相关,剧集序号是{$episode_number}。
  7313. {$role_demo}
  7314. {$scene_demo}
  7315. {$prop_demo}
  7316. 5.<分段剧本>按照“分段模式”进行拆分和组织,要求如下:
  7317. 5.1片段分割逻辑(优先级从高到低):
  7318. - 第一优先级:时长上限(单个片段不超过15秒,超过必须切为新片段)
  7319. - 第二优先级:叙事节拍Beat(一个完整的事件/动作/情绪起落单元,如一段对话的来回、一个动作序列:推门→看见→震惊、一次闪回/回忆、旁白+画面组合)
  7320. - 第三优先级:语义块(按“谁、在哪、做什么、情绪怎样”聚合,如同一段旁白/独白、同一个角色的连续动作、同一个视角的连续镜头)
  7321. - 只要事件没结束、情绪没断,哪怕换场景,也留在同一个片段里
  7322. 5.2片段结构:片段数量+旁白音色+多个片段(每个片段由多个分镜组合而成(片段序号格式:片段1、片段2...))
  7323. - 片段数量格式为: 片段数量:8
  7324. - 旁白音色需描述旁白的整体音色特征,包含:性别、年龄(约XX岁)、音质(如清冷、沉稳、磁性等)、表达形式(如叙述感、旁白式等)、情绪氛围(如空灵韵味、看透世事的清冷等),字数控制在80字以内。举例:成年女性(约25-35岁),音色清冷且富有叙述感,语速平缓,带有仙侠剧特有的空灵韵味。
  7325. - 片段序号格式必须如下: ##片段+片段序号\n时长:xxs,例如: ##片段1\n时长:12s
  7326. 5.3分镜结构:每个分镜包含以下字段:
  7327. - 分镜序号在片段内用1、2、3显示,如分镜1、分镜2,并且分镜序号需连续(跨片段中的分镜序号需连续,如片段2中的分镜序号从片段1的最大序号+1开始),分镜序号前后不要带任何其他符号,特别是*符号。
  7328. - 出场角色: 如果有主体出现在本镜,则必须选择本集中的<主体列表>中的一个(如必须使用“沈秋岚-程家居家围裙造型”,而不是“沈秋岚”,不得缺少“-”前后和“:”前面的任何内容,不能自创)
  7329. - 场景:必须选择本集中的<场景列表>中的一个(如需使用“酒店会客室-景池筹备会版”,而不是“酒店会客室”,不得缺少“-”前后和“:”前面的任何内容,不能自创)
  7330. - 出场道具:如果有道具出现在本镜,则必须选择本集中的<道具列表>中的一个(如需使用“酒杯-高脚杯”,而不是“酒杯”或“高脚杯”,不得缺少“-”前后和“:”前面的任何内容,不能自创)
  7331. - 画面:【构图/视角】+画面描述(越简单越好,对视觉场景/实景的描述,不描述氛围,比如紧张、压抑的氛围)。举例:[近景特写,浅景深]季潇握着手机贴在耳边,指关节因为用力而泛白。她的眼眶微红,睫毛在狂风中颤抖,嘴唇紧抿,眼神中闪烁着最后一点希冀的微光。
  7332. - 画面中如果出现主体,只能选择上述<主体列表>中的一个,主体的名称也必须保持一致(如必须使用“沈秋岚-程家居家围裙造型”,而不是“沈秋岚”,不得缺少“-”前后和“:”前面的任何内容,不能自创)
  7333. - 画面中如果出现道具,只能选择上述<道具列表>中的一个,道具的名称也必须保持一致(如必须使用“酒杯-高脚杯”,而不是“酒杯”或“高脚杯”,不得缺少“-”前后和“:”前面的任何内容,不能自创)
  7334. - 运镜:场景+画面描述+运镜
  7335. - 运镜描述中如果出现主体或道具,逻辑需同画面一直,必须选择<主体列表>或<道具列表>中的一个,名称也必须保持一致
  7336. - 配音台词:格式为“中文配音:[角色]台词”,角色配音台词和语言由模型匹配生成,角色必须对应本集中的<主体列表>中的一个(非对话内容则统一固定为“旁白”角色,[角色]的角色名必须完整使用主体列表的主体名,如必须使用“沈秋岚-程家居家围裙造型”,而不是“沈秋岚”,不得缺少“-”前后和“:”前面的任何内容,不能自创,不能自创)
  7337. - 台词内容需满足以下要求: (1. 台词来源规则:台词内容必须逐字摘录自小说原文中的人物直接对话(带引号的内容),不允许改写、总结、压缩、扩写或拼接原文对白,不允许生成原文中不存在的对话。2. 非对话过滤:旁白、心理描写、环境描写、动作描写等内容,禁止出现在台词内容中。3. 空台词规则:如果该分镜中没有人物对话,则“台词内容”必须返回“无”,严禁输出“无台词”、“没有”等任何替代文本。4. 禁止推测:不得根据语境补全人物可能说的话。5.如果没有原文,直接返回空)
  7338. - 背景音效:背景音效需根据内容和画面生成,贴合当前画面描述和动作,例如:背景音效:高跟鞋的脚本声
  7339. 6.《情绪-视听语言映射表》:
  7340. 6.1 场景空间与时间心理学 (Scene & Temporality)
  7341. 维度 | 视听特征 | 对应核心情绪 | 心理学/叙事原理
  7342. -----|---------|------------|----------------
  7343. 场景空间 | 封闭狭小 (Corridors/Closets) | 压抑、幽闭恐惧 | 物理限制映射心理自由的剥夺。
  7344. 场景空间 | 极度开阔 (Empty Plains/Desert) | 孤独、渺小、疏离 | 剥离社会性支撑,强化个体的虚无感。
  7345. 时间选择 | 黄金时刻 (Golden Hour) | 怀旧、温情、希望 | 柔和漫反射光线符合大脑对“美好记忆”的视觉加工。
  7346. 时间选择 | 蓝调时刻/深夜 (Blue/Night) | 冷漠、忧郁、未知危险 | 冷色调抑制血清素,增强感官的不安全感。
  7347. 6.2 剪辑节奏与感知阈值 (Shot Duration & Rhythm)
  7348. 维度 | 视听特征 | 对应核心情绪 | 心理学/叙事原理
  7349. -----|---------|------------|----------------
  7350. 长镜头 | >15秒的静止或缓慢移动 | 沉思、肃穆、煎熬 | 强迫观众进入实时流逝,产生生理性的存在感。
  7351. 快切剪辑 | <2秒的频繁切换 | 混乱、亢奋、惊恐 | 模拟高肾上腺素下的视觉捕捉,剥夺深度思考时间。
  7352. 节奏错位 | 非同步切分 (L-cut/J-cut) | 扭曲、不协调 | 破坏连续性假定,产生心理上的不适与悬疑感。
  7353. 6.3 运镜调度与情绪调动 (Cinematic Movement & Affect)
  7354. 运镜方式 | 物理特征 | 心理/情绪功能 | 专业应用场景
  7355. --------|---------|--------------|-------------
  7356. 推镜头 (Dolly In) | 摄影机物理接近主体 | 专注、发现、心理逼近。通过压缩背景空间,强制观众忽略环境,进入主体的内心世界。 | 角色顿悟、秘密揭晓、情感升温。
  7357. 拉镜头 (Dolly Out) | 摄影机物理远离主体 | 疏离、解脱、宿命感。将主体置于广阔背景中,显示主体的渺小或强调事情已成定局。 | 悲剧结尾、孤独的告别、审视全局。
  7358. 摇镜头 (Pan/Tilt) | 轴心转动,视点改变 | 观察、审读、惊奇感。模拟人眼转头,用于揭示空间关系或进行逻辑上的“连接”。 | 发现潜伏的危机、环视战场、建立空间地理志。
  7359. 跟拍 (Tracking Shot) | 保持与主体同速移动 | 强烈的参与感、伴随性焦虑。让观众感觉自己是角色的共谋者或影子,产生“实时感”。 | 逃亡、快步穿行于走廊、展示角色的行动力。
  7360. 横移 (Truck/Side Track) | 与主体平行横向移动 | 客观审视、叙事流逝。像卷轴画一样展开,产生一种冷静、观察者的旁白视角。 | 展示环境细节、两个角色在边走边谈时的心理博弈。
  7361. 手持/肩扛 (Handheld) | 生理性不规则晃动 | 不安、真实感、心理动荡。模拟肉眼观察,通过不稳定性唤起观众的本能警觉。 | 冲突现场、纪录片式纪实感、角色精神崩溃。
  7362. 斯坦尼康 (Steadicam) | 流畅、近乎滑行的移动 | 全知感、梦幻、无缝观察。消除物理重力感,产生一种“上帝视角”或“游魂般”的观察。 | 漫长而复杂的转场、舞会等优雅场景的穿梭。
  7363. 希区柯克变焦 (Dolly Zoom) | 推进同时反向变焦 | 眩晕、世界崩塌、极度惊骇。空间透视的物理扭曲直接引发生理性失衡。 | 角色得知真相的瞬间、高度恐惧。
  7364. 升降/摇臂 (Crane Shot) | 垂直或斜向空间位移 | 释放、崇高、超越性。打破二维平面的限制,将情绪从微观拉向宏观。 | 史诗感的结局、战场全景、情绪的爆发式升华。
  7365. 6.4 构图、色彩与符号学 (Composition & Aesthetics)
  7366. 维度 | 视听特征 | 对应核心情绪 | 心理学原理
  7367. -----|---------|------------|------------
  7368. 角度 | 仰角 (Low Angle) | 威严、恐惧、崇高 | 模拟幼年视角,确立权力层级。
  7369. 角度 | 俯角 (High Angle) | 脆弱、无助、卑微 | 模拟审视感,削弱主体力量。
  7370. 构图 | 荷兰坡 (Dutch Tilt) | 疯狂、崩溃、失衡 | 重力轴偏离导致前庭系统产生失衡反应。
  7371. 色彩 | 极高饱和度/冷暖撞色 | 狂喜、冲突、超现实 | 强烈的视觉刺激诱发情绪阈值波动。
  7372. 6.5 进阶视听修辞与认知干预 (Advanced AV Rhetoric)
  7373. 修辞策略 | 技术实现 | 心理/情绪效应 | 理论基石
  7374. --------|---------|--------------|----------
  7375. 声画对位 (Contrapuntal AV) | 在极端残酷/激烈的画面中配以极其舒缓优雅的音乐(如古典乐)。 | 荒诞恐怖感、神圣的残酷、间离效果。通过感官冲突迫使观众从原始情绪中抽离,进入形而上的思考。 | 安德烈·塔可夫斯基《雕刻时光》、库布里克式美学。
  7376. 非平衡构图 (Compositional Tension) | 突破平衡法则,将视觉重点(Eye-tracking)置于画面边缘或极端角落。 | 本能警觉、不安全感、期待感。利用大脑对“视觉缺失”的补充本能,暗示画面外存在不可见的威胁。 | 格式塔心理学、眼动追踪实验。
  7377. 认知负荷调控 (Neurocinematics) | 利用快速剪辑抑制前额叶皮层(理性区),激活杏仁核(情绪区)。 | 纯粹感官化、生存模式切换。若要观众思考,则延长镜头以激活认知分析;若要观众受惊,则通过视觉过载切断其逻辑判断。 | 神经电影学 (Neurocinematics)。
  7378. 6.6 声音设计与潜意识影响 (Soundscape)
  7379. 维度 | 视听特征 | 对应核心情绪 | 心理学/生理学原理
  7380. -----|---------|------------|------------------
  7381. 音色 | 极低频音 (Infra-sound) | 潜伏恐惧、生理不安 | 模仿自然灾害频率,唤醒生物避害本能。
  7382. 环境声 | 极度死寂 (Silence) | 震惊、神圣、虚无 | 听觉剥夺放大视觉信号,产生心理张力。
  7383. 非现场音 | 不和谐音/尖锐音效 | 精神紧绷、躁郁 | 高频声音直接刺激杏仁核,引发防御机制。
  7384. 理论基石:
  7385. - The Five C's of Cinematography by Joseph V. Mascelli
  7386. - Film Art: An Introduction by David Bordwell
  7387. - Sight, Sound, Motion by Herbert Zettl
  7388. - Notes on the Cinematograph by Robert Bresson
  7389. \n\n
  7390. 7.{$mappedArtStyle_prompt}
  7391. 示例格式:\n
  7392. 第1集:隐形的守护者
  7393. ###故事梗概
  7394. 内容梗概:许芸归家遭徐母毒打,她冷静周旋并编造谎言掩盖行踪,引得一旁阴沉的卞大伟愈发急躁。半个月后,徐母离家,卞大伟兽性大发欲行不轨。生死关头,隐形的程景现身肉搏,屋内家具横飞,卞大伟被“空气”重创。许芸趁乱逃出,却惊见地面凭空浮现一串血脚印。意识到守护者为救自己负伤,许芸不再逃避,对着虚无的空气执拗告白,誓要与这看不见的爱人共进退。
  7395. ###美术风格
  7396. 基础画风风格词:胡金铨武侠
  7397. 视觉风格描述:整体采用复古武侠电影美学,以低饱和度的青灰与墨色调为主,营造冷峻、肃杀的江湖意境。光影上运用强烈的侧逆光与深邃硬阴影,完美契合“墨影剑客”神秘莫测的特质。画面带有35mm胶片的粗粝质感,构图简约且富有张力,展现出深沉的复仇叙事氛围。
  7398. ###主体列表
  7399. 陆沉:冷酷神秘的复仇剑客,陆家庄唯一幸存者,身负绝世武功与血海深仇。{全景,正面拍摄,青年男性,亚洲人,黑色长发束起,五官轮廓深邃。服饰穿搭:黑色连帽斗篷,深灰色交领劲装,黑色束腰,背负一柄漆黑宽大的无锋重剑。姿态:站立。}{{青年男性(约25-30岁),音色冷峻、低沉,语速平稳缓慢,透着一股拒人于千里之外的孤傲与肃杀。}}
  7400. 刀疤脸:武林盟的嚣张爪牙,性格暴戾、欺软怕硬,是赵无极麾下的打手。{全景,正面拍摄,中年男性,亚洲人,短发,左脸有一道明显的深长刀疤。服饰穿搭:土黄色粗布武士服,腰间挂着一枚古铜色的“武林盟”令牌。姿态:站立。}{{中年男性(约35-45岁),音色粗犷、沙哑,语气嚣张跋扈,带有明显的威胁感和市井痞气。}}
  7401. 老李:悦来酒馆的老板,胆小卑微的底层百姓,在乱世中艰难求生。{全景,正面拍摄,老年男性,亚洲人,灰白乱发,面容消瘦且布满皱纹。服饰穿搭:破旧的深蓝色粗布长袍,腰间系着一条带有污渍的灰色围裙。姿态:站立。}{{老年男性(约60岁以上),音色苍老、沙哑且带有颤抖,语气唯唯诺诺,充满了卑微与惊恐。}}
  7402. ###场景列表
  7403. 边境小镇街道:夜幕笼罩下的边陲小镇,风沙肆虐,街道两旁是破旧的木质建筑,透着一股荒凉与肃杀的气息,是故事发生的地理背景。{远景,夜幕下的边境小镇,破败的木质建筑错落有致,风沙在空旷的街道上飞扬,远处有微弱的灯火。无人物。}
  7404. 悦来酒馆:酒馆内部陈设简陋,木质桌椅散乱分布,柜台上摆放着酒坛。室内光线昏暗,只有几盏摇曳的油灯提供微弱的暖光,与室外的冷调形成对比。{室内场景,昏暗的古风酒馆内部,粗糙的木质桌椅,木质柜台与酒坛,空气中浮动着尘埃,强烈的侧逆光勾勒出环境轮廓,深邃的硬阴影。无人物。}
  7405. ###道具列表
  7406. 全家福照片:一张全家福照片{AI真人短剧,横屏16:9,8K电影级写实,无人物实体出镜的道具参考图,家庭全家福相框,多角度视角图,正面、背面、侧面、局部细节,木质或金属相框,照片内容可模糊表现多人合影构图,重点展示边缘站位感,相框尺寸标注25厘米×20厘米×2厘米,白底产品级干净展示}
  7407. 酒杯-高脚杯:一个高脚杯{AI真人短剧,横屏16:9,8K电影级写实,高脚酒杯,道具多角度视角图,正面、背面、侧面、局部细节,透明玻璃材质,细长杯柄,尺寸标注高24厘米杯口6厘米,白底干净展示,无人物}
  7408. ###分段剧本
  7409. 片段数量:8
  7410. 旁白音色:成年男性(约35-50岁),音色厚重且富有磁性,带有老电影叙事般的沧桑感,语调低沉平稳,富有江湖气息。
  7411. ##片段1
  7412. 时长:12s
  7413. 分镜1
  7414. 出场角色:刀疤脸
  7415. 场景:边境小镇街道
  7416. 出场道具:酒杯-高脚杯
  7417. 画面:[大远景,冷色调,高对比度] 漫天黄沙在狂风中如怒龙翻滚,遮蔽了微弱的星光。悦来酒馆残破的木门在风中剧烈摇晃,铁链撞击木板发出沉重的闷响,四周建筑尽显荒凉。
  7418. 运镜:大远景固定镜头,随后风沙席卷向镜头形成遮挡转场。
  7419. 配音台词:
  7420. 背景音效:
  7421. 分镜2
  7422. 出场角色:刀疤脸
  7423. 场景:悦来酒馆
  7424. 出场道具:酒杯-高脚杯
  7425. 画面:[全景,暖色调阴影构图] 酒馆内部昏暗,几盏油灯在风中疯狂摇曳,投射出扭曲的长影。角落里的食客低头缩颈,双手紧紧攥着粗瓷碗,空气中漂浮着细小的尘埃。
  7426. 运镜:缓慢横移,掠过阴影中瑟缩的食客。
  7427. 配音台词:
  7428. 背景音效:
  7429. 分镜3
  7430. 出场角色:刀疤脸
  7431. 场景:悦来酒馆
  7432. 出场道具:酒杯-高脚杯
  7433. 画面:[中景,三分法构图] 三名大汉围坐在正中央的圆桌旁,腰间挂着的“武林盟”金色令牌在灯火下闪烁。桌上残羹冷炙横陈,一名大汉正张狂地大笑,脚踩在长凳上。
  7434. 运镜:从中景推向令牌特写。
  7435. 配音台词:
  7436. 背景音效:
  7437. 分镜4
  7438. 出场角色:刀疤脸
  7439. 场景:悦来酒馆
  7440. 出场道具:酒杯-高脚杯
  7441. 画面:[特写,低角度仰拍] 刀疤脸横肉颤动,嘴角挂着轻蔑的笑。他随手倾覆手中的酒碗,琥珀色的液体在空中划过弧线,重重溅在老李打着补丁的布鞋和裤脚上。
  7442. 运镜:极速推向酒水溅起的瞬间。
  7443. 配音台词:
  7444. 背景音效:
  7445. 分镜5
  7446. 出场角色:刀疤脸
  7447. 场景:悦来酒馆
  7448. 出场道具:酒杯-高脚杯
  7449. 画面:[近景,荷兰角倾斜构图] 刀疤脸斜睨着眼,昏暗的灯光勾勒出他脸上狰狞的伤疤。他身体前倾,阴影完全笼罩了对面的老李,眼神如毒蛇般阴冷。
  7450. 运镜:固定机位,利用倾斜构图制造压迫感。
  7451. 配音台词:中文配音:[刀疤脸]老李头,这酒里掺水了吧?
  7452. 背景音效:
  7453. \n\n";
  7454. $origin_prompt = trim((string)getProp($data, 'prompt', '确认分镜大纲'));
  7455. $is_regenerate_version = !in_array($origin_prompt, ['确认分镜大纲', '继续策划下一集'], true);
  7456. $is_global_generate_pics = $origin_prompt == '确认分镜大纲' ? 1 : 0;
  7457. $prompt = $origin_prompt;
  7458. $is_single = (int)getProp($anime, 'is_multi') !== 1;
  7459. // 获取章节内容
  7460. $full_content = getProp($anime, 'content');
  7461. if ($uploaded_content) {
  7462. $full_content = $uploaded_content;
  7463. }
  7464. // 根据章节内容拆分章节
  7465. $chapters = $this->splitContent($full_content);
  7466. $chapter_content = isset($chapters[($episode_number-1)]['content']) ? $chapters[($episode_number-1)]['content'] : '';
  7467. if (!$chapter_content) $chapter_content = $full_content; // 如果无法拆分章节则直接用原文
  7468. $messages = [];
  7469. if ($prompt == '确认分镜大纲') {
  7470. // 暂时保留
  7471. $content = $chapter_content;
  7472. if ($is_single) $content = $full_content; // 单剧集使用全文
  7473. if (!$content) {
  7474. Utils::throwError('20003:章节内容无法获取,请确认');
  7475. }
  7476. $question = $is_single
  7477. ? $demo . "请根据以上要求分析完整剧情,直接输出第1集完整单剧集分镜剧本,需一次性生成并可直接保存,剧情内容如下: \n\n$content"
  7478. : $demo . "请根据以上要求分析本章剧情,完成分镜剧本,本集剧情内容如下: \n\n$content";
  7479. // 构建消息
  7480. $messages[] =
  7481. [
  7482. 'role' => 'user',
  7483. 'content' => $question
  7484. ];
  7485. }else if ($prompt == '继续策划下一集') {
  7486. if ($is_single) {
  7487. Utils::throwError('20003:单剧集不支持继续策划下一集');
  7488. }
  7489. $content = $chapter_content;
  7490. if (!$content) {
  7491. Utils::throwError('20003:章节内容无法获取,请确认');
  7492. }
  7493. $question = $demo."请根据以上要求分析本集剧情内容,完成本集分镜剧本(需衔接上一集内容)";
  7494. // 获取上一集最后记录
  7495. $prev_sequence = $episode_number - 1;
  7496. $messages = $this->getEpisodeChatContent($anime_id, $prev_sequence, $content);
  7497. // 构建消息
  7498. $messages[] =
  7499. [
  7500. 'role' => 'user',
  7501. 'content' => $question
  7502. ];
  7503. // dd($messages);
  7504. }else {
  7505. $content = $uploaded_content ? $chapter_content : trim((string)getProp($base_episode, 'content'));
  7506. if (!$content) {
  7507. $content = $is_single ? $full_content : ($chapter_content ?: $full_content);
  7508. }
  7509. if (!$content) {
  7510. Utils::throwError('20003:章节内容无法获取,请确认');
  7511. }
  7512. $messages = $this->getEpisodeChatMessages($anime_id, $episode_number);
  7513. $targetEpisodeText = $is_single ? '第1集完整单剧集版本' : "第{$episode_number}集完整的新版本剧集";
  7514. $question = $demo . "\n请基于以上历史对话记录,重新生成{$targetEpisodeText},并严格按示例格式输出完整内容。\n";
  7515. if ($origin_prompt) {
  7516. $question .= "本次修改要求如下:\n{$origin_prompt}";
  7517. }
  7518. $messages[] =
  7519. [
  7520. 'role' => 'user',
  7521. 'content' => $question
  7522. ];
  7523. }
  7524. // 处理文本模型
  7525. $model = getProp($data, 'model');
  7526. if (!$model) {
  7527. // 用户没有输入,从anime表获取
  7528. $anime = DB::table('mp_animes')->where('id', $anime_id)->first();
  7529. $model = getProp($anime, 'model');
  7530. if (!$model) {
  7531. // anime表也没有,使用默认值
  7532. $model = 'doubao-seed-2-0-mini-260215';
  7533. }
  7534. }
  7535. // 验证模型是否在可用模型表中
  7536. if (!DB::table('mp_text_models')->where('model', $model)->where('is_enabled', 1)->exists()) {
  7537. $model = 'doubao-seed-2-0-mini-260215';
  7538. }
  7539. // 模型兼容性处理:deepseek-chat 和 deepseek-reasoner 映射到 deepseek-v4-flash
  7540. $thinkingMode = getProp($data, 'thinking', 'disabled'); // 默认非思考模式
  7541. $reasoningEffort = getProp($data, 'reasoning_effort', 'high'); // 默认思考强度
  7542. if ($model === 'deepseek-chat') {
  7543. // deepseek-chat 映射到 deepseek-v4-flash,非思考模式
  7544. $model = 'deepseek-v4-flash';
  7545. $thinkingMode = 'disabled';
  7546. } elseif ($model === 'deepseek-reasoner') {
  7547. // deepseek-reasoner 映射到 deepseek-v4-flash,思考模式
  7548. $model = 'deepseek-v4-flash';
  7549. $thinkingMode = 'enabled';
  7550. }
  7551. $post_data = [
  7552. 'model' => $model,
  7553. 'messages' => $messages,
  7554. // 'max_tokens' => 8192,
  7555. 'temperature' => 0.7,
  7556. 'frequency_penalty' => 0,
  7557. 'presence_penalty' => 0,
  7558. 'response_format' => ['type' => 'text'],
  7559. 'thinking' => ['type' => $thinkingMode],
  7560. 'stream' => true // 启用流式输出
  7561. ];
  7562. if ($thinkingMode == 'enabled') $post_data['reasoning_effort'] = $reasoningEffort;
  7563. // 根据模型类型选择调用方法
  7564. $fullContent = '';
  7565. $fullReasoningContent = '';
  7566. $usage = [];
  7567. try {
  7568. if (in_array($model, ['deepseek-reasoner', 'deepseek-chat', 'deepseek-v4-flash', 'deepseek-v4-pro'])) {
  7569. // DeepSeek 官方模型使用 DeepSeek API
  7570. $streamGenerator = $this->deepSeekStreamResponse($post_data);
  7571. } else if (in_array($model, $this->gpt_text_models)) {
  7572. // GPT-5.4 模型使用 TokenRouter API
  7573. $streamGenerator = $this->gpt54StreamResponse($post_data);
  7574. } else {
  7575. // 其他模型使用火山引擎API
  7576. $streamGenerator = $this->volcEngineChatCompletion($post_data);
  7577. }
  7578. // 验证返回值类型
  7579. if (!is_iterable($streamGenerator)) {
  7580. $errorType = is_object($streamGenerator) ? get_class($streamGenerator) : gettype($streamGenerator);
  7581. dLog('deepseek')->error('chat流式生成器无效', [
  7582. 'generator_type' => $errorType,
  7583. 'model' => $model,
  7584. 'anime_id' => $anime_id,
  7585. 'episode_number' => $episode_number
  7586. ]);
  7587. logDB('deepseek', 'error', 'chat流式生成器类型错误', [
  7588. 'type' => $errorType,
  7589. 'model' => $model
  7590. ]);
  7591. yield [
  7592. 'type' => 'error',
  7593. 'episode' => [],
  7594. 'answer' => '',
  7595. 'reasoning' => '',
  7596. 'usage' => [],
  7597. 'error' => '接口返回数据异常,请重新请求'
  7598. ];
  7599. return;
  7600. }
  7601. // 处理流式输出
  7602. foreach ($streamGenerator as $chunk) {
  7603. if (isset($chunk['type'])) {
  7604. if ($chunk['type'] === 'done') {
  7605. // 最终结果
  7606. $fullContent = $chunk['full_content'];
  7607. $fullReasoningContent = $chunk['full_reasoning'];
  7608. $usage = isset($chunk['usage']) ? $chunk['usage'] : [];
  7609. } else {
  7610. // 逐块yield数据
  7611. yield $chunk;
  7612. }
  7613. }
  7614. }
  7615. } catch (\Exception $e) {
  7616. dLog('deepseek')->error('chat流式处理异常: ' . $e->getMessage(), [
  7617. 'model' => $model,
  7618. 'anime_id' => $anime_id,
  7619. 'episode_number' => $episode_number,
  7620. 'trace' => $e->getTraceAsString()
  7621. ]);
  7622. logDB('deepseek', 'error', 'chat流式处理失败', [
  7623. 'error' => $e->getMessage(),
  7624. 'model' => $model
  7625. ]);
  7626. yield [
  7627. 'type' => 'error',
  7628. 'episode' => [],
  7629. 'answer' => '',
  7630. 'reasoning' => '',
  7631. 'usage' => [],
  7632. 'error' => '处理请求时发生异常: ' . $e->getMessage()
  7633. ];
  7634. return;
  7635. }
  7636. dLog('deepseek')->info('完整内容: '.$fullContent);
  7637. logDB('deepseek', 'info', '完整内容', ['content' => $fullContent, 'anime_id' => $anime_id]);
  7638. // 处理完整内容并返回最终结果
  7639. $episode_arr = handleEpisodeContentForAce($fullContent);
  7640. logDB('deepseek', 'info', '解析内容', $episode_arr);
  7641. // // 如果有anime_script_id,进行主体和场景名称的智能匹配和替换(激进策略)
  7642. // if ($anime_script_id && !empty($extra_roles) && !empty($extra_scenes)) {
  7643. // $episode_arr = $this->matchAndReplaceRolesAndScenes($episode_arr, $extra_roles, $extra_scenes);
  7644. // logDB('deepseek', 'info', '匹配替换后的解析内容', $episode_arr);
  7645. // }
  7646. // 验证逻辑:如果anime_script_id有值,需要验证生成的主体和场景是否匹配
  7647. $max_retries = 3;
  7648. $retry_count = 0;
  7649. $validation_failed = false;
  7650. $validation_error_msg = '';
  7651. // 验证生成的内容
  7652. if ($anime_script_id) {
  7653. // 检查是否有acts数据
  7654. if (empty($episode_arr['acts'])) {
  7655. $validation_failed = true;
  7656. $validation_error_msg = '未生成分镜剧本相关的内容';
  7657. } else {
  7658. // 检查主体列表是否匹配 - 只验证生成的主体名是否都在预期列表中
  7659. $generated_roles = array_column($episode_arr['roles'], 'role');
  7660. // 过滤掉"旁白"角色
  7661. $generated_roles = array_filter($generated_roles, function($role) {
  7662. return $role !== '旁白';
  7663. });
  7664. $expected_roles = array_keys($extra_roles);
  7665. // 找出生成的主体中不在预期列表中的主体
  7666. $invalid_roles = array_diff($generated_roles, $expected_roles);
  7667. if (!empty($invalid_roles)) {
  7668. $validation_failed = true;
  7669. $validation_error_msg = '生成的主体列表包含不在【强制主体设定】中的主体:' . implode('、', $invalid_roles);
  7670. }
  7671. // 检查场景列表是否匹配 - 只验证生成的场景名是否都在预期列表中
  7672. if (!$validation_failed) {
  7673. $generated_scenes = array_column($episode_arr['scenes'], 'scene');
  7674. $expected_scenes = array_keys($extra_scenes);
  7675. // 找出生成的场景中不在预期列表中的场景
  7676. $invalid_scenes = array_diff($generated_scenes, $expected_scenes);
  7677. if (!empty($invalid_scenes)) {
  7678. $validation_failed = true;
  7679. $validation_error_msg = '生成的场景列表包含不在【强制场景设定】中的场景:' . implode('、', $invalid_scenes);
  7680. }
  7681. }
  7682. }
  7683. } else {
  7684. // 如果没有anime_script_id,只检查是否有acts数据
  7685. if (empty($episode_arr['acts'])) {
  7686. $validation_failed = true;
  7687. $validation_error_msg = '未生成分镜剧本相关的内容';
  7688. }
  7689. }
  7690. // 重试逻辑
  7691. while ($validation_failed && $retry_count < $max_retries) {
  7692. $retry_count++;
  7693. dLog('deepseek')->warning("验证失败,开始第{$retry_count}次重试", [
  7694. 'reason' => $validation_error_msg,
  7695. 'anime_id' => $anime_id,
  7696. 'episode_number' => $episode_number
  7697. ]);
  7698. logDB('deepseek', 'warning', "第{$retry_count}次重试", [
  7699. 'reason' => $validation_error_msg,
  7700. 'anime_id' => $anime_id
  7701. ]);
  7702. // 发送流式重试提示
  7703. yield [
  7704. 'type' => 'retry',
  7705. 'retry_count' => $retry_count,
  7706. 'reason' => $validation_error_msg
  7707. ];
  7708. // 构建重试提示词
  7709. $retry_prompt = "生成的剧本存在问题:{$validation_error_msg}。\n\n请严格按照要求重新生成,确保:\n";
  7710. if ($anime_script_id) {
  7711. $retry_prompt .= "1. 主体列表中的所有主体名称必须从【强制主体设定】中选择,不得使用其他主体名称\n";
  7712. $retry_prompt .= "2. 场景列表中的所有场景名称必须从【强制场景设定】中选择,不得使用其他场景名称\n";
  7713. $retry_prompt .= "3. 必须包含完整的分镜剧本内容\n";
  7714. } else {
  7715. $retry_prompt .= "1. 必须包含完整的分镜剧本内容\n";
  7716. }
  7717. // 添加重试消息到messages
  7718. $messages[] = [
  7719. 'role' => 'assistant',
  7720. 'content' => $fullContent
  7721. ];
  7722. $messages[] = [
  7723. 'role' => 'user',
  7724. 'content' => $retry_prompt
  7725. ];
  7726. // 更新post_data的messages
  7727. $post_data['messages'] = $messages;
  7728. // 重新调用API
  7729. try {
  7730. if (in_array($model, ['deepseek-reasoner', 'deepseek-chat', 'deepseek-v4-flash', 'deepseek-v4-pro'])) {
  7731. $streamGenerator = $this->deepSeekStreamResponse($post_data);
  7732. } else if (in_array($model, $this->gpt_text_models)) {
  7733. $streamGenerator = $this->gpt54StreamResponse($post_data);
  7734. } else {
  7735. $streamGenerator = $this->volcEngineChatCompletion($post_data);
  7736. }
  7737. // 处理流式输出
  7738. $fullContent = '';
  7739. $fullReasoningContent = '';
  7740. foreach ($streamGenerator as $chunk) {
  7741. if (isset($chunk['type'])) {
  7742. if ($chunk['type'] === 'done') {
  7743. $fullContent = $chunk['full_content'];
  7744. $fullReasoningContent = $chunk['full_reasoning'];
  7745. $usage = isset($chunk['usage']) ? $chunk['usage'] : [];
  7746. } else {
  7747. yield $chunk;
  7748. }
  7749. }
  7750. }
  7751. // 重新解析内容
  7752. dLog('deepseek')->info("第{$retry_count}次重试完整内容: " . $fullContent);
  7753. logDB('deepseek', 'info', "第{$retry_count}次重试解析内容", ['content' => $fullContent, 'anime_id' => $anime_id]);
  7754. $episode_arr = handleEpisodeContentForAce($fullContent);
  7755. // // 进行主体和场景名称的智能匹配和替换(激进策略)
  7756. // if ($anime_script_id && !empty($extra_roles) && !empty($extra_scenes)) {
  7757. // $episode_arr = $this->matchAndReplaceRolesAndScenes($episode_arr, $extra_roles, $extra_scenes);
  7758. // }
  7759. dLog('deepseek')->info('第'.$retry_count.'次重试后内容: '.$fullContent);
  7760. logDB('deepseek', 'info', "第{$retry_count}次重试解析内容", $episode_arr);
  7761. // 重新验证
  7762. $validation_failed = false;
  7763. $validation_error_msg = '';
  7764. if ($anime_script_id) {
  7765. if (empty($episode_arr['acts'])) {
  7766. $validation_failed = true;
  7767. $validation_error_msg = '未生成分镜剧本相关的内容';
  7768. } else {
  7769. // 检查主体列表 - 只验证生成的主体名是否都在预期列表中
  7770. $generated_roles = array_column($episode_arr['roles'], 'role');
  7771. // 过滤掉"旁白"角色
  7772. $generated_roles = array_filter($generated_roles, function($role) {
  7773. return $role !== '旁白';
  7774. });
  7775. $expected_roles = array_keys($extra_roles);
  7776. $invalid_roles = array_diff($generated_roles, $expected_roles);
  7777. if (!empty($invalid_roles)) {
  7778. $validation_failed = true;
  7779. $validation_error_msg = '生成的主体列表包含不在【强制主体设定】中的主体:' . implode('、', $invalid_roles);
  7780. }
  7781. // 检查场景列表 - 只验证生成的场景名是否都在预期列表中
  7782. if (!$validation_failed) {
  7783. $generated_scenes = array_column($episode_arr['scenes'], 'scene');
  7784. $expected_scenes = array_keys($extra_scenes);
  7785. $invalid_scenes = array_diff($generated_scenes, $expected_scenes);
  7786. if (!empty($invalid_scenes)) {
  7787. $validation_failed = true;
  7788. $validation_error_msg = '生成的场景列表包含不在【强制场景设定】中的场景:' . implode('、', $invalid_scenes);
  7789. }
  7790. }
  7791. }
  7792. } else {
  7793. if (empty($episode_arr['acts'])) {
  7794. $validation_failed = true;
  7795. $validation_error_msg = '未生成分镜剧本相关的内容';
  7796. }
  7797. }
  7798. } catch (\Exception $e) {
  7799. dLog('deepseek')->error("第{$retry_count}次重试异常: " . $e->getMessage(), [
  7800. 'trace' => $e->getTraceAsString()
  7801. ]);
  7802. break;
  7803. }
  7804. }
  7805. // 如果重试后仍然失败
  7806. if ($validation_failed) {
  7807. dLog('deepseek')->error('达到最大重试次数,仍未生成有效内容', [
  7808. 'retry_count' => $retry_count,
  7809. 'last_error' => $validation_error_msg
  7810. ]);
  7811. try {
  7812. $now = date('Y-m-d H:i:s');
  7813. $records = [
  7814. [
  7815. 'uid' => $uid,
  7816. 'anime_id' => $anime_id,
  7817. 'sequence' => $episode_number,
  7818. 'role' => 'user',
  7819. 'content' => $prompt,
  7820. 'created_at' => $now,
  7821. 'updated_at' => $now
  7822. ],
  7823. [
  7824. 'uid' => $uid,
  7825. 'anime_id' => $anime_id,
  7826. 'sequence' => $episode_number,
  7827. 'role' => 'assistant',
  7828. 'content' => $fullContent,
  7829. 'created_at' => $now,
  7830. 'updated_at' => $now
  7831. ]
  7832. ];
  7833. DB::table('mp_anime_records')->insert($records);
  7834. } catch (\Exception $e) {
  7835. dLog('deepseek')->error('保存对话记录失败', ['error' => $e->getMessage()]);
  7836. }
  7837. yield [
  7838. 'type' => 'done',
  7839. 'episode' => [],
  7840. 'answer' => $fullContent,
  7841. 'reasoning' => $fullReasoningContent,
  7842. 'usage' => $usage,
  7843. 'error' => "生成剧本有误,已重试{$retry_count}次仍失败,请稍后重试。错误原因:{$validation_error_msg}"
  7844. ];
  7845. return;
  7846. }
  7847. // 验证成功,记录日志
  7848. if ($retry_count > 0) {
  7849. dLog('deepseek')->info("经过{$retry_count}次重试后验证成功");
  7850. logDB('deepseek', 'info', '重试成功', ['retry_count' => $retry_count, 'anime_id' => $anime_id]);
  7851. }
  7852. // 老逻辑
  7853. $existing_roles = $base_episode ? json_decode((string)getProp($base_episode, 'roles'), true) : [];
  7854. $existing_scenes = $base_episode ? json_decode((string)getProp($base_episode, 'scenes'), true) : [];
  7855. $existing_props = $base_episode ? json_decode((string)getProp($base_episode, 'props'), true) : [];
  7856. $existing_roles = is_array($existing_roles) ? $existing_roles : [];
  7857. $existing_scenes = is_array($existing_scenes) ? $existing_scenes : [];
  7858. $existing_props = is_array($existing_props) ? $existing_props : [];
  7859. $now = date('Y-m-d H:i:s');
  7860. try {
  7861. DB::beginTransaction();
  7862. $saveResult = $this->saveEpisodeVersionData(
  7863. $anime_id,
  7864. $episode_number,
  7865. $episode_arr,
  7866. $existing_roles,
  7867. $existing_scenes,
  7868. $existing_props,
  7869. $current_episode,
  7870. $base_episode,
  7871. $is_regenerate_version,
  7872. $content,
  7873. $now,
  7874. $ref_products
  7875. );
  7876. $episode = $saveResult['episode'];
  7877. $episode_id = $saveResult['episode_id'];
  7878. $merged_roles = $saveResult['merged_roles'];
  7879. $merged_scenes = $saveResult['merged_scenes'];
  7880. $merged_props = $saveResult['merged_props'];
  7881. $record_content = $origin_prompt;
  7882. if ($uploaded_content !== '') {
  7883. // $record_content .= "\n\n###上传内容\n" . $uploaded_content;
  7884. }
  7885. $records = [
  7886. [
  7887. 'uid' => $uid,
  7888. 'anime_id' => $anime_id,
  7889. 'role' => 'user',
  7890. 'content' => $record_content,
  7891. 'sequence' => $episode_number,
  7892. 'episode_id' => $episode_id,
  7893. 'created_at' => $now,
  7894. 'updated_at' => $now
  7895. ],
  7896. [
  7897. 'uid' => $uid,
  7898. 'anime_id' => $anime_id,
  7899. 'role' => 'assistant',
  7900. 'content' => $fullContent,
  7901. // 'content' => '我已按照您的要求完成策划并将内容更新到您右侧的策划文档。',
  7902. 'sequence' => $episode_number,
  7903. 'episode_id' => $episode_id,
  7904. 'created_at' => $now,
  7905. 'updated_at' => $now
  7906. ]
  7907. ];
  7908. $boolen4 = DB::table('mp_anime_records')->insert($records);
  7909. if (!$boolen4) {
  7910. Utils::throwError('20003:对话记录保存失败');
  7911. }
  7912. // 保存模型到anime表
  7913. $update_anime_data = [
  7914. 'model' => $model,
  7915. 'updated_at' => $now
  7916. ];
  7917. if ($uploaded_content) {
  7918. $update_anime_data['content'] = $uploaded_content;
  7919. }
  7920. DB::table('mp_animes')->where('id', $anime_id)->update($update_anime_data);
  7921. }catch (\Exception $e) {
  7922. DB::rollBack();
  7923. dLog('deepseek')->info('新建对话错误信息: '.$e->getMessage());
  7924. yield [
  7925. 'type' => 'done',
  7926. 'answer' => $fullContent,
  7927. 'reasoning' => $fullReasoningContent,
  7928. 'usage' => $usage,
  7929. 'error' => $e->getMessage(),
  7930. ];
  7931. return;
  7932. }
  7933. DB::commit();
  7934. if ($is_global_generate_pics) {
  7935. // // 批量生成全局角色图片
  7936. // $this->batchSetGlobalRoleImg([
  7937. // 'anime_id' => $anime_id,
  7938. // ], true);
  7939. // // 批量生成全局场景图片
  7940. // $this->batchSetGlobalSceneImg([
  7941. // 'anime_id' => $anime_id,
  7942. // ], true);
  7943. // // 将全局角色和场景的task_id、task_status和url继承给当前分集
  7944. // $this->inheritGlobalImagesToEpisodes($anime_id, $episode_id);
  7945. }
  7946. $episode['episode_id'] = $episode_id;
  7947. $episode['roles'] = $merged_roles;
  7948. $episode['scenes'] = $merged_scenes;
  7949. // $episode['acts'] = isset($episode_arr['acts']) && is_array($episode_arr['acts']) ? $episode_arr['acts'] : [];
  7950. yield [
  7951. 'type' => 'done',
  7952. 'episode' => $episode,
  7953. 'answer' => $fullContent,
  7954. 'reasoning' => $fullReasoningContent,
  7955. 'usage' => $usage
  7956. ];
  7957. }
  7958. /**
  7959. * chatForAce的非流式版本 - 用于队列任务
  7960. * 与chatForAce逻辑完全一致,但不使用流式输出
  7961. *
  7962. * @param array $data 请求数据
  7963. * @return array 生成结果
  7964. */
  7965. public function chatForAceNonStream($data) {
  7966. $allChunks = [];
  7967. $finalResult = null;
  7968. // 调用流式方法并收集所有数据
  7969. foreach ($this->chatForAce($data) as $chunk) {
  7970. $allChunks[] = $chunk;
  7971. // 保存最终结果
  7972. if (isset($chunk['type']) && $chunk['type'] === 'done') {
  7973. $finalResult = $chunk;
  7974. }
  7975. }
  7976. // 返回最终结果
  7977. if ($finalResult) {
  7978. return $finalResult;
  7979. }
  7980. // 如果没有done类型的数据,返回最后一个chunk
  7981. return end($allChunks) ?: ['error' => '生成失败'];
  7982. }
  7983. /**
  7984. * 批量生成多个分集
  7985. *
  7986. * @param array $data 请求数据
  7987. * @return array 任务信息
  7988. */
  7989. public function batchGenerateEpisodes($data) {
  7990. $uid = Site::getUid();
  7991. $cpid = Site::getCpid();
  7992. $anime_id = getProp($data, 'anime_id');
  7993. $generate_episode_number = (int)getProp($data, 'generate_episode_number', 1);
  7994. if (!$anime_id) {
  7995. Utils::throwError('20003:anime_id参数缺失');
  7996. }
  7997. if ($generate_episode_number < 1) {
  7998. Utils::throwError('20003:生成集数必须大于0');
  7999. }
  8000. // 获取动漫信息
  8001. $anime = DB::table('mp_animes')->where('id', $anime_id)->first();
  8002. if (!$anime) {
  8003. Utils::throwError('20003:该剧集不存在');
  8004. }
  8005. // 检查是否是多剧集模式
  8006. if ((int)getProp($anime, 'is_multi') !== 1) {
  8007. Utils::throwError('20003:单剧集不支持批量生成');
  8008. }
  8009. // 获取当前已生成的最大集数
  8010. $currentMaxEpisode = DB::table('mp_anime_episodes')
  8011. ->where('anime_id', $anime_id)
  8012. ->where('is_default', 1)
  8013. ->max('episode_number');
  8014. $currentMaxEpisode = $currentMaxEpisode ?: 0;
  8015. // 确定起始集数
  8016. $startEpisodeNumber = $currentMaxEpisode + 1;
  8017. // 如果用户指定了起始集数(通过episode_number参数)
  8018. if (isset($data['episode_number'])) {
  8019. $userStartEpisode = (int)getProp($data, 'episode_number');
  8020. if ($userStartEpisode > $currentMaxEpisode) {
  8021. $startEpisodeNumber = $userStartEpisode;
  8022. }
  8023. }
  8024. // 计算结束集数
  8025. $endEpisodeNumber = $startEpisodeNumber + $generate_episode_number - 1;
  8026. // 检查是否超过总集数限制
  8027. $end_episode_sequence = getProp($anime, 'end_episode_sequence');
  8028. if ($end_episode_sequence && $endEpisodeNumber > $end_episode_sequence) {
  8029. Utils::throwError("20003:总集数只有{$end_episode_sequence}集,无法生成到第{$endEpisodeNumber}集");
  8030. }
  8031. // 检查是否已有进行中的任务
  8032. $existingTask = DB::table('mp_batch_episode_generation_tasks')
  8033. ->where('anime_id', $anime_id)
  8034. ->whereIn('status', ['pending', 'processing'])
  8035. ->first();
  8036. if ($existingTask) {
  8037. Utils::throwError('20003:该剧集已有批量生成任务正在进行中,请等待完成后再试');
  8038. }
  8039. $now = date('Y-m-d H:i:s');
  8040. // 准备任务数据
  8041. $taskData = [
  8042. 'anime_id' => $anime_id,
  8043. 'uid' => $uid,
  8044. 'cpid' => $cpid,
  8045. 'start_episode' => $startEpisodeNumber,
  8046. 'end_episode' => $endEpisodeNumber,
  8047. 'total_episodes' => $generate_episode_number,
  8048. 'current_episode' => 0,
  8049. 'completed_episodes' => 0,
  8050. 'status' => 'pending',
  8051. 'request_data' => json_encode($data, JSON_UNESCAPED_UNICODE),
  8052. 'created_at' => $now,
  8053. 'updated_at' => $now
  8054. ];
  8055. // 创建批量生成任务记录
  8056. $taskId = DB::table('mp_batch_episode_generation_tasks')->insertGetId($taskData);
  8057. if (!$taskId) {
  8058. Utils::throwError('20003:创建批量生成任务失败');
  8059. }
  8060. // 更新任务状态为processing
  8061. DB::table('mp_batch_episode_generation_tasks')
  8062. ->where('id', $taskId)
  8063. ->update([
  8064. 'status' => 'processing',
  8065. 'updated_at' => $now
  8066. ]);
  8067. // 分发到队列
  8068. \App\Jobs\ProcessBatchEpisodeGenerationJob::dispatch(
  8069. $taskId,
  8070. $anime_id,
  8071. $startEpisodeNumber,
  8072. $generate_episode_number,
  8073. $data,
  8074. $uid,
  8075. $cpid
  8076. );
  8077. return [
  8078. 'task_id' => $taskId,
  8079. 'anime_id' => $anime_id,
  8080. 'start_episode' => $startEpisodeNumber,
  8081. 'end_episode' => $endEpisodeNumber,
  8082. 'total_episodes' => $generate_episode_number,
  8083. 'status' => 'processing',
  8084. 'message' => "已创建批量生成任务,将生成第{$startEpisodeNumber}集到第{$endEpisodeNumber}集,共{$generate_episode_number}集"
  8085. ];
  8086. }
  8087. /**
  8088. * 获取批量生成任务状态
  8089. *
  8090. * @param array $data 请求数据
  8091. * @return array 任务状态信息
  8092. */
  8093. public function getBatchGenerationTaskStatus($data) {
  8094. $task_id = getProp($data, 'task_id');
  8095. $anime_id = getProp($data, 'anime_id');
  8096. if (!$task_id && !$anime_id) {
  8097. Utils::throwError('20003:task_id或anime_id参数缺失');
  8098. }
  8099. $query = DB::table('mp_batch_episode_generation_tasks');
  8100. if ($task_id) {
  8101. $query->where('id', $task_id);
  8102. } else {
  8103. // 获取该anime最新的任务
  8104. $query->where('anime_id', $anime_id)
  8105. ->orderByDesc('id')
  8106. ->limit(1);
  8107. }
  8108. $task = $query->first();
  8109. if (!$task) {
  8110. Utils::throwError('20003:任务不存在');
  8111. }
  8112. // 获取任务详情
  8113. $details = DB::table('mp_batch_episode_generation_details')
  8114. ->where('task_id', $task->id)
  8115. ->orderBy('episode_number')
  8116. ->get()
  8117. ->map(function($item) {
  8118. return [
  8119. 'episode_number' => $item->episode_number,
  8120. 'status' => $item->status,
  8121. 'error_message' => $item->error_message,
  8122. 'created_at' => $item->created_at,
  8123. 'updated_at' => $item->updated_at
  8124. ];
  8125. })
  8126. ->toArray();
  8127. // 计算进度百分比
  8128. $progress = 0;
  8129. if ($task->total_episodes > 0) {
  8130. $progress = round(($task->completed_episodes / $task->total_episodes) * 100, 2);
  8131. }
  8132. return [
  8133. 'task_id' => $task->id,
  8134. 'anime_id' => $task->anime_id,
  8135. 'start_episode' => $task->start_episode,
  8136. 'end_episode' => $task->end_episode,
  8137. 'total_episodes' => $task->total_episodes,
  8138. 'current_episode' => $task->current_episode,
  8139. 'completed_episodes' => $task->completed_episodes,
  8140. 'progress' => $progress,
  8141. 'status' => $task->status,
  8142. 'error_message' => $task->error_message,
  8143. 'details' => $details,
  8144. 'created_at' => $task->created_at,
  8145. 'updated_at' => $task->updated_at,
  8146. 'completed_at' => $task->completed_at
  8147. ];
  8148. }
  8149. /**
  8150. * 智能匹配并替换主体和场景名称
  8151. * 如果生成的主体或场景名称是强制设定名称的子串,则自动替换为完整名称
  8152. *
  8153. * @param array $episode_arr 解析后的剧集数据
  8154. * @param array $extra_roles 强制主体设定(key为主体名称)
  8155. * @param array $extra_scenes 强制场景设定(key为场景名称)
  8156. * @return array 替换后的剧集数据
  8157. */
  8158. private function matchAndReplaceRolesAndScenes($episode_arr, $extra_roles, $extra_scenes) {
  8159. $replaced_count = 0;
  8160. // 1. 处理主体列表
  8161. if (!empty($episode_arr['roles'])) {
  8162. foreach ($episode_arr['roles'] as &$role) {
  8163. $generated_role_name = $role['role'] ?? '';
  8164. // 跳过旁白
  8165. if ($generated_role_name === '旁白') {
  8166. continue;
  8167. }
  8168. // 如果生成的主体名不在强制设定中,尝试模糊匹配
  8169. if (!isset($extra_roles[$generated_role_name])) {
  8170. $matched_role = $this->findMatchingName($generated_role_name, array_keys($extra_roles));
  8171. if ($matched_role) {
  8172. dLog('deepseek')->info("主体名称替换: {$generated_role_name} => {$matched_role}");
  8173. $role['role'] = $matched_role;
  8174. $replaced_count++;
  8175. }
  8176. }
  8177. }
  8178. }
  8179. // 2. 处理场景列表
  8180. if (!empty($episode_arr['scenes'])) {
  8181. foreach ($episode_arr['scenes'] as &$scene) {
  8182. $generated_scene_name = $scene['scene'] ?? '';
  8183. // 如果生成的场景名不在强制设定中,尝试模糊匹配
  8184. if (!isset($extra_scenes[$generated_scene_name])) {
  8185. $matched_scene = $this->findMatchingName($generated_scene_name, array_keys($extra_scenes));
  8186. if ($matched_scene) {
  8187. dLog('deepseek')->info("场景名称替换: {$generated_scene_name} => {$matched_scene}");
  8188. $scene['scene'] = $matched_scene;
  8189. $replaced_count++;
  8190. }
  8191. }
  8192. }
  8193. }
  8194. // 3. 处理分段剧本中的主体和场景引用(全能模式下只使用segment_content字段)
  8195. if (!empty($episode_arr['acts'])) {
  8196. foreach ($episode_arr['acts'] as &$act) {
  8197. if (!empty($act['segments'])) {
  8198. foreach ($act['segments'] as &$segment) {
  8199. if (!empty($segment['segment_content'])) {
  8200. $original_content = $segment['segment_content'];
  8201. $replaced_content = $original_content;
  8202. // 替换场景名 - 在文本中查找并替换
  8203. foreach (array_keys($extra_scenes) as $full_scene_name) {
  8204. // 尝试找到可能的短名称
  8205. $potential_short_names = [];
  8206. // 提取场景名的主要部分(冒号之前)
  8207. if (mb_strpos($full_scene_name, ':') !== false) {
  8208. $short_name = mb_substr($full_scene_name, 0, mb_strpos($full_scene_name, ':'));
  8209. $potential_short_names[] = $short_name;
  8210. } elseif (mb_strpos($full_scene_name, ':') !== false) {
  8211. $short_name = mb_substr($full_scene_name, 0, mb_strpos($full_scene_name, ':'));
  8212. $potential_short_names[] = $short_name;
  8213. }
  8214. // 替换场景字段中的短名称
  8215. foreach ($potential_short_names as $short_name) {
  8216. if ($short_name !== $full_scene_name) {
  8217. $pattern = '/(?<=场景:|场景:|拍摄场景:|拍摄场景:|背景场景:|背景场景:|环境:|环境:)' . preg_quote($short_name, '/') . '(?=\s|$|\n)/u';
  8218. $new_content = preg_replace($pattern, $full_scene_name, $replaced_content);
  8219. if ($new_content !== $replaced_content) {
  8220. $replaced_content = $new_content;
  8221. $replaced_count++;
  8222. dLog('deepseek')->info("场景名替换: {$short_name} => {$full_scene_name}");
  8223. }
  8224. }
  8225. }
  8226. }
  8227. // 替换配音角色名(跳过旁白)
  8228. foreach (array_keys($extra_roles) as $full_role_name) {
  8229. if ($full_role_name === '旁白') {
  8230. continue;
  8231. }
  8232. // 尝试找到可能的短名称
  8233. $potential_short_names = [];
  8234. // 提取角色名的主要部分(短横线之前)
  8235. if (mb_strpos($full_role_name, '-') !== false) {
  8236. $short_name = mb_substr($full_role_name, 0, mb_strpos($full_role_name, '-'));
  8237. $potential_short_names[] = $short_name;
  8238. }
  8239. // 替换配音台词中的短名称
  8240. foreach ($potential_short_names as $short_name) {
  8241. if ($short_name !== $full_role_name) {
  8242. $pattern = '/(?<=配音台词:\[|配音台词:\[|中文配音:\[|中文配音:\[)\s*' . preg_quote($short_name, '/') . '\s*(?=\])/u';
  8243. $new_content = preg_replace($pattern, $full_role_name, $replaced_content);
  8244. if ($new_content !== $replaced_content) {
  8245. $replaced_content = $new_content;
  8246. $replaced_count++;
  8247. dLog('deepseek')->info("角色名替换: {$short_name} => {$full_role_name}");
  8248. }
  8249. }
  8250. }
  8251. }
  8252. // 如果有替换,更新segment_content
  8253. if ($replaced_content !== $original_content) {
  8254. $segment['segment_content'] = $replaced_content;
  8255. }
  8256. }
  8257. }
  8258. }
  8259. }
  8260. }
  8261. if ($replaced_count > 0) {
  8262. logDB('deepseek', 'info', '主体和场景名称智能替换', [
  8263. 'replaced_count' => $replaced_count
  8264. ]);
  8265. }
  8266. return $episode_arr;
  8267. }
  8268. /**
  8269. * 查找匹配的名称
  8270. * 规则:如果生成的名称是强制设定名称的子串(开头部分),则返回匹配的强制设定名称
  8271. *
  8272. * @param string $generated_name 生成的名称
  8273. * @param array $expected_names 预期的名称列表
  8274. * @return string|null 匹配到的名称,如果没有匹配则返回null
  8275. */
  8276. private function findMatchingName($generated_name, $expected_names) {
  8277. if (empty($generated_name)) {
  8278. return null;
  8279. }
  8280. // 尝试精确匹配
  8281. if (in_array($generated_name, $expected_names)) {
  8282. return $generated_name;
  8283. }
  8284. // 尝试模糊匹配:生成的名称是否为某个预期名称的开头部分
  8285. foreach ($expected_names as $expected_name) {
  8286. // 检查生成的名称是否是预期名称的开头(如"沈秋岚"是"沈秋岚-程家居家围裙造型"的开头)
  8287. if (mb_strpos($expected_name, $generated_name) === 0) {
  8288. return $expected_name;
  8289. }
  8290. }
  8291. // 更宽松的匹配:检查生成的名称是否包含在预期名称中的任意位置
  8292. // 这样可以处理一些变体,但优先级较低
  8293. foreach ($expected_names as $expected_name) {
  8294. if (mb_strpos($expected_name, $generated_name) !== false) {
  8295. return $expected_name;
  8296. }
  8297. }
  8298. return null;
  8299. }
  8300. /**
  8301. * 重新生成分段剧本
  8302. * @param array $data 包含以下字段:
  8303. * - prompt: string|null 用户修改要求(可选)
  8304. * - template_id: int|null 提示词模板ID(可选)
  8305. * - act_id: int|null 单个片段ID(与episode_id二选一)
  8306. * - episode_id: int|null 剧集ID,用于重新生成整个剧集的所有片段(与act_id二选一)
  8307. * @return array 返回生成结果
  8308. */
  8309. public function regenerateSegmentScript($data) {
  8310. $uid = Site::getUid();
  8311. $prompt = trim((string)getProp($data, 'prompt', ''));
  8312. $template_id = getProp($data, 'template_id', 0);
  8313. $act_id = getProp($data, 'act_id', 0);
  8314. $episode_id = getProp($data, 'episode_id', 0);
  8315. // 验证:prompt和template_id至少提供一个
  8316. if (empty($prompt) && empty($template_id)) {
  8317. Utils::throwError('20003:请提供修改要求或选择提示词模板');
  8318. }
  8319. // 验证:act_id和episode_id二选一
  8320. if (empty($act_id) && empty($episode_id)) {
  8321. Utils::throwError('20003:请提供片段ID或剧集ID');
  8322. }
  8323. if (!empty($act_id) && !empty($episode_id)) {
  8324. Utils::throwError('20003:片段ID和剧集ID只能选择一个');
  8325. }
  8326. // 如果提供了template_id,获取模板提示词
  8327. $template_prompt = '';
  8328. if ($template_id > 0) {
  8329. $template = DB::table('mp_prompt_templates')
  8330. ->where('id', $template_id)
  8331. ->where('is_deleted', 0)
  8332. ->first();
  8333. if (!$template) {
  8334. Utils::throwError('20003:提示词模板不存在或已删除');
  8335. }
  8336. $template_prompt = $template->template_prompt ?? '';
  8337. }
  8338. // 合并用户提示词和模板提示词
  8339. $final_prompt = '';
  8340. if (!empty($template_prompt)) {
  8341. $final_prompt = $template_prompt;
  8342. if (!empty($prompt)) {
  8343. $final_prompt .= "\n\n补充要求:" . $prompt;
  8344. }
  8345. } else {
  8346. $final_prompt = $prompt;
  8347. }
  8348. // 获取需要重新生成的内容参考
  8349. $reference_content = '';
  8350. $target_episode_id = 0;
  8351. $target_anime_id = 0;
  8352. $target_episode_number = 0;
  8353. if ($act_id > 0) {
  8354. // 单个片段模式:获取该片段的内容
  8355. $segment = DB::table('mp_episode_segments')
  8356. ->where('id', $act_id)
  8357. ->first();
  8358. if (!$segment) {
  8359. Utils::throwError('20003:片段不存在');
  8360. }
  8361. $target_episode_id = $segment->episode_id;
  8362. $target_anime_id = $segment->anime_id;
  8363. $target_episode_number = $segment->episode_number;
  8364. $reference_content = $segment->act_content ?? '';
  8365. $reference_content = "\n\n【原分段剧本内容】\n{$reference_content}";
  8366. } else {
  8367. // 整个剧集模式:获取所有片段的内容
  8368. $segments = DB::table('mp_episode_segments')
  8369. ->where('episode_id', $episode_id)
  8370. ->orderBy('act_number')
  8371. ->get();
  8372. if ($segments->isEmpty()) {
  8373. Utils::throwError('20003:该剧集没有片段数据');
  8374. }
  8375. $target_episode_id = $episode_id;
  8376. $target_anime_id = $segments[0]->anime_id;
  8377. $target_episode_number = $segments[0]->episode_number;
  8378. // 拼接所有片段内容,保留格式用于AI理解
  8379. $act_contents = [];
  8380. foreach ($segments as $seg) {
  8381. $act_number = $seg->act_number;
  8382. $act_content = $seg->act_content ?? '';
  8383. if (!empty($act_content)) {
  8384. $act_contents[] = "##片段{$act_number}\n" . $act_content;
  8385. }
  8386. }
  8387. $reference_content = implode("\n\n", $act_contents);
  8388. $episode_content = DB::table('mp_anime_episodes')->where('id', $episode_id)->value('content');
  8389. if ($episode_content) {
  8390. $reference_content = "\n\n【本集原始内容】\n{$episode_content}\n\n【本集生成剧本内容】\n{$reference_content}";
  8391. }else {
  8392. $reference_content = "\n\n【原分段剧本内容】\n{$reference_content}";
  8393. }
  8394. }
  8395. if (empty($reference_content)) {
  8396. Utils::throwError('20003:没有可参考的片段内容');
  8397. }
  8398. // 获取剧集的基本信息(intro、art_style、roles、scenes)
  8399. $episode = DB::table('mp_anime_episodes')
  8400. ->where('id', $target_episode_id)
  8401. ->first();
  8402. if (!$episode) {
  8403. Utils::throwError('20003:剧集不存在');
  8404. }
  8405. $anime = DB::table('mp_animes')->where('id', $target_anime_id)->first();
  8406. $anime_script_id = getProp($anime, 'script_id');
  8407. $intro = $episode->intro ?? '';
  8408. $art_style = $episode->art_style ?? '';
  8409. $roles = json_decode($episode->roles ?? '[]', true);
  8410. $scenes = json_decode($episode->scenes ?? '[]', true);
  8411. $props = json_decode($episode->props ?? '[]', true);
  8412. // 构建提示词中的主体列表和场景列表参考
  8413. $roles_ref = '';
  8414. if (!empty($roles) && is_array($roles)) {
  8415. $roles_list = [];
  8416. foreach ($roles as $role) {
  8417. $role_name = getProp($role, 'role', '');
  8418. $role_desc = getProp($role, 'description', '');
  8419. $pic_prompt = getProp($role, 'pic_prompt', '');
  8420. $voice_prompt = getProp($role, 'voice_prompt', '');
  8421. if (!empty($role_name)) {
  8422. $role_line = $role_name;
  8423. if (!empty($role_desc)) $role_line .= ":" . $role_desc;
  8424. if (!empty($pic_prompt)) $role_line .= "{" . $pic_prompt . "}";
  8425. if (!empty($voice_prompt)) $role_line .= "{{" . $voice_prompt . "}}";
  8426. $roles_list[] = $role_line;
  8427. }
  8428. }
  8429. if (!empty($roles_list)) {
  8430. $roles_ref = "\n\n【主体列表参考】\n" . implode("\n", $roles_list);
  8431. }
  8432. }
  8433. $scenes_ref = '';
  8434. if (!empty($scenes) && is_array($scenes)) {
  8435. $scenes_list = [];
  8436. foreach ($scenes as $scene) {
  8437. $scene_name = getProp($scene, 'scene', '');
  8438. $scene_desc = getProp($scene, 'description', '');
  8439. $pic_prompt = getProp($scene, 'pic_prompt', '');
  8440. if (!empty($scene_name)) {
  8441. $scene_line = $scene_name;
  8442. if (!empty($scene_desc)) $scene_line .= ":" . $scene_desc;
  8443. if (!empty($pic_prompt)) $scene_line .= "{" . $pic_prompt . "}";
  8444. $scenes_list[] = $scene_line;
  8445. }
  8446. }
  8447. if (!empty($scenes_list)) {
  8448. $scenes_ref = "\n\n【场景列表参考】\n" . implode("\n", $scenes_list);
  8449. }
  8450. }
  8451. $props_ref = '';
  8452. if (!empty($props) && is_array($props)) {
  8453. $props_list = [];
  8454. foreach ($props as $prop) {
  8455. $prop_name = getProp($prop, 'prop', '');
  8456. $prop_desc = getProp($prop, 'description', '');
  8457. $pic_prompt = getProp($prop, 'pic_prompt', '');
  8458. if (!empty($prop_name)) {
  8459. $prop_line = $prop_name;
  8460. if (!empty($prop_desc)) $prop_line .= ":" . $prop_desc;
  8461. if (!empty($pic_prompt)) $prop_line .= "{" . $pic_prompt . "}";
  8462. $props_list[] = $prop_line;
  8463. }
  8464. }
  8465. if (!empty($props_list)) {
  8466. $props_ref = "\n\n【道具列表参考】\n" . implode("\n", $props_list);
  8467. }
  8468. }
  8469. // 构建美术风格参考
  8470. $art_style_ref = '';
  8471. if (!empty($art_style)) {
  8472. $art_style_ref = "\n\n【美术风格参考】\n" . $art_style;
  8473. }
  8474. // 构建内容简介参考
  8475. $intro_ref = '';
  8476. if (!empty($intro)) {
  8477. $intro_ref = "\n\n【内容简介参考】\n" . $intro;
  8478. }
  8479. // 构建完整的AI提示词
  8480. // $system_prompt = "你是一个专业的剧本编剧和影视导演,擅长根据已有的分段剧本进行优化和调整。你需要严格按照用户的要求重新生成分段剧本内容,保持原有的格式和结构。";
  8481. $user_prompt = "请根据以下要求重新生成分段剧本:\n\n【用户要求】\n{$final_prompt}";
  8482. $user_prompt .= $intro_ref;
  8483. $user_prompt .= $art_style_ref;
  8484. $user_prompt .= $reference_content;
  8485. // $user_prompt .= $roles_ref;
  8486. // $user_prompt .= $scenes_ref;
  8487. $system_prompt = "\n\n【强制要求】\n";
  8488. $system_prompt .= "- 请仅生成<分段剧本>部分,不要生成其他任何内容:";
  8489. $system_prompt .= "- 第一优先级:片段时长上限(单个片段不超过15秒,超过必须切为新片段)";
  8490. $system_prompt .= "- 每个分镜包含:分镜序号、出场角色、场景、出场道具、画面、运镜、配音台词、背景音效\n";
  8491. $system_prompt .= "- 场景必须从【场景列表参考】中选择(如果提供了场景列表),场景名必须精确匹配【场景列表参考】中的名称(如需匹配“酒店会客室-景池筹备会版”,而不是“酒店会客室”,不得缺少“-”前后和“:”前面的任何内容,不能自创,不能自创){$scenes_ref}\n";
  8492. $system_prompt .= "- 出场角色(如果有出现在本分镜)必须从【主体列表参考】中选择(如果提供了主体列表),角色名必须精准匹配【主体列表参考】中的名称(如需匹配“沈秋岚-程家居家围裙造型”,而不是“沈秋岚”,不得缺少“-”前后和“:”前面的任何内容,不能自创,不能自创){$roles_ref}\n";
  8493. $system_prompt .= "- 出场道具(如果有出现在本分镜)必须从【道具列表参考】中选择(如果提供了道具列表),道具名必须精准匹配【道具列表参考】中的名称(如需匹配“智能手机-黑色版”,而不是“智能手机”,不得缺少“-”前后和“:”前面的任何内容,不能自创,不能自创){$props_ref}\n";
  8494. $system_prompt .= "- 【重要】在“画面:”和“运镜:”描述中,如果提到角色或道具,必须使用【主体列表参考】或【道具列表参考】中的完整名称,一个字都不能少,一个字都不能改。例如:正确写法“运镜:镜头推向{林启明-黑色大衣七爷造型}”,错误写法“运镜:镜头推向林启明”(缺少造型后缀)。必须包含“-”及其后面的所有内容,不得简化、不得省略、不得自行修改\n";
  8495. $system_prompt .= "- 保持美术风格的一致性(如果提供了美术风格参考)\n";
  8496. $system_prompt .= "- 参考示例格式如下,此为第一优先规则,必须按以下格式生成内容,请忽略其他任何提示词部分关于规则的描述:
  8497. ###分段剧本
  8498. 片段数量:8
  8499. 旁白音色:成年男性(约35-50岁),音色厚重且富有磁性,带有老电影叙事般的沧桑感,语调低沉平稳,富有江湖气息。
  8500. ##片段1
  8501. 时长:12s
  8502. 分镜1
  8503. 出场角色:刀疤脸
  8504. 场景:边境小镇街道
  8505. 出场道具:酒杯-高脚杯
  8506. 画面:[大远景,冷色调,高对比度] 漫天黄沙在狂风中如怒龙翻滚,遮蔽了微弱的星光。悦来酒馆残破的木门在风中剧烈摇晃,铁链撞击木板发出沉重的闷响,四周建筑尽显荒凉。
  8507. 运镜:大远景固定镜头,随后风沙席卷向镜头形成遮挡转场。
  8508. 配音台词:
  8509. 背景音效:
  8510. 分镜2
  8511. 出场角色:刀疤脸
  8512. 场景:悦来酒馆
  8513. 出场道具:酒杯-高脚杯
  8514. 画面:[全景,暖色调阴影构图] 酒馆内部昏暗,几盏油灯在风中疯狂摇曳,投射出扭曲的长影。角落里的食客低头缩颈,双手紧紧攥着粗瓷碗,空气中漂浮着细小的尘埃。
  8515. 运镜:缓慢横移,掠过阴影中瑟缩的食客。
  8516. 配音台词:
  8517. 背景音效:
  8518. 分镜3
  8519. 出场角色:刀疤脸
  8520. 场景:悦来酒馆
  8521. 出场道具:酒杯-高脚杯
  8522. 画面:[中景,三分法构图] 三名大汉围坐在正中央的圆桌旁,腰间挂着的“武林盟”金色令牌在灯火下闪烁。桌上残羹冷炙横陈,一名大汉正张狂地大笑,脚踩在长凳上。
  8523. 运镜:从中景推向令牌特写。
  8524. 配音台词:
  8525. 背景音效:
  8526. 分镜4
  8527. 出场角色:刀疤脸
  8528. 场景:悦来酒馆
  8529. 出场道具:酒杯-高脚杯
  8530. 画面:[特写,低角度仰拍] 刀疤脸横肉颤动,嘴角挂着轻蔑的笑。他随手倾覆手中的酒碗,琥珀色的液体在空中划过弧线,重重溅在老李打着补丁的布鞋和裤脚上。
  8531. 运镜:极速推向酒水溅起的瞬间。
  8532. 配音台词:
  8533. 背景音效:
  8534. 分镜5
  8535. 出场角色:刀疤脸
  8536. 场景:悦来酒馆
  8537. 出场道具:酒杯-高脚杯
  8538. 画面:[近景,荷兰角倾斜构图] 刀疤脸斜睨着眼,昏暗的灯光勾勒出他脸上狰狞的伤疤。他身体前倾,阴影完全笼罩了对面的老李,眼神如毒蛇般阴冷。
  8539. 运镜:固定机位,利用倾斜构图制造压迫感。
  8540. 配音台词:中文配音:[刀疤脸]老李头,这酒里掺水了吧?
  8541. 背景音效:";
  8542. // 获取模型配置
  8543. $model = getProp($data, 'model');
  8544. if (!$model) {
  8545. $model = $anime ? ($anime->model ?? 'deepseek-v4-pro') : 'deepseek-v4-pro';
  8546. }
  8547. // 验证模型是否在可用模型表中
  8548. if (!DB::table('mp_text_models')->where('model', $model)->where('is_enabled', 1)->exists()) {
  8549. $model = 'deepseek-v4-pro';
  8550. }
  8551. // 模型兼容性处理
  8552. $thinkingMode = 'disabled';
  8553. $reasoningEffort = 'high';
  8554. if ($model === 'deepseek-chat') {
  8555. $model = 'deepseek-v4-flash';
  8556. $thinkingMode = 'disabled';
  8557. } elseif ($model === 'deepseek-reasoner') {
  8558. $model = 'deepseek-v4-flash';
  8559. $thinkingMode = 'enabled';
  8560. }
  8561. // 构建消息
  8562. $messages = [
  8563. [
  8564. 'role' => 'system',
  8565. 'content' => $system_prompt
  8566. ],
  8567. [
  8568. 'role' => 'user',
  8569. 'content' => $user_prompt
  8570. ]
  8571. ];
  8572. // dd($messages);
  8573. // 构建请求参数
  8574. $post_data = [
  8575. 'model' => $model,
  8576. 'messages' => $messages,
  8577. 'temperature' => 0.7,
  8578. 'frequency_penalty' => 0,
  8579. 'presence_penalty' => 0,
  8580. 'response_format' => ['type' => 'text'],
  8581. 'thinking' => ['type' => $thinkingMode],
  8582. 'stream' => false // 非流式输出
  8583. ];
  8584. if ($thinkingMode == 'enabled') $post_data['reasoning_effort'] = $reasoningEffort;
  8585. // 调用AI生成新的分段剧本
  8586. try {
  8587. $fullContent = '';
  8588. $usage = [];
  8589. // 根据模型类型选择调用方法
  8590. if (in_array($model, ['deepseek-reasoner', 'deepseek-chat', 'deepseek-v4-flash', 'deepseek-v4-pro'])) {
  8591. // DeepSeek 官方模型使用 DeepSeek API
  8592. $chatResult = $this->chatOnly($post_data);
  8593. } else if (in_array($model, $this->gpt_text_models)) {
  8594. // GPT 模型使用 TokenRouter API
  8595. $chatResult = $this->gpt54ChatOnly($post_data);
  8596. } else {
  8597. // 其他模型使用火山引擎API
  8598. $chatResult = $this->volcEngineChatCompletion($post_data);
  8599. }
  8600. if (is_array($chatResult)) {
  8601. $fullContent = $chatResult['fullContent'] ?? '';
  8602. $usage = $chatResult['usage'] ?? [];
  8603. }
  8604. $generated_content = $fullContent;
  8605. if (empty($generated_content)) {
  8606. Utils::throwError('20003:AI生成内容为空,请重试');
  8607. }
  8608. // 解析生成的内容 - 按片段分割
  8609. $parsed_segments = [];
  8610. // 先在开头添加换行符,确保第1片段也能被正确分割
  8611. $normalizedText = "\n" . $generated_content;
  8612. $parts = preg_split('/\n\s*##/', $normalizedText);
  8613. foreach ($parts as $part) {
  8614. $part = trim($part);
  8615. if (empty($part)) continue;
  8616. // 匹配"片段X"格式
  8617. if (!preg_match('/^片段\d+/', $part)) {
  8618. continue;
  8619. }
  8620. // 分离标题和内容
  8621. $lines = explode("\n", $part, 2);
  8622. $actTitle = trim($lines[0]);
  8623. $actContent = isset($lines[1]) ? trim($lines[1]) : '';
  8624. // 解析标题,提取序号
  8625. $actNumber = 0;
  8626. if (preg_match('/^片段(\d+)\s*$/u', $actTitle, $segmentTitleMatch)) {
  8627. $actNumber = intval($segmentTitleMatch[1]);
  8628. } else {
  8629. $actNumber = count($parsed_segments) + 1;
  8630. }
  8631. // 提取时长(仅保留数字)
  8632. $actDuration = 0;
  8633. if (preg_match('/时长[::]\s*([^\n]+)/u', $actContent, $actDurationMatch)) {
  8634. $actDurationStr = trim($actDurationMatch[1]);
  8635. // 提取数字部分(支持整数和小数)
  8636. if (preg_match('/([-+]?[0-9]*\.?[0-9]+)/', $actDurationStr, $numMatch)) {
  8637. $actDuration = (float)$numMatch[1];
  8638. }
  8639. }
  8640. // 清理act_content:去除时长、旁白音色等非分镜内容行
  8641. // 只保留分镜内容,并确保分镜标记使用【】格式
  8642. $cleaned_content = '';
  8643. $content_lines = explode("\n", $actContent);
  8644. $is_capturing = false; // 标记是否开始捕获分镜内容
  8645. $count = 0;
  8646. foreach ($content_lines as $line) {
  8647. $trimmed_line = trim($line);
  8648. // 跳过时长和旁白音色行
  8649. if (preg_match('/^(时长|旁白音色)[::]/u', $trimmed_line)) {
  8650. continue;
  8651. }
  8652. // 检测是否是分镜开始
  8653. // 支持格式:【分镜X】、【镜头X】、分镜X、镜头X
  8654. if (preg_match('/^(?:【)?(分镜|镜头)(\d+)(?:】)?/u', $trimmed_line, $shot_match)) {
  8655. $count++;
  8656. $is_capturing = true;
  8657. // 如果没有【】,则添加
  8658. if (!preg_match('/^【/u', $trimmed_line)) {
  8659. $trimmed_line = preg_replace('/^(分镜|镜头)(\d+)/u', '【$1$2】', $trimmed_line);
  8660. }
  8661. if ($count > 1) $trimmed_line = "\n".$trimmed_line;
  8662. }
  8663. // 如果已经开始捕获,则添加该行
  8664. if ($is_capturing && !empty($trimmed_line)) {
  8665. $cleaned_content .= $trimmed_line."\n";
  8666. }
  8667. }
  8668. $cleaned_content = str_replace(["@", "{", "}"], "", $cleaned_content);
  8669. $parsed_segments[] = [
  8670. 'act_number' => $actNumber,
  8671. 'act_title' => $actTitle,
  8672. 'act_duration' => $actDuration,
  8673. 'act_content' => $cleaned_content,
  8674. ];
  8675. }
  8676. if (empty($parsed_segments)) {
  8677. Utils::throwError('20003:无法解析生成的分段剧本,请检查格式');
  8678. }
  8679. // 获取角色和场景名称用于标记
  8680. $role_names = [];
  8681. $scene_names = [];
  8682. if (!empty($roles) && is_array($roles)) {
  8683. foreach ($roles as $role) {
  8684. $role_name = getProp($role, 'role', '');
  8685. if (!empty($role_name)) {
  8686. $role_names[] = $role_name;
  8687. }
  8688. }
  8689. }
  8690. if (!empty($scenes) && is_array($scenes)) {
  8691. foreach ($scenes as $scene) {
  8692. $scene_name = getProp($scene, 'scene', '');
  8693. if (!empty($scene_name)) {
  8694. $scene_names[] = $scene_name;
  8695. }
  8696. }
  8697. }
  8698. // 获取道具名称用于标记
  8699. $prop_names = [];
  8700. if (!empty($props) && is_array($props)) {
  8701. foreach ($props as $prop) {
  8702. $prop_name = getProp($prop, 'prop', '');
  8703. if (!empty($prop_name)) {
  8704. $prop_names[] = $prop_name;
  8705. }
  8706. }
  8707. }
  8708. // 对角色、场景、道具名称按长度降序排序(避免短名称先匹配到长名称的一部分)
  8709. usort($role_names, function($a, $b) {
  8710. return mb_strlen($b) - mb_strlen($a);
  8711. });
  8712. usort($scene_names, function($a, $b) {
  8713. return mb_strlen($b) - mb_strlen($a);
  8714. });
  8715. usort($prop_names, function($a, $b) {
  8716. return mb_strlen($b) - mb_strlen($a);
  8717. });
  8718. // 处理每个片段的 act_show_content
  8719. foreach ($parsed_segments as &$segment) {
  8720. $act_content = getProp($segment, 'act_content', '');
  8721. if (empty($act_content)) {
  8722. $segment['act_show_content'] = '';
  8723. continue;
  8724. }
  8725. $processed_content = $act_content;
  8726. // 先匹配场景(在"场景:"后面)
  8727. foreach ($scene_names as $scene_name) {
  8728. // 转义特殊字符
  8729. $escaped_scene = preg_quote($scene_name, '/');
  8730. // 匹配场景:后面的场景名称(可能换行,也可能不换行)
  8731. $pattern = '/(场景:[\s\r\n]*)(' . $escaped_scene . ')/u';
  8732. $processed_content = preg_replace_callback($pattern, function($matches) {
  8733. $prefix = $matches[1];
  8734. $scene_name = $matches[2];
  8735. // 检查是否已经被标记
  8736. if (strpos($scene_name, '{') === false && strpos($scene_name, '}') === false) {
  8737. return $prefix . '{' . $scene_name . '}';
  8738. }
  8739. return $matches[0];
  8740. }, $processed_content);
  8741. }
  8742. // 匹配道具(在"道具:"后面,逻辑与场景一致)
  8743. foreach ($prop_names as $prop_name) {
  8744. // 转义特殊字符
  8745. $escaped_prop = preg_quote($prop_name, '/');
  8746. // 匹配道具:后面的道具名称(可能换行,也可能不换行)
  8747. $pattern = '/(道具:[\s\r\n]*)(' . $escaped_prop . ')/u';
  8748. $processed_content = preg_replace_callback($pattern, function($matches) {
  8749. $full_match = $matches[0];
  8750. $prefix = $matches[1];
  8751. $prop_name = $matches[2];
  8752. // 检查是否已经被标记
  8753. if (strpos($prop_name, '{') === false && strpos($prop_name, '}') === false) {
  8754. return $prefix . '{' . $prop_name . '}';
  8755. }
  8756. return $full_match;
  8757. }, $processed_content);
  8758. }
  8759. // 再匹配角色名称(在"画面:"部分和"角色:"部分)
  8760. foreach ($role_names as $role_name) {
  8761. // 转义特殊字符
  8762. $escaped_role = preg_quote($role_name, '/');
  8763. // 1. 匹配"画面:"开始到换行之前的内容
  8764. $pattern = '/(画面:[^\n]*)/u';
  8765. $processed_content = preg_replace_callback($pattern, function($matches) use ($escaped_role) {
  8766. $line = $matches[1]; // 整行"画面:xxxxx"
  8767. // 在这一行中匹配所有角色名(避免重复标记)
  8768. $new_line = preg_replace(
  8769. '/(?<![{])(' . $escaped_role . ')(?![}])/u',
  8770. '{$1}',
  8771. $line
  8772. );
  8773. return $new_line;
  8774. }, $processed_content);
  8775. // 2. 匹配"角色:"或"出场角色:"这一整行内的角色名称
  8776. $pattern = '/((?:出场)?角色:[^\n]*)/u';
  8777. $processed_content = preg_replace_callback($pattern, function($matches) use ($escaped_role) {
  8778. $line = $matches[1]; // 整行"角色:xxxxx"或"出场角色:xxxxx"
  8779. // 在这一行中匹配所有角色名(避免重复标记)
  8780. $new_line = preg_replace(
  8781. '/(?<![{])(' . $escaped_role . ')(?![}])/u',
  8782. '{$1}',
  8783. $line
  8784. );
  8785. return $new_line;
  8786. }, $processed_content);
  8787. }
  8788. // 匹配道具名称(在"画面:"部分,逻辑与角色一致)
  8789. foreach ($prop_names as $prop_name) {
  8790. // 转义特殊字符
  8791. $escaped_prop = preg_quote($prop_name, '/');
  8792. // 匹配"画面:"开始到换行之前的内容
  8793. $pattern = '/(画面:[^\n]*)/u';
  8794. $processed_content = preg_replace_callback($pattern, function($matches) use ($escaped_prop) {
  8795. $line = $matches[1]; // 整行"画面:xxxxx"
  8796. // 在这一行中匹配所有道具名(避免重复标记)
  8797. $new_line = preg_replace(
  8798. '/(?<![{])(' . $escaped_prop . ')(?![}])/u',
  8799. '{$1}',
  8800. $line
  8801. );
  8802. return $new_line;
  8803. }, $processed_content);
  8804. }
  8805. // 匹配角色名称(在"运镜:"部分)
  8806. foreach ($role_names as $role_name) {
  8807. // 转义特殊字符
  8808. $escaped_role = preg_quote($role_name, '/');
  8809. // 匹配"运镜:"开始到换行之前的内容
  8810. $pattern = '/(运镜:[^\n]*)/u';
  8811. $processed_content = preg_replace_callback($pattern, function($matches) use ($escaped_role) {
  8812. $line = $matches[1]; // 整行"运镜:xxxxx"
  8813. // 在这一行中匹配所有角色名(避免重复标记)
  8814. $new_line = preg_replace(
  8815. '/(?<![{])(' . $escaped_role . ')(?![}])/u',
  8816. '{$1}',
  8817. $line
  8818. );
  8819. return $new_line;
  8820. }, $processed_content);
  8821. }
  8822. // 匹配道具名称(在"运镜:"部分)
  8823. foreach ($prop_names as $prop_name) {
  8824. // 转义特殊字符
  8825. $escaped_prop = preg_quote($prop_name, '/');
  8826. // 匹配"运镜:"开始到换行之前的内容
  8827. $pattern = '/(运镜:[^\n]*)/u';
  8828. $processed_content = preg_replace_callback($pattern, function($matches) use ($escaped_prop) {
  8829. $line = $matches[1]; // 整行"运镜:xxxxx"
  8830. // 在这一行中匹配所有道具名(避免重复标记)
  8831. $new_line = preg_replace(
  8832. '/(?<![{])(' . $escaped_prop . ')(?![}])/u',
  8833. '{$1}',
  8834. $line
  8835. );
  8836. return $new_line;
  8837. }, $processed_content);
  8838. }
  8839. // 处理【镜头X】或【分镜X】重新编号为从1开始的连续整数【镜头X】
  8840. $shot_counter = 0;
  8841. $processed_content = preg_replace_callback(
  8842. '/【(?:镜头|分镜)(\d+)】/u',
  8843. function($matches) use (&$shot_counter) {
  8844. $shot_counter++;
  8845. return '【镜头' . $shot_counter . '】';
  8846. },
  8847. $processed_content
  8848. );
  8849. $segment['act_show_content'] = $processed_content;
  8850. }
  8851. unset($segment); // 解除引用
  8852. // 保存到数据库
  8853. $now = date('Y-m-d H:i:s');
  8854. $saved_acts = []; // 用于记录保存后的片段信息
  8855. if ($act_id > 0) {
  8856. // 单个片段模式:更新单条记录
  8857. if (count($parsed_segments) > 0) {
  8858. $new_segment = $parsed_segments[0];
  8859. $update_data = [
  8860. 'act_content' => getProp($new_segment, 'act_content', ''),
  8861. 'act_show_content' => getProp($new_segment, 'act_show_content', ''),
  8862. 'act_title' => getProp($new_segment, 'act_title', ''),
  8863. 'act_duration' => getProp($new_segment, 'act_duration', 0),
  8864. 'updated_at' => $now,
  8865. ];
  8866. DB::table('mp_episode_segments')
  8867. ->where('id', $act_id)
  8868. ->update($update_data);
  8869. // 获取更新后的记录
  8870. $updated_act = DB::table('mp_episode_segments')
  8871. ->where('id', $act_id)
  8872. ->first();
  8873. if ($updated_act) {
  8874. $saved_acts[] = [
  8875. 'act_id' => $updated_act->id,
  8876. 'act_number' => $updated_act->act_number,
  8877. 'act_duration' => $updated_act->video_duration ? $updated_act->video_duration : $updated_act->act_duration,
  8878. 'act_content' => $updated_act->act_content,
  8879. 'act_show_content' => $updated_act->act_show_content,
  8880. 'video_url' => $updated_act->video_url ?? '',
  8881. 'video_duration' => $updated_act->video_duration ?? 0,
  8882. 'video_time_point_start' => $updated_act->video_time_point_start ?? 0,
  8883. 'video_time_point_end' => $updated_act->video_time_point_end ?? 0,
  8884. ];
  8885. }
  8886. dLog('deepseek')->info('单个片段重新生成成功', ['act_id' => $act_id]);
  8887. }
  8888. } else {
  8889. // 整个剧集模式:删除原有记录并批量保存新记录
  8890. DB::beginTransaction();
  8891. try {
  8892. // 删除原有的所有片段记录
  8893. DB::table('mp_episode_segments')
  8894. ->where('episode_id', $target_episode_id)
  8895. ->delete();
  8896. // 批量插入新的片段记录
  8897. $segments_to_insert = [];
  8898. foreach ($parsed_segments as $index => $segment) {
  8899. $act_number = $index + 1;
  8900. $segments_to_insert[] = [
  8901. 'anime_id' => $target_anime_id,
  8902. 'episode_id' => $target_episode_id,
  8903. 'episode_number' => $target_episode_number,
  8904. 'act_number' => $act_number,
  8905. 'act_title' => getProp($segment, 'act_title', ''),
  8906. 'act_duration' => getProp($segment, 'act_duration', 0),
  8907. 'act_content' => getProp($segment, 'act_content', ''),
  8908. 'act_show_content' => getProp($segment, 'act_show_content', ''),
  8909. 'created_at' => $now,
  8910. 'updated_at' => $now,
  8911. ];
  8912. }
  8913. if (!empty($segments_to_insert)) {
  8914. DB::table('mp_episode_segments')->insert($segments_to_insert);
  8915. }
  8916. DB::commit();
  8917. // 查询保存后的所有片段记录
  8918. $saved_segments = DB::table('mp_episode_segments')
  8919. ->where('episode_id', $target_episode_id)
  8920. ->orderBy('act_number')
  8921. ->get();
  8922. foreach ($saved_segments as $saved_segment) {
  8923. $saved_acts[] = [
  8924. 'act_id' => $saved_segment->id,
  8925. 'act_number' => $saved_segment->act_number,
  8926. 'act_duration' => $saved_segment->video_duration ? $saved_segment->video_duration : $saved_segment->act_duration,
  8927. 'act_content' => $saved_segment->act_content,
  8928. 'act_show_content' => $saved_segment->act_show_content,
  8929. 'video_url' => $saved_segment->video_url ?? '',
  8930. 'video_duration' => $saved_segment->video_duration ?? 0,
  8931. 'video_time_point_start' => $saved_segment->video_time_point_start ?? 0,
  8932. 'video_time_point_end' => $saved_segment->video_time_point_end ?? 0,
  8933. ];
  8934. }
  8935. dLog('deepseek')->info('整个剧集片段重新生成成功', [
  8936. 'episode_id' => $target_episode_id,
  8937. 'segments_count' => count($segments_to_insert)
  8938. ]);
  8939. } catch (\Exception $e) {
  8940. DB::rollBack();
  8941. dLog('deepseek')->error('保存重新生成的片段失败: ' . $e->getMessage());
  8942. Utils::throwError('20003:保存失败,请重试');
  8943. }
  8944. }
  8945. // 构造返回数据结构(参考AnimeService返回格式,但不包含products)
  8946. return [
  8947. 'anime_id' => $target_anime_id,
  8948. 'episode_id' => $target_episode_id,
  8949. 'title' => getProp($episode, 'title', ''),
  8950. 'episode_number' => $target_episode_number,
  8951. 'is_generated' => getProp($episode, 'is_generated', 0),
  8952. 'acts' => $saved_acts,
  8953. ];
  8954. } catch (\Exception $e) {
  8955. dLog('deepseek')->error('重新生成分段剧本失败: ' . $e->getMessage());
  8956. throw $e;
  8957. }
  8958. }
  8959. private function buildSingleEpisodeFromScript(array $script_arr, string $fullContent, string $animeName = ''): array
  8960. {
  8961. $episode_arr = [
  8962. 'episode_title' => $animeName, // 单剧集默认使用剧本名
  8963. 'intro' => getProp($script_arr, 'intro'),
  8964. 'art_style' => getProp($script_arr, 'art_style'),
  8965. 'roles' => is_array(getProp($script_arr, 'roles')) ? getProp($script_arr, 'roles') : [],
  8966. 'scenes' => is_array(getProp($script_arr, 'scenes')) ? getProp($script_arr, 'scenes') : [],
  8967. 'acts' => is_array(getProp($script_arr, 'acts')) ? getProp($script_arr, 'acts') : [],
  8968. ];
  8969. if (empty($episode_arr['acts'])) {
  8970. $fallback_episode_arr = handleEpisodeContent($fullContent);
  8971. if (!empty($fallback_episode_arr['acts'])) {
  8972. $episode_arr = array_merge($fallback_episode_arr, [
  8973. 'intro' => getProp($fallback_episode_arr, 'intro') ?: getProp($script_arr, 'intro'),
  8974. 'art_style' => getProp($fallback_episode_arr, 'art_style') ?: getProp($script_arr, 'art_style'),
  8975. 'roles' => !empty(getProp($fallback_episode_arr, 'roles')) ? getProp($fallback_episode_arr, 'roles') : getProp($script_arr, 'roles', []),
  8976. 'scenes' => !empty(getProp($fallback_episode_arr, 'scenes')) ? getProp($fallback_episode_arr, 'scenes') : getProp($script_arr, 'scenes', []),
  8977. ]);
  8978. }
  8979. }
  8980. // if (!getProp($episode_arr, 'episode_title')) {
  8981. // $episode_title = '';
  8982. // $episodes = getProp($script_arr, 'episodes', []);
  8983. // if (is_array($episodes) && !empty($episodes[0]['title'])) {
  8984. // $episode_title = '第1集:' . $episodes[0]['title'];
  8985. // }
  8986. // if (!$episode_title) {
  8987. // $episode_title = '第1集:' . ($animeName ?: '未命名');
  8988. // }
  8989. // $episode_arr['episode_title'] = $episode_title;
  8990. // }
  8991. return $episode_arr;
  8992. }
  8993. private function saveEpisodeVersionData(
  8994. int $anime_id,
  8995. int $episode_number,
  8996. array $episode_arr,
  8997. array $existing_roles,
  8998. array $existing_scenes,
  8999. array $existing_props,
  9000. $current_episode,
  9001. $base_episode,
  9002. bool $is_regenerate_version,
  9003. string $content,
  9004. string $now,
  9005. array $ref_products = []
  9006. ): array {
  9007. $generated_roles = isset($episode_arr['roles']) && is_array($episode_arr['roles']) ? $episode_arr['roles'] : [];
  9008. $generated_scenes = isset($episode_arr['scenes']) && is_array($episode_arr['scenes']) ? $episode_arr['scenes'] : [];
  9009. $generated_props = isset($episode_arr['props']) && is_array($episode_arr['props']) ? $episode_arr['props'] : [];
  9010. $merged_roles = $generated_roles;
  9011. $merged_scenes = $generated_scenes;
  9012. $merged_props = $generated_props;
  9013. // $merged_roles = $this->mergeEpisodeResourceItems($existing_roles, $generated_roles, 'role');
  9014. // $merged_scenes = $this->mergeEpisodeResourceItems($existing_scenes, $generated_scenes, 'scene');
  9015. // 获取 mp_animes 表中的全局 roles 和 scenes 用于对比
  9016. $anime = DB::table('mp_animes')->where('id', $anime_id)->first();
  9017. $ace_mode = getProp($anime, 'ace_mode');
  9018. dLog('deepseek')->info('ace_mode', ['ace_mode' => $ace_mode]);
  9019. $global_roles = json_decode(getProp($anime, 'roles'), true);
  9020. $global_scenes = json_decode(getProp($anime, 'scenes'), true);
  9021. $global_roles = is_array($global_roles) ? $global_roles : [];
  9022. $global_scenes = is_array($global_scenes) ? $global_scenes : [];
  9023. $art_style = getProp($episode_arr, 'art_style') ?: getProp($anime, 'art_style');
  9024. $art_style_type = getProp($anime, 'art_style_type');
  9025. // 检查并创建 roles 的图片生成任务
  9026. // $merged_roles = $this->checkAndCreateImageTasks($merged_roles, $global_roles, 'role', $art_style);
  9027. // 检查并创建 scenes 的图片生成任务
  9028. // $merged_scenes = $this->checkAndCreateImageTasks($merged_scenes, $global_scenes, 'scene', $art_style);
  9029. $arr = $this->batchSetEpisodeRolesAndScenesImg($anime_id, $episode_number, $merged_roles, $merged_scenes, $merged_props, $ref_products, $art_style_type);
  9030. if ($arr && is_array($arr)) {
  9031. $merged_roles = $arr['roles'];
  9032. $merged_scenes = $arr['scenes'];
  9033. $merged_props = isset($arr['props']) ? $arr['props'] : [];
  9034. }
  9035. // 确保数据是数组类型
  9036. if (!is_array($merged_roles)) {
  9037. dLog('deepseek')->error('merged_roles不是数组类型', [
  9038. 'type' => gettype($merged_roles),
  9039. 'value' => $merged_roles
  9040. ]);
  9041. $merged_roles = [];
  9042. }
  9043. if (!is_array($merged_scenes)) {
  9044. dLog('deepseek')->error('merged_scenes不是数组类型', [
  9045. 'type' => gettype($merged_scenes),
  9046. 'value' => $merged_scenes
  9047. ]);
  9048. $merged_scenes = [];
  9049. }
  9050. if (!is_array($merged_props)) {
  9051. dLog('deepseek')->error('merged_props不是数组类型', [
  9052. 'type' => gettype($merged_props),
  9053. 'value' => $merged_props
  9054. ]);
  9055. $merged_props = [];
  9056. }
  9057. // 同步新出现的主体和场景到全局
  9058. // $this->syncNewResourcesToGlobal($anime_id, $merged_roles, $merged_scenes, $global_roles, $global_scenes);
  9059. $role_arr = [];
  9060. foreach ($merged_roles as $item) {
  9061. $role_arr[$item['role']] = $item;
  9062. }
  9063. $version_count = DB::table('mp_anime_episodes')
  9064. ->where('anime_id', $anime_id)
  9065. ->where('episode_number', $episode_number)
  9066. ->count('id');
  9067. $episode = [
  9068. 'anime_id' => $anime_id,
  9069. 'episode_number' => $episode_number,
  9070. 'title' => getProp($episode_arr, 'episode_title'),
  9071. 'content' => $content,
  9072. 'intro' => getProp($episode_arr, 'intro'),
  9073. 'art_style' => getProp($episode_arr, 'art_style'),
  9074. 'roles' => json_encode($merged_roles, 256),
  9075. 'scenes' => json_encode($merged_scenes, 256),
  9076. 'props' => json_encode($merged_props, 256),
  9077. 'generate_status' => '待执行',
  9078. 'generate_at' => $now,
  9079. 'is_default' => 1,
  9080. 'updated_at' => $now,
  9081. ];
  9082. $del_flag = false;
  9083. if ($is_regenerate_version) {
  9084. DB::table('mp_anime_episodes')
  9085. ->where('anime_id', $anime_id)
  9086. ->where('episode_number', $episode_number)
  9087. ->update(['is_default' => 0, 'updated_at' => $now]);
  9088. $episode['sequence'] = $version_count + 1;
  9089. $episode['created_at'] = $now;
  9090. $episode['cpid'] = Site::getCpid();
  9091. $episode_id = DB::table('mp_anime_episodes')->insertGetId($episode);
  9092. if (!$episode_id) {
  9093. Utils::throwError('20003:创建剧集版本失败');
  9094. }
  9095. } else {
  9096. $target_episode = $current_episode ?: $base_episode;
  9097. if ($target_episode) {
  9098. $episode_id = getProp($target_episode, 'id');
  9099. $episode['sequence'] = getProp($target_episode, 'sequence', 1);
  9100. DB::table('mp_anime_episodes')
  9101. ->where('anime_id', $anime_id)
  9102. ->where('episode_number', $episode_number)
  9103. ->where('id', '<>', $episode_id)
  9104. ->update(['is_default' => 0, 'updated_at' => $now]);
  9105. $boolen = DB::table('mp_anime_episodes')->where('id', $episode_id)->update($episode);
  9106. if ($boolen === false) {
  9107. logDB('deepseek', 'error', '分集内容保存失败', ['anime_id' => $anime_id, 'episode_id' => $episode_id]);
  9108. Utils::throwError('20003:分集内容保存失败');
  9109. }
  9110. $del_flag = true;
  9111. } else {
  9112. DB::table('mp_anime_episodes')
  9113. ->where('anime_id', $anime_id)
  9114. ->where('episode_number', $episode_number)
  9115. ->update(['is_default' => 0, 'updated_at' => $now]);
  9116. $episode['sequence'] = $version_count > 0 ? ($version_count + 1) : 1;
  9117. $episode['created_at'] = $now;
  9118. $episode['cpid'] = Site::getCpid();
  9119. $episode_id = DB::table('mp_anime_episodes')->insertGetId($episode);
  9120. if (!$episode_id) {
  9121. logDB('deepseek', 'error', '分集内容保存失败', ['anime_id' => $anime_id, 'episode_number' => $episode_number]);
  9122. Utils::throwError('20003:分集内容保存失败');
  9123. }
  9124. }
  9125. }
  9126. $segments = [];
  9127. $acts = isset($episode_arr['acts']) && is_array($episode_arr['acts']) ? $episode_arr['acts'] : [];
  9128. // 如果是第2集及以后,获取上一集的主体音色数据
  9129. $previous_roles_voice = [];
  9130. if ($episode_number >= 2) {
  9131. $previous_episode = DB::table('mp_anime_episodes')
  9132. ->where('anime_id', $anime_id)
  9133. ->where('episode_number', $episode_number - 1)
  9134. ->where('is_default', 1)
  9135. ->first();
  9136. if ($previous_episode) {
  9137. $previous_roles = json_decode(getProp($previous_episode, 'roles'), true);
  9138. if (is_array($previous_roles)) {
  9139. foreach ($previous_roles as $prev_role) {
  9140. $role_name = getProp($prev_role, 'role');
  9141. if ($role_name) {
  9142. $previous_roles_voice[$role_name] = [
  9143. 'voice_name' => getProp($prev_role, 'voice_name'),
  9144. 'voice_type' => getProp($prev_role, 'voice_type'),
  9145. 'voice_audio_url' => getProp($prev_role, 'voice_audio_url'),
  9146. ];
  9147. $voice_prompt = getProp($prev_role, 'voice_prompt');
  9148. if ($voice_prompt) {
  9149. $previous_roles_voice[$role_name]['voice_prompt'] = $voice_prompt;
  9150. }
  9151. }
  9152. }
  9153. }
  9154. }
  9155. }
  9156. // 将继承的音色数据同步到当前集的主体列表
  9157. if (!empty($previous_roles_voice)) {
  9158. foreach ($merged_roles as &$role) {
  9159. $role_name = getProp($role, 'role');
  9160. if ($role_name && isset($previous_roles_voice[$role_name])) {
  9161. // 强制继承上一集的音色数据
  9162. $role['voice_name'] = $previous_roles_voice[$role_name]['voice_name'];
  9163. $role['voice_type'] = $previous_roles_voice[$role_name]['voice_type'];
  9164. $role['voice_audio_url'] = $previous_roles_voice[$role_name]['voice_audio_url'];
  9165. // 如果上一集有 voice_prompt 则继承,否则跳过
  9166. if (!empty($previous_roles_voice[$role_name]['voice_prompt'])) {
  9167. $role['voice_prompt'] = $previous_roles_voice[$role_name]['voice_prompt'];
  9168. }
  9169. }
  9170. }
  9171. unset($role); // 解除引用
  9172. // 更新 role_arr 以便后续使用
  9173. $role_arr = [];
  9174. foreach ($merged_roles as $item) {
  9175. $role_arr[$item['role']] = $item;
  9176. }
  9177. }
  9178. $emotion_list = DB::table('mp_emotion_list')->where('is_enabled', 1)->pluck('emotion_name', 'emotion_code')->toArray();
  9179. $emotion_list = array_flip($emotion_list);
  9180. foreach ($acts as $act) {
  9181. $act_segments = getProp($act, 'segments', []);
  9182. if (!is_array($act_segments)) {
  9183. continue;
  9184. }
  9185. if ((int)$ace_mode === 1) {
  9186. // ace_mode=1: 按幕合并 segment_content,每幕只存一条记录
  9187. $merged_parts = [];
  9188. foreach ($act_segments as $seg) {
  9189. $seg_num = getProp($seg, 'segment_number');
  9190. $seg_content = getProp($seg, 'segment_content');
  9191. $merged_parts[] = "【分镜{$seg_num}】\n{$seg_content}\n";
  9192. }
  9193. $act_content = implode("\n", $merged_parts);
  9194. $act_content = str_replace(["@", "{", "}"], "", $act_content);
  9195. $segments[] = [
  9196. 'anime_id' => $anime_id,
  9197. 'episode_id' => $episode_id,
  9198. 'episode_number' => $episode_number,
  9199. 'act_number' => getProp($act, 'act_number'),
  9200. 'act_title' => getProp($act, 'act_title'),
  9201. 'act_duration' => getProp($act, 'act_duration'),
  9202. 'act_content' => $act_content,
  9203. 'created_at' => $now,
  9204. 'updated_at' => $now
  9205. ];
  9206. } else {
  9207. foreach ($act_segments as $segment) {
  9208. $voice_actor = getProp($segment, 'voice_actor');
  9209. // 优先从上一集继承音色数据
  9210. $timbre_info = [];
  9211. if (isset($previous_roles_voice[$voice_actor])) {
  9212. // 从上一集继承音色数据
  9213. $timbre_info = $previous_roles_voice[$voice_actor];
  9214. } elseif (isset($role_arr[$voice_actor])) {
  9215. // 使用当前集的主体音色数据
  9216. $timbre_info = $role_arr[$voice_actor];
  9217. }
  9218. $voice_type = getProp($timbre_info, 'voice_type');
  9219. $timbre_emotion = DB::table('mp_timbres')->where('timbre_type', $voice_type)->value('emotion');
  9220. if ($timbre_emotion) {
  9221. $timbre_emotion = explode(',', $timbre_emotion);
  9222. } else {
  9223. $timbre_emotion = [];
  9224. }
  9225. $emotion = getProp($segment, 'emotion', '中性');
  9226. if (!in_array($emotion, $timbre_emotion)) {
  9227. $emotion = '中性';
  9228. }
  9229. $emotion_type = isset($emotion_list[$emotion]) ? $emotion_list[$emotion] : 'neutral';
  9230. $segments[] = [
  9231. 'anime_id' => $anime_id,
  9232. 'episode_id' => $episode_id,
  9233. 'episode_number' => $episode_number,
  9234. 'act_number' => getProp($act, 'act_number'),
  9235. 'act_title' => getProp($act, 'act_title'),
  9236. 'act_duration' => getProp($act, 'act_duration'),
  9237. 'segment_id' => getProp($segment, 'segment_id'),
  9238. 'segment_number' => getProp($segment, 'segment_number'),
  9239. 'segment_content' => getProp($segment, 'segment_content'),
  9240. 'description' => getProp($segment, 'description'),
  9241. 'composition' => getProp($segment, 'composition'),
  9242. 'camera_movement' => getProp($segment, 'camera_movement'),
  9243. 'voice_actor' => $voice_actor,
  9244. 'dialogue' => getProp($segment, 'dialogue'),
  9245. 'frame_type' => getProp($segment, 'frame_type'),
  9246. 'scene' => getProp($segment, 'scene'),
  9247. 'characters' => getProp($segment, 'characters'),
  9248. 'tail_frame' => getProp($segment, 'tail_frame'),
  9249. 'voice_name' => getProp($timbre_info, 'voice_name'),
  9250. 'voice_type' => $voice_type,
  9251. 'voice_audio_url' => getProp($timbre_info, 'voice_audio_url'),
  9252. 'emotion' => $emotion,
  9253. 'emotion_type' => $emotion_type,
  9254. 'gender' => getProp($segment, 'gender', '0'),
  9255. 'speed_ratio' => getProp($segment, 'speed_ratio', 0),
  9256. 'loudness_ratio' => getProp($segment, 'loudness_ratio', 0),
  9257. 'emotion_scale' => getProp($segment, 'emotion_scale', 4),
  9258. 'pitch' => getProp($segment, 'pitch', 0),
  9259. 'created_at' => $now,
  9260. 'updated_at' => $now
  9261. ];
  9262. }
  9263. }
  9264. }
  9265. if ($segments) {
  9266. if ($del_flag) {
  9267. DB::table('mp_episode_segments')
  9268. ->where('anime_id', $anime_id)
  9269. ->where('episode_id', $episode_id)
  9270. ->delete();
  9271. }
  9272. $boolen = DB::table('mp_episode_segments')->insert($segments);
  9273. if (!$boolen) {
  9274. logDB('deepseek', 'error', '分镜内容保存失败', ['anime_id' => $anime_id, 'episode_id' => $episode_id, 'segments_count' => count($segments)]);
  9275. Utils::throwError('20003:分镜内容保存失败');
  9276. }
  9277. }
  9278. dLog('deepseek')->info('segments', $segments);
  9279. // ace_mode=1: acts 返回值按合并格式调整
  9280. if ((int)$ace_mode === 1) {
  9281. $table_act_data = DB::table('mp_episode_segments')->where('episode_id', $episode_id)->orderBy('act_number')->select('id as act_id', 'act_number')->get();
  9282. $act_map = [];
  9283. foreach ($table_act_data as $item) {
  9284. $act_map[getProp($item, 'act_number')] = getProp($item, 'act_id');
  9285. }
  9286. $restructured_acts = [];
  9287. foreach ($acts as $act) {
  9288. $act_segments = getProp($act, 'segments', []);
  9289. if (!is_array($act_segments)) {
  9290. continue;
  9291. }
  9292. $merged_parts = [];
  9293. foreach ($act_segments as $seg) {
  9294. $seg_num = getProp($seg, 'segment_number');
  9295. $seg_content = getProp($seg, 'segment_content');
  9296. $merged_parts[] = "【分镜{$seg_num}】{$seg_content}\n";
  9297. }
  9298. $act_content = implode("\n", $merged_parts);
  9299. $act_content = str_replace(["@", "{", "}"], "", $act_content);
  9300. $restructured_acts[] = [
  9301. 'anime_id' => $anime_id,
  9302. 'episode_id' => $episode_id,
  9303. 'episode_number' => $episode_number,
  9304. 'act_id' => isset($act_map[getProp($act, 'act_number')]) ? $act_map[getProp($act, 'act_number')] : 0,
  9305. 'act_number' => getProp($act, 'act_number'),
  9306. 'act_title' => getProp($act, 'act_title'),
  9307. 'act_duration' => getProp($act, 'act_duration'),
  9308. 'act_content' => $act_content,
  9309. 'created_at' => $now,
  9310. 'updated_at' => $now,
  9311. ];
  9312. }
  9313. $acts = $restructured_acts;
  9314. }
  9315. $episode['episode_id'] = $episode_id;
  9316. $episode['roles'] = $merged_roles;
  9317. $episode['scenes'] = $merged_scenes;
  9318. $episode['props'] = $merged_props;
  9319. $episode['acts'] = $acts;
  9320. return [
  9321. 'episode' => $episode,
  9322. 'episode_id' => $episode_id,
  9323. 'merged_roles' => $merged_roles,
  9324. 'merged_scenes' => $merged_scenes,
  9325. 'merged_props' => $merged_props,
  9326. ];
  9327. }
  9328. private function buildEpisodeItemContent(array $items, string $nameKey): string
  9329. {
  9330. $content = '';
  9331. foreach ($items as $item) {
  9332. $name = trim((string)getProp($item, $nameKey));
  9333. if ($name === '' || $name === '旁白') {
  9334. continue;
  9335. }
  9336. $description = trim((string)getProp($item, 'description'));
  9337. $content .= $name . ': ' . $description;
  9338. if ($nameKey == 'role') {
  9339. $pic_prompt = trim(getProp($item, 'pic_prompt'));
  9340. if ($pic_prompt) $content .= '{'.$pic_prompt.'}';
  9341. $voice_name = trim(getProp($item, 'voice_name'));
  9342. if ($voice_name) $content .= '{{'.$voice_name.'}}';
  9343. $voice_prompt = trim(getProp($item, 'voice_prompt'));
  9344. if ($voice_prompt) $content .= '{{'.$voice_prompt.'}}';
  9345. }else {
  9346. $pic_prompt = trim(getProp($item, 'pic_prompt'));
  9347. if ($pic_prompt) $content .= '{'.$pic_prompt.'}';
  9348. }
  9349. $content .= "\n";
  9350. }
  9351. return trim($content);
  9352. }
  9353. private function getEpisodeChatContent($animeId, $sequence, $content) {
  9354. if ((int)$sequence <= 0) {
  9355. return [];
  9356. }
  9357. $origin_content = '';
  9358. if ($content) {
  9359. $origin_content = '本集剧情内容:'.$content;
  9360. }else {
  9361. $origin_content = DB::table('mp_animes')->where('id', $animeId)->value('content');
  9362. if ($origin_content) $origin_content = '剧本全文内容:'.$origin_content;
  9363. }
  9364. $ace_mode = DB::table('mp_animes')->where('id', $animeId)->value('ace_mode');
  9365. // 获取分集信息
  9366. $episode = DB::table('mp_anime_episodes')
  9367. ->where('anime_id', $animeId)
  9368. ->where('episode_number', $sequence)
  9369. ->where('is_default', 1)
  9370. ->first();
  9371. if (!$episode) {
  9372. return [];
  9373. }
  9374. $episode_id = getProp($episode, 'id');
  9375. $episode_number = getProp($episode, 'episode_number');
  9376. $title = getProp($episode, 'title');
  9377. $intro = getProp($episode, 'intro');
  9378. $art_style = getProp($episode, 'art_style');
  9379. $roles = json_decode(getProp($episode, 'roles'), true) ?: [];
  9380. $scenes = json_decode(getProp($episode, 'scenes'), true) ?: [];
  9381. // 组装纯文本内容
  9382. $content = '';
  9383. // 添加分集标题和梗概
  9384. $content .= "###第{$episode_number}集:{$title}\n\n";
  9385. $content .= "###故事梗概\n";
  9386. $content .= trim($intro) . "\n\n";
  9387. // 添加美术风格
  9388. $content .= "###美术风格\n";
  9389. $content .= trim($art_style) . "\n\n";
  9390. // 添加主体列表
  9391. $content .= "###主体列表\n";
  9392. $pangbai_voice_prompt = "";
  9393. foreach ($roles as $role) {
  9394. $name = getProp($role, 'role');
  9395. $description = getProp($role, 'description');
  9396. $pic_prompt = getProp($role, 'pic_prompt');
  9397. $voice_prompt = getProp($role, 'voice_prompt');
  9398. $voice_name = getProp($role, 'voice_name');
  9399. $content .= "{$name}: {$description}";
  9400. if ($pic_prompt) $content .= '{'.$pic_prompt.'}';
  9401. if ($voice_prompt) $content .= '{{'.$voice_prompt.'}}';
  9402. if ($voice_name) $content .= '{{'.$voice_name.'}}';
  9403. if ($name == '旁白') $pangbai_voice_prompt = $voice_prompt;
  9404. $content .= "\n";
  9405. }
  9406. $content .= "\n";
  9407. // 添加场景列表
  9408. $content .= "###场景列表\n";
  9409. foreach ($scenes as $scene) {
  9410. $name = getProp($scene, 'scene');
  9411. $description = getProp($scene, 'description');
  9412. $pic_prompt = getProp($scene, 'pic_prompt');
  9413. $content .= "{$name}: {$description}";
  9414. if ($pic_prompt) $content .= '{'.$pic_prompt.'}';
  9415. $content .= "\n";
  9416. }
  9417. $content .= "\n";
  9418. // 获取分镜信息
  9419. $segments = DB::table('mp_episode_segments')
  9420. ->where('anime_id', $animeId)
  9421. ->where('episode_id', $episode_id)
  9422. ->orderBy('segment_number')
  9423. ->get();
  9424. if ($segments && count($segments) > 0) {
  9425. if ((int)$ace_mode === 1) {
  9426. $content .= "###分段剧本\n";
  9427. // 获取片段数量
  9428. $act_count = DB::table('mp_episode_segments')->where('anime_id', $animeId)->where('episode_id', $episode_id)->groupBy('act_number')->count('act_number');
  9429. $content .= "片段数量:{$act_count}\n";
  9430. if ($pangbai_voice_prompt) $content .= "旁白音色:{$pangbai_voice_prompt}\n";
  9431. $content .= "\n";
  9432. }else {
  9433. $content .= "###分镜剧本\n";
  9434. }
  9435. // 按幕分组
  9436. $currentAct = null;
  9437. foreach ($segments as $segment) {
  9438. $act_number = getProp($segment, 'act_number');
  9439. $act_title = getProp($segment, 'act_title');
  9440. $act_duration = getProp($segment, 'act_duration');
  9441. $segment_number = getProp($segment, 'segment_number');
  9442. $segment_content = getProp($segment, 'segment_content');
  9443. $scene_description = getProp($segment, 'scene_description');
  9444. $scene_name = getProp($segment, 'scene_name');
  9445. $composition = getProp($segment, 'composition');
  9446. $camera_movement = getProp($segment, 'camera_movement');
  9447. $voice_type = getProp($segment, 'voice_type');
  9448. $characters = getProp($segment, 'characters');
  9449. $dialogue = getProp($segment, 'dialogue');
  9450. $frame_type = getProp($segment, 'frame_type');
  9451. $tail_frame_description = getProp($segment, 'tail_frame_description');
  9452. // 判断是否是全能模式
  9453. if ((int)$ace_mode === 1) { // 全能模式
  9454. // 如果是新的分段,添加分段标题
  9455. if ($act_number !== $currentAct) {
  9456. $currentAct = $act_number;
  9457. $content .= "##{$act_title}\n";
  9458. $content .= "时长:{$act_duration}s\n";
  9459. }
  9460. }else {
  9461. // 如果是新的幕,添加幕标题
  9462. if ($act_number !== $currentAct) {
  9463. $currentAct = $act_number;
  9464. $content .= "##第{$act_number}幕:{$scene_name}\n";
  9465. }
  9466. }
  9467. // 添加分镜信息
  9468. $content .= "分镜{$segment_number}\n";
  9469. $content .= "分镜内容:\n{$segment_content}\n";
  9470. // $content .= "画面描述:{$scene_description}\n";
  9471. // $content .= "场景:{$scene_name}\n";
  9472. // $content .= "构图设计:{$composition}\n";
  9473. // $content .= "运镜调度:{$camera_movement}\n";
  9474. // if (!empty($voice_type)) {
  9475. // $content .= "配音角色:{$voice_type}\n";
  9476. // }
  9477. // if (!empty($characters)) {
  9478. // $content .= "出镜角色:{$characters}\n";
  9479. // }
  9480. // if (!empty($dialogue)) {
  9481. // $content .= "台词内容:\"{$dialogue}\"\n";
  9482. // }
  9483. // $content .= "画面类型:{$frame_type}\n";
  9484. // $content .= "尾帧描述:{$tail_frame_description}\n";
  9485. $content .= "\n";
  9486. }
  9487. }
  9488. $content = trim($content);
  9489. if($content) $content = "上集剧本内容:\n{$content}";
  9490. return [
  9491. [
  9492. 'role' => 'user',
  9493. 'content' => $origin_content
  9494. ],
  9495. [
  9496. 'role' => 'assistant',
  9497. 'content' => $content
  9498. ]
  9499. ];
  9500. }
  9501. private function getEpisodeChatMessages($animeId, $sequence): array
  9502. {
  9503. if ((int)$sequence <= 0) {
  9504. return [];
  9505. }
  9506. return DB::table('mp_anime_records')
  9507. ->where('anime_id', $animeId)
  9508. ->where('sequence', $sequence)
  9509. ->where('episode_id', '>', 0)
  9510. ->select('role', 'content')
  9511. ->orderBy('created_at')
  9512. ->orderBy('id')
  9513. ->get()
  9514. ->map(function ($value) {
  9515. return (array)$value;
  9516. })
  9517. ->toArray();
  9518. }
  9519. private function mergeEpisodeResourceItems(array $existingItems, array $generatedItems, string $nameKey): array
  9520. {
  9521. $existingMap = [];
  9522. foreach ($existingItems as $item) {
  9523. $itemKey = $this->normalizeEpisodeResourceKey(getProp($item, $nameKey));
  9524. if ($itemKey === '') {
  9525. continue;
  9526. }
  9527. $existingMap[$itemKey] = $item;
  9528. }
  9529. if (!$generatedItems) {
  9530. return array_values($existingItems);
  9531. }
  9532. $merged = [];
  9533. foreach ($generatedItems as $item) {
  9534. $itemKey = $this->normalizeEpisodeResourceKey(getProp($item, $nameKey));
  9535. if ($itemKey === '') {
  9536. continue;
  9537. }
  9538. if (isset($existingMap[$itemKey])) {
  9539. if (trim((string)getProp($item, 'description')) === '') {
  9540. $merged[] = $existingMap[$itemKey];
  9541. continue;
  9542. }
  9543. // 检查内容是否发生变化
  9544. $isChanged = $this->isEpisodeResourceChanged($existingMap[$itemKey], $item, $nameKey);
  9545. if (!$isChanged) {
  9546. // nameKey 和 description 一致,继承现有项的 URL、task_id 和 task_status
  9547. $mergedItem = [
  9548. $nameKey => trim((string)getProp($item, $nameKey)),
  9549. 'description' => trim((string)getProp($item, 'description')),
  9550. ];
  9551. // 如果有 pic_prompt,添加到合并项中
  9552. $picPrompt = getProp($item, 'pic_prompt');
  9553. if (!empty($picPrompt)) {
  9554. $mergedItem['pic_prompt'] = trim((string)$picPrompt);
  9555. }
  9556. // 继承现有项的 URL
  9557. $existingUrl = getProp($existingMap[$itemKey], 'url');
  9558. if (!empty($existingUrl)) {
  9559. $mergedItem['url'] = $existingUrl;
  9560. }
  9561. // 继承现有项的 task_id
  9562. $existingTaskId = getProp($existingMap[$itemKey], 'task_id');
  9563. if (!empty($existingTaskId)) {
  9564. $mergedItem['task_id'] = $existingTaskId;
  9565. }
  9566. // 继承现有项的 task_status
  9567. $existingTaskStatus = getProp($existingMap[$itemKey], 'task_status');
  9568. if (!empty($existingTaskStatus)) {
  9569. $mergedItem['task_status'] = $existingTaskStatus;
  9570. }
  9571. // 保留生成项的音色字段
  9572. $voiceName = getProp($item, 'voice_name');
  9573. if (!empty($voiceName)) {
  9574. $mergedItem['voice_name'] = $voiceName;
  9575. }
  9576. $voiceType = getProp($item, 'voice_type');
  9577. if (!empty($voiceType)) {
  9578. $mergedItem['voice_type'] = $voiceType;
  9579. }
  9580. $audioUrl = getProp($item, 'voice_audio_url');
  9581. if (!empty($audioUrl)) {
  9582. $mergedItem['voice_audio_url'] = $audioUrl;
  9583. }
  9584. $merged[] = $mergedItem;
  9585. } else {
  9586. // nameKey 或 description 不一致,重置 URL、task_id 和 task_status,但保留音色字段
  9587. $mergedItem = [
  9588. $nameKey => trim((string)getProp($item, $nameKey)),
  9589. 'description' => trim((string)getProp($item, 'description')),
  9590. ];
  9591. // 如果有 pic_prompt,添加到合并项中
  9592. $picPrompt = getProp($item, 'pic_prompt');
  9593. if (!empty($picPrompt)) {
  9594. $mergedItem['pic_prompt'] = trim((string)$picPrompt);
  9595. }
  9596. // 保留生成项的音色字段
  9597. $voiceName = getProp($item, 'voice_name');
  9598. if (!empty($voiceName)) {
  9599. $mergedItem['voice_name'] = $voiceName;
  9600. }
  9601. $voiceType = getProp($item, 'voice_type');
  9602. if (!empty($voiceType)) {
  9603. $mergedItem['voice_type'] = $voiceType;
  9604. }
  9605. $audioUrl = getProp($item, 'voice_audio_url');
  9606. if (!empty($audioUrl)) {
  9607. $mergedItem['voice_audio_url'] = $audioUrl;
  9608. }
  9609. // 不继承 URL、task_id 和 task_status(重置状态)
  9610. $merged[] = $mergedItem;
  9611. }
  9612. } else {
  9613. // 新增项,保留生成项的所有字段
  9614. $merged[] = $this->resetEpisodeResourceItem($item, $nameKey, ['url', 'task_id', 'task_status', 'voice_name', 'voice_type', 'voice_audio_url', 'pic_prompt']);
  9615. }
  9616. }
  9617. return $merged ?: array_values($existingItems);
  9618. }
  9619. private function resetEpisodeResourceItem(array $item, string $nameKey, array $preserveFields = []): array
  9620. {
  9621. $resetItem = [
  9622. $nameKey => trim((string)getProp($item, $nameKey)),
  9623. 'description' => trim((string)getProp($item, 'description')),
  9624. ];
  9625. // 保留指定的字段
  9626. foreach ($preserveFields as $field) {
  9627. $value = getProp($item, $field);
  9628. if (!empty($value)) {
  9629. $resetItem[$field] = $value;
  9630. }
  9631. }
  9632. return $resetItem;
  9633. }
  9634. private function isEpisodeResourceChanged(array $existingItem, array $generatedItem, string $nameKey): bool
  9635. {
  9636. $existingKey = $this->normalizeEpisodeResourceKey(getProp($existingItem, $nameKey));
  9637. $generatedKey = $this->normalizeEpisodeResourceKey(getProp($generatedItem, $nameKey));
  9638. if ($existingKey !== $generatedKey) {
  9639. return true;
  9640. }
  9641. // 比较 description 是否变化
  9642. if (trim((string)getProp($existingItem, 'description')) !== trim((string)getProp($generatedItem, 'description'))) {
  9643. return true;
  9644. }
  9645. // 比较 pic_prompt 是否变化
  9646. return trim((string)getProp($existingItem, 'pic_prompt')) !== trim((string)getProp($generatedItem, 'pic_prompt'));
  9647. }
  9648. private function normalizeEpisodeResourceKey($value): string
  9649. {
  9650. $value = trim((string)$value);
  9651. if ($value === '') {
  9652. return '';
  9653. }
  9654. return strtolower((string)preg_replace('/\s+/u', '', $value));
  9655. }
  9656. // 生成全局角色图片
  9657. private function batchSetGlobalRoleImg($data, $is_force=false) {
  9658. $anime_id = getProp($data, 'anime_id');
  9659. if (!$anime_id) Utils::throwError('1002:请选择对话');
  9660. $anime = DB::table('mp_animes')->where('id', $anime_id)->where('is_deleted', 0)->first();
  9661. $roles = json_decode(getProp($anime, 'roles'), true) ?: [];
  9662. $art_style = getProp($anime, 'art_style');
  9663. $update_flag = false;
  9664. foreach ($roles as &$role) {
  9665. $role_name = getProp($role, 'role');
  9666. if ($role_name == '旁白') continue;
  9667. // 优先使用 pic_prompt,如果没有则使用 description
  9668. $pic_prompt = getProp($role, 'pic_prompt');
  9669. $description = !empty($pic_prompt) ? trim((string)$pic_prompt) : getProp($role, 'description');
  9670. // if ($art_style) $description = "美术风格:\n{$art_style}\n主体描述:{$description}\n图片要求:图片侧重点是人物,不要出现场景或背景,并且只能生成一个人物";
  9671. if ($art_style) $description = "主体描述:{$description}\n图片要求:图片侧重点是人物,不要出现场景或背景,并且只能生成一个人物";
  9672. // 参考图地址
  9673. $ref_img_url = getProp($role, 'url');
  9674. if (!$is_force && $ref_img_url) continue;
  9675. $update_flag = true;
  9676. // 此处调用AIImageGenerationService的生成图片任务接口: createImageGenerationTask(注意使用默认参数,prompt使用角色描述),并返回任务id
  9677. try {
  9678. $params = [
  9679. 'prompt' => $description,
  9680. // 'ref_img_urls' => !empty($ref_img_url) ? [$ref_img_url] : []
  9681. 'ref_img_urls' => []
  9682. ];
  9683. $task = $this->aiImageGenerationService->createImageGenerationTask($params);
  9684. $task_id = $task->id;
  9685. if (!$task_id) {
  9686. Utils::throwError("20003: 角色({$role_name})生成图片失败");
  9687. }
  9688. $role['task_id'] = $task_id;
  9689. $role['task_status'] = 'processing';
  9690. } catch (\Exception $e) {
  9691. Utils::throwError("20003:" . $e->getMessage());
  9692. }
  9693. }
  9694. if (!$update_flag) return true;
  9695. // 保存角色信息
  9696. $boolen = DB::table('mp_animes')->where('id', $anime_id)->update([
  9697. 'roles' => json_encode($roles, 256),
  9698. 'generate_status' => '执行中',
  9699. 'generate_at' => date('Y-m-d H:i:s'),
  9700. 'updated_at' => date('Y-m-d H:i:s')
  9701. ]);
  9702. if (!$boolen) {
  9703. Utils::throwError('20003:保存角色信息失败');
  9704. }
  9705. return $boolen;
  9706. }
  9707. // 生成全局场景图片
  9708. private function batchSetGlobalSceneImg($data, $is_force=false) {
  9709. $anime_id = getProp($data, 'anime_id');
  9710. if (!$anime_id) Utils::throwError('1002:请选择对话');
  9711. $anime = DB::table('mp_animes')->where('id', $anime_id)->where('is_deleted', 0)->first();
  9712. $scenes = json_decode(getProp($anime, 'scenes'), true) ?: [];
  9713. $art_style = getProp($anime, 'art_style');
  9714. $update_flag = false;
  9715. foreach ($scenes as &$scene) {
  9716. $scene_name = getProp($scene, 'scene');
  9717. // 优先使用 pic_prompt,如果没有则使用 description
  9718. $pic_prompt = getProp($scene, 'pic_prompt');
  9719. $description = !empty($pic_prompt) ? trim((string)$pic_prompt) : getProp($scene, 'description');
  9720. if ($art_style) $description = "场景描述:{$description}\n图片要求: 图片侧重点是场景,生成的是场景环境图片,不要出现人物(此为第一优先要求)";
  9721. // 参考图地址
  9722. $ref_img_url = getProp($scene, 'url');
  9723. if (!$is_force && $ref_img_url) continue;
  9724. $update_flag = true;
  9725. // 此处调用AIImageGenerationService的生成图片任务接口: createImageGenerationTask(注意使用默认参数,prompt使用角色描述),并返回任务id
  9726. try {
  9727. $params = [
  9728. 'prompt' => $description,
  9729. // 'ref_img_urls' => !empty($ref_img_url) ? [$ref_img_url] : [],
  9730. 'ref_img_urls' => []
  9731. ];
  9732. $task = $this->aiImageGenerationService->createImageGenerationTask($params);
  9733. $task_id = $task->id;
  9734. if (!$task_id) {
  9735. Utils::throwError("20003: 场景({$scene_name})生成图片失败");
  9736. }
  9737. $scene['task_id'] = $task_id;
  9738. $scene['task_status'] = 'processing';
  9739. } catch (\Exception $e) {
  9740. Utils::throwError("20003:" . $e->getMessage());
  9741. }
  9742. }
  9743. if (!$update_flag) return true;
  9744. // 保存角色信息
  9745. $boolen = DB::table('mp_animes')->where('id', $anime_id)->update([
  9746. 'scenes' => json_encode($scenes, 256),
  9747. 'generate_status' => '执行中',
  9748. 'generate_at' => date('Y-m-d H:i:s'),
  9749. 'updated_at' => date('Y-m-d H:i:s')
  9750. ]);
  9751. if (!$boolen) {
  9752. Utils::throwError('20003:保存角色信息失败');
  9753. }
  9754. return $boolen;
  9755. }
  9756. // 生成分镜主体、场景和道具图片
  9757. private function batchSetEpisodeRolesAndScenesImg($anime_id, $episode_number, $roles, $scenes, $props = [], $ref_products=[], $art_style_type='', $is_force=false) {
  9758. $character_prefix = '';
  9759. $scene_prefix = '';
  9760. $prop_prefix = '';
  9761. if ($art_style_type) {
  9762. $art_style_arr = $this->getArtStyleShortPromptByInput($art_style_type);
  9763. $character_prefix = isset($art_style_arr['character_prefix']) ? $art_style_arr['character_prefix'] : '';
  9764. $scene_prefix = isset($art_style_arr['scene_prefix']) ? $art_style_arr['scene_prefix'] : '';
  9765. $prop_prefix = isset($art_style_arr['scene_prefix']) ? $art_style_arr['scene_prefix'] : ''; // 道具使用场景的前缀
  9766. }
  9767. $update_flag = false;
  9768. // 获取上一集的roles、scenes和props数据
  9769. $prev_roles = [];
  9770. $prev_scenes = [];
  9771. $prev_props = [];
  9772. if ($episode_number > 1) {
  9773. $prev_episode = DB::table('mp_anime_episodes')
  9774. ->where('anime_id', $anime_id)
  9775. ->where('episode_number', $episode_number - 1)
  9776. ->where('is_default', 1)
  9777. ->first();
  9778. if ($prev_episode) {
  9779. $prev_roles = json_decode(getProp($prev_episode, 'roles'), true) ?: [];
  9780. $prev_scenes = json_decode(getProp($prev_episode, 'scenes'), true) ?: [];
  9781. $prev_props = json_decode(getProp($prev_episode, 'props'), true) ?: [];
  9782. dLog('deepseek')->info("获取上一集数据成功", [
  9783. 'anime_id' => $anime_id,
  9784. 'prev_episode_number' => $episode_number - 1,
  9785. 'prev_roles_count' => count($prev_roles),
  9786. 'prev_scenes_count' => count($prev_scenes),
  9787. 'prev_props_count' => count($prev_props)
  9788. ]);
  9789. }
  9790. }
  9791. // 处理角色图片生成
  9792. foreach ($roles as &$role) {
  9793. $role_name = getProp($role, 'role');
  9794. if ($role_name == '旁白') continue;
  9795. // 如果参考资产里有该角色,则直接使用该图片,不继续生成
  9796. if (isset($ref_products[$role_name])) {
  9797. $role['url'] = isset($ref_products[$role_name]['url']) ? $ref_products[$role_name]['url'] : '';
  9798. if ($role['url']) continue;
  9799. }
  9800. $role_description = getProp($role, 'description');
  9801. $role_pic_prompt = getProp($role, 'pic_prompt');
  9802. $role_url = getProp($role, 'url');
  9803. // 检查是否可以从上一集继承
  9804. $inherited = false;
  9805. if (!$is_force && !empty($prev_roles)) {
  9806. foreach ($prev_roles as $prev_role) {
  9807. $prev_role_name = getProp($prev_role, 'role');
  9808. $prev_description = getProp($prev_role, 'description');
  9809. $prev_pic_prompt = getProp($prev_role, 'pic_prompt');
  9810. $prev_url = getProp($prev_role, 'url');
  9811. $prev_task_id = getProp($prev_role, 'task_id');
  9812. $prev_task_status = getProp($prev_role, 'task_status');
  9813. // 如果role、description、pic_prompt都一致,且上一集已有url,则继承
  9814. if ($role_name == $prev_role_name
  9815. && $role_description == $prev_description
  9816. && $role_pic_prompt == $prev_pic_prompt
  9817. && !empty($prev_url)) {
  9818. $role['task_id'] = $prev_task_id;
  9819. $role['task_status'] = $prev_task_status;
  9820. $role['url'] = $prev_url;
  9821. $inherited = true;
  9822. dLog('deepseek')->info("角色({$role_name})从上一集继承图片数据", [
  9823. 'anime_id' => $anime_id,
  9824. 'episode_number' => $episode_number,
  9825. 'task_id' => $prev_task_id,
  9826. 'url' => $prev_url
  9827. ]);
  9828. break;
  9829. }
  9830. }
  9831. }
  9832. // 如果已继承或已有url,跳过生成
  9833. if ($inherited || (!$is_force && $role_url)) {
  9834. continue;
  9835. }
  9836. // 优先使用 pic_prompt,如果没有则使用 description
  9837. $pic_prompt = getProp($role, 'pic_prompt');
  9838. $description = !empty($pic_prompt) ? trim((string)$pic_prompt) : getProp($role, 'description');
  9839. $description = "{$character_prefix}。\n主体描述:{$description}\n超高清,高细节,主体突出,构图合理,画面干净,无场景或背景,单人物,无文字,无水印,无字幕,电影级光影,视觉焦点明确";
  9840. $update_flag = true;
  9841. // 调用AIImageGenerationService的生成图片任务接口
  9842. try {
  9843. $params = [
  9844. 'prompt' => $description,
  9845. 'ref_img_urls' => []
  9846. ];
  9847. $task = $this->aiImageGenerationService->createImageGenerationTask($params);
  9848. $task_id = $task->id;
  9849. if (!$task_id) {
  9850. dLog('deepseek')->error("角色({$role_name})生成图片失败");
  9851. continue; // 不中断,继续处理其他角色
  9852. }
  9853. $role['task_id'] = $task_id;
  9854. $role['task_status'] = 'processing';
  9855. dLog('deepseek')->info("角色({$role_name})创建图片生成任务成功", [
  9856. 'anime_id' => $anime_id,
  9857. 'episode_number' => $episode_number,
  9858. 'task_id' => $task_id
  9859. ]);
  9860. } catch (\Exception $e) {
  9861. dLog('deepseek')->error("角色({$role_name})创建图片生成任务失败: " . $e->getMessage());
  9862. // 不抛出异常,继续处理其他角色
  9863. }
  9864. }
  9865. // 处理场景图片生成
  9866. foreach ($scenes as &$scene) {
  9867. $scene_name = getProp($scene, 'scene');
  9868. $scene_description = getProp($scene, 'description');
  9869. $scene_pic_prompt = getProp($scene, 'pic_prompt');
  9870. $scene_url = getProp($scene, 'url');
  9871. // 如果参考资产里有该场景,则直接使用该图片,不继续生成
  9872. if (isset($ref_products[$scene_name])) {
  9873. $scene['url'] = isset($ref_products[$scene_name]['url']) ? $ref_products[$scene_name]['url'] : '';
  9874. if ($scene['url']) continue;
  9875. }
  9876. // 检查是否可以从上一集继承
  9877. $inherited = false;
  9878. if (!$is_force && !empty($prev_scenes)) {
  9879. foreach ($prev_scenes as $prev_scene) {
  9880. $prev_scene_name = getProp($prev_scene, 'scene');
  9881. $prev_description = getProp($prev_scene, 'description');
  9882. $prev_pic_prompt = getProp($prev_scene, 'pic_prompt');
  9883. $prev_url = getProp($prev_scene, 'url');
  9884. $prev_task_id = getProp($prev_scene, 'task_id');
  9885. $prev_task_status = getProp($prev_scene, 'task_status');
  9886. // 如果scene、description、pic_prompt都一致,且上一集已有url,则继承
  9887. if ($scene_name == $prev_scene_name
  9888. && $scene_description == $prev_description
  9889. && $scene_pic_prompt == $prev_pic_prompt
  9890. && !empty($prev_url)) {
  9891. $scene['task_id'] = $prev_task_id;
  9892. $scene['task_status'] = $prev_task_status;
  9893. $scene['url'] = $prev_url;
  9894. $inherited = true;
  9895. dLog('deepseek')->info("场景({$scene_name})从上一集继承图片数据", [
  9896. 'anime_id' => $anime_id,
  9897. 'episode_number' => $episode_number,
  9898. 'task_id' => $prev_task_id,
  9899. 'url' => $prev_url
  9900. ]);
  9901. break;
  9902. }
  9903. }
  9904. }
  9905. // 如果已继承或已有url,跳过生成
  9906. if ($inherited || (!$is_force && $scene_url)) {
  9907. continue;
  9908. }
  9909. // 优先使用 pic_prompt,如果没有则使用 description
  9910. $pic_prompt = getProp($scene, 'pic_prompt');
  9911. $description = !empty($pic_prompt) ? trim((string)$pic_prompt) : getProp($scene, 'description');
  9912. $description = "{$scene_prefix}。\n场景描述:{$description}\n无人物,超高清,高细节,主体突出,构图合理,画面干净,无文字,无水印,无字幕,电影级光影,视觉焦点明确";
  9913. $update_flag = true;
  9914. // 调用AIImageGenerationService的生成图片任务接口
  9915. try {
  9916. $params = [
  9917. 'prompt' => $description,
  9918. 'ref_img_urls' => []
  9919. ];
  9920. $task = $this->aiImageGenerationService->createImageGenerationTask($params);
  9921. $task_id = $task->id;
  9922. if (!$task_id) {
  9923. dLog('deepseek')->error("场景({$scene_name})生成图片失败");
  9924. continue; // 不中断,继续处理其他场景
  9925. }
  9926. $scene['task_id'] = $task_id;
  9927. $scene['task_status'] = 'processing';
  9928. dLog('deepseek')->info("场景({$scene_name})创建图片生成任务成功", [
  9929. 'anime_id' => $anime_id,
  9930. 'episode_number' => $episode_number,
  9931. 'task_id' => $task_id
  9932. ]);
  9933. } catch (\Exception $e) {
  9934. dLog('deepseek')->error("场景({$scene_name})创建图片生成任务失败: " . $e->getMessage());
  9935. // 不抛出异常,继续处理其他场景
  9936. }
  9937. }
  9938. // 处理道具图片生成(逻辑与场景一致)
  9939. foreach ($props as &$prop) {
  9940. $prop_name = getProp($prop, 'prop');
  9941. $prop_description = getProp($prop, 'description');
  9942. $prop_pic_prompt = getProp($prop, 'pic_prompt');
  9943. $prop_url = getProp($prop, 'url');
  9944. // 如果参考资产里有该道具,则直接使用该图片,不继续生成
  9945. if (isset($ref_products[$prop_name])) {
  9946. $prop['url'] = isset($ref_products[$prop_name]['url']) ? $ref_products[$prop_name]['url'] : '';
  9947. if ($prop['url']) continue;
  9948. }
  9949. // 检查是否可以从上一集继承
  9950. $inherited = false;
  9951. if (!$is_force && !empty($prev_props)) {
  9952. foreach ($prev_props as $prev_prop) {
  9953. $prev_prop_name = getProp($prev_prop, 'prop');
  9954. $prev_description = getProp($prev_prop, 'description');
  9955. $prev_pic_prompt = getProp($prev_prop, 'pic_prompt');
  9956. $prev_url = getProp($prev_prop, 'url');
  9957. $prev_task_id = getProp($prev_prop, 'task_id');
  9958. $prev_task_status = getProp($prev_prop, 'task_status');
  9959. // 如果prop、description、pic_prompt都一致,且上一集已有url,则继承
  9960. if ($prop_name == $prev_prop_name
  9961. && $prop_description == $prev_description
  9962. && $prop_pic_prompt == $prev_pic_prompt
  9963. && !empty($prev_url)) {
  9964. $prop['task_id'] = $prev_task_id;
  9965. $prop['task_status'] = $prev_task_status;
  9966. $prop['url'] = $prev_url;
  9967. $inherited = true;
  9968. dLog('deepseek')->info("道具({$prop_name})从上一集继承图片数据", [
  9969. 'anime_id' => $anime_id,
  9970. 'episode_number' => $episode_number,
  9971. 'task_id' => $prev_task_id,
  9972. 'url' => $prev_url
  9973. ]);
  9974. break;
  9975. }
  9976. }
  9977. }
  9978. // 如果已继承或已有url,跳过生成
  9979. if ($inherited || (!$is_force && $prop_url)) {
  9980. continue;
  9981. }
  9982. // 优先使用 pic_prompt,如果没有则使用 description
  9983. $pic_prompt = getProp($prop, 'pic_prompt');
  9984. $description = !empty($pic_prompt) ? trim((string)$pic_prompt) : getProp($prop, 'description');
  9985. $description = "{$prop_prefix}。\n道具描述:{$description}\n无人物,超高清,高细节,主体突出,构图合理,画面干净,无文字,无水印,无字幕,电影级光影,视觉焦点明确";
  9986. $update_flag = true;
  9987. // 调用AIImageGenerationService的生成图片任务接口
  9988. try {
  9989. $params = [
  9990. 'prompt' => $description,
  9991. 'ref_img_urls' => []
  9992. ];
  9993. $task = $this->aiImageGenerationService->createImageGenerationTask($params);
  9994. $task_id = $task->id;
  9995. if (!$task_id) {
  9996. dLog('deepseek')->error("道具({$prop_name})生成图片失败");
  9997. continue; // 不中断,继续处理其他道具
  9998. }
  9999. $prop['task_id'] = $task_id;
  10000. $prop['task_status'] = 'processing';
  10001. dLog('deepseek')->info("道具({$prop_name})创建图片生成任务成功", [
  10002. 'anime_id' => $anime_id,
  10003. 'episode_number' => $episode_number,
  10004. 'task_id' => $task_id
  10005. ]);
  10006. } catch (\Exception $e) {
  10007. dLog('deepseek')->error("道具({$prop_name})创建图片生成任务失败: " . $e->getMessage());
  10008. // 不抛出异常,继续处理其他道具
  10009. }
  10010. }
  10011. if (!$update_flag) return ['roles' => $roles, 'scenes' => $scenes, 'props' => $props];
  10012. // 将处理完毕的roles、scenes和props与全局(mp_animes)数据对比并合并
  10013. $this->syncRolesAndScenesToGlobal($anime_id, $roles, $scenes, $props);
  10014. // // 保存剧集的角色和场景信息
  10015. // $boolen = DB::table('mp_anime_episodes')->where('id', $episode_id)->update([
  10016. // 'roles' => json_encode($roles, 256),
  10017. // 'scenes' => json_encode($scenes, 256),
  10018. // 'generate_status' => '执行中',
  10019. // 'generate_at' => date('Y-m-d H:i:s'),
  10020. // 'updated_at' => date('Y-m-d H:i:s')
  10021. // ]);
  10022. // if (!$boolen) {
  10023. // dLog('deepseek')->error('保存剧集角色和场景信息失败', ['episode_id' => $episode_id]);
  10024. // Utils::throwError('20003:保存剧集角色和场景信息失败');
  10025. // }
  10026. dLog('deepseek')->info('剧集角色、场景和道具图片任务创建完成', [
  10027. // 'episode_id' => $episode_id,
  10028. 'roles_count' => count($roles),
  10029. 'scenes_count' => count($scenes),
  10030. 'props_count' => count($props)
  10031. ]);
  10032. return ['roles' => $roles, 'scenes' => $scenes, 'props' => $props];
  10033. }
  10034. /**
  10035. * 将剧集的roles、scenes和props同步到全局(mp_animes)
  10036. * 如果全局中不存在相同的role/scene/prop(名称、description、pic_prompt三项一致),则添加到全局
  10037. *
  10038. * @param int $anime_id 动漫对话ID
  10039. * @param array $episode_roles 剧集角色数据
  10040. * @param array $episode_scenes 剧集场景数据
  10041. * @param array $episode_props 剧集道具数据
  10042. * @return bool
  10043. */
  10044. private function syncRolesAndScenesToGlobal($anime_id, $episode_roles, $episode_scenes, $episode_props = []) {
  10045. // 获取全局数据
  10046. $anime = DB::table('mp_animes')->where('id', $anime_id)->where('is_deleted', 0)->first();
  10047. if (!$anime) {
  10048. dLog('deepseek')->warning('动漫对话不存在', ['anime_id' => $anime_id]);
  10049. return false;
  10050. }
  10051. $global_roles = json_decode(getProp($anime, 'roles'), true) ?: [];
  10052. $global_scenes = json_decode(getProp($anime, 'scenes'), true) ?: [];
  10053. $global_props = json_decode(getProp($anime, 'props'), true) ?: [];
  10054. $roles_added = false;
  10055. $roles_updated = false;
  10056. $scenes_added = false;
  10057. $props_added = false;
  10058. // 处理角色同步
  10059. foreach ($episode_roles as $episode_role) {
  10060. $episode_role_name = getProp($episode_role, 'role');
  10061. $episode_description = getProp($episode_role, 'description');
  10062. $episode_pic_prompt = getProp($episode_role, 'pic_prompt');
  10063. $episode_voice_prompt = getProp($episode_role, 'voice_prompt');
  10064. // 跳过旁白
  10065. if ($episode_role_name == '旁白') {
  10066. continue;
  10067. }
  10068. // 检查全局中是否已存在相同的角色
  10069. $exists = false;
  10070. $global_role_index = -1;
  10071. foreach ($global_roles as $index => $global_role) {
  10072. $global_role_name = getProp($global_role, 'role');
  10073. $global_description = getProp($global_role, 'description');
  10074. $global_pic_prompt = getProp($global_role, 'pic_prompt');
  10075. // 如果role、description、pic_prompt三项都一致,则认为已存在
  10076. if ($episode_role_name == $global_role_name
  10077. && $episode_description == $global_description
  10078. && $episode_pic_prompt == $global_pic_prompt) {
  10079. $exists = true;
  10080. $global_role_index = $index;
  10081. break;
  10082. }
  10083. }
  10084. // 如果不存在,则添加到全局
  10085. if (!$exists) {
  10086. $global_roles[] = $episode_role;
  10087. $roles_added = true;
  10088. dLog('deepseek')->info("角色({$episode_role_name})添加到全局", [
  10089. 'anime_id' => $anime_id,
  10090. 'role' => $episode_role_name
  10091. ]);
  10092. } else {
  10093. // 如果存在,且剧集有voice_prompt字段,将voice_prompt更新到全局
  10094. if (!empty($episode_voice_prompt)) {
  10095. $global_voice_prompt = getProp($global_roles[$global_role_index], 'voice_prompt');
  10096. // 如果全局没有voice_prompt或与剧集不一致,则更新
  10097. if ($global_voice_prompt !== $episode_voice_prompt) {
  10098. $global_roles[$global_role_index]['voice_prompt'] = $episode_voice_prompt;
  10099. $roles_updated = true;
  10100. dLog('deepseek')->info("角色({$episode_role_name})的voice_prompt更新到全局", [
  10101. 'anime_id' => $anime_id,
  10102. 'role' => $episode_role_name,
  10103. 'voice_prompt' => $episode_voice_prompt
  10104. ]);
  10105. }
  10106. }
  10107. }
  10108. }
  10109. // 处理场景同步
  10110. foreach ($episode_scenes as $episode_scene) {
  10111. $episode_scene_name = getProp($episode_scene, 'scene');
  10112. $episode_description = getProp($episode_scene, 'description');
  10113. $episode_pic_prompt = getProp($episode_scene, 'pic_prompt');
  10114. // 检查全局中是否已存在相同的场景
  10115. $exists = false;
  10116. foreach ($global_scenes as $global_scene) {
  10117. $global_scene_name = getProp($global_scene, 'scene');
  10118. $global_description = getProp($global_scene, 'description');
  10119. $global_pic_prompt = getProp($global_scene, 'pic_prompt');
  10120. // 如果scene、description、pic_prompt三项都一致,则认为已存在
  10121. if ($episode_scene_name == $global_scene_name
  10122. && $episode_description == $global_description
  10123. && $episode_pic_prompt == $global_pic_prompt) {
  10124. $exists = true;
  10125. break;
  10126. }
  10127. }
  10128. // 如果不存在,则添加到全局
  10129. if (!$exists) {
  10130. $global_scenes[] = $episode_scene;
  10131. $scenes_added = true;
  10132. dLog('deepseek')->info("场景({$episode_scene_name})添加到全局", [
  10133. 'anime_id' => $anime_id,
  10134. 'scene' => $episode_scene_name
  10135. ]);
  10136. }
  10137. }
  10138. // 处理道具同步(逻辑与场景一致)
  10139. foreach ($episode_props as $episode_prop) {
  10140. $episode_prop_name = getProp($episode_prop, 'prop');
  10141. $episode_description = getProp($episode_prop, 'description');
  10142. $episode_pic_prompt = getProp($episode_prop, 'pic_prompt');
  10143. // 检查全局中是否已存在相同的道具
  10144. $exists = false;
  10145. foreach ($global_props as $global_prop) {
  10146. $global_prop_name = getProp($global_prop, 'prop');
  10147. $global_description = getProp($global_prop, 'description');
  10148. $global_pic_prompt = getProp($global_prop, 'pic_prompt');
  10149. // 如果prop、description、pic_prompt三项都一致,则认为已存在
  10150. if ($episode_prop_name == $global_prop_name
  10151. && $episode_description == $global_description
  10152. && $episode_pic_prompt == $global_pic_prompt) {
  10153. $exists = true;
  10154. break;
  10155. }
  10156. }
  10157. // 如果不存在,则添加到全局
  10158. if (!$exists) {
  10159. $global_props[] = $episode_prop;
  10160. $props_added = true;
  10161. dLog('deepseek')->info("道具({$episode_prop_name})添加到全局", [
  10162. 'anime_id' => $anime_id,
  10163. 'prop' => $episode_prop_name
  10164. ]);
  10165. }
  10166. }
  10167. // 如果有新增或更新,则更新全局数据
  10168. if ($roles_added || $roles_updated || $scenes_added || $props_added) {
  10169. $update_data = ['updated_at' => date('Y-m-d H:i:s')];
  10170. if ($roles_added || $roles_updated) {
  10171. $update_data['roles'] = json_encode($global_roles, 256);
  10172. }
  10173. if ($scenes_added) {
  10174. $update_data['scenes'] = json_encode($global_scenes, 256);
  10175. }
  10176. if ($props_added) {
  10177. $update_data['props'] = json_encode($global_props, 256);
  10178. }
  10179. DB::table('mp_animes')->where('id', $anime_id)->update($update_data);
  10180. dLog('deepseek')->info('剧集数据同步到全局完成', [
  10181. 'anime_id' => $anime_id,
  10182. 'roles_added' => $roles_added,
  10183. 'roles_updated' => $roles_updated,
  10184. 'scenes_added' => $scenes_added,
  10185. 'props_added' => $props_added,
  10186. 'global_roles_count' => count($global_roles),
  10187. 'global_scenes_count' => count($global_scenes),
  10188. 'global_props_count' => count($global_props)
  10189. ]);
  10190. }
  10191. return true;
  10192. }
  10193. /**
  10194. * 将全局角色和场景的task_id、task_status和url继承给指定分集
  10195. *
  10196. * @param int $anime_id 动漫对话ID
  10197. * @param int $episode_id 分集ID
  10198. * @return bool
  10199. */
  10200. private function inheritGlobalImagesToEpisodes($anime_id, $episode_id) {
  10201. // 获取全局角色和场景数据
  10202. $anime = DB::table('mp_animes')->where('id', $anime_id)->where('is_deleted', 0)->first();
  10203. if (!$anime) {
  10204. dLog('deepseek')->warning('动漫对话不存在', ['anime_id' => $anime_id]);
  10205. return false;
  10206. }
  10207. $global_roles = json_decode(getProp($anime, 'roles'), true) ?: [];
  10208. $global_scenes = json_decode(getProp($anime, 'scenes'), true) ?: [];
  10209. // 获取指定的分集
  10210. $episode = DB::table('mp_anime_episodes')->where('id', $episode_id)->where('anime_id', $anime_id)->first();
  10211. if (!$episode) {
  10212. dLog('deepseek')->warning('分集不存在', ['anime_id' => $anime_id, 'episode_id' => $episode_id]);
  10213. return false;
  10214. }
  10215. $episode_roles = json_decode(getProp($episode, 'roles'), true) ?: [];
  10216. $episode_scenes = json_decode(getProp($episode, 'scenes'), true) ?: [];
  10217. $roles_updated = false;
  10218. $scenes_updated = false;
  10219. // 继承角色的task_id、task_status和url
  10220. foreach ($episode_roles as &$episode_role) {
  10221. $episode_role_name = getProp($episode_role, 'role');
  10222. $episode_description = getProp($episode_role, 'description');
  10223. $episode_pic_prompt = getProp($episode_role, 'pic_prompt');
  10224. $episode_url = getProp($episode_role, 'url');
  10225. // 跳过旁白角色
  10226. if ($episode_role_name == '旁白') {
  10227. continue;
  10228. }
  10229. // 如果剧集中已有URL,跳过
  10230. if (!empty($episode_url)) {
  10231. continue;
  10232. }
  10233. // 遍历全局角色数据,查找匹配的角色
  10234. foreach ($global_roles as $global_role) {
  10235. $global_role_name = getProp($global_role, 'role');
  10236. $global_description = getProp($global_role, 'description');
  10237. $global_pic_prompt = getProp($global_role, 'pic_prompt');
  10238. $global_url = getProp($global_role, 'url');
  10239. $global_task_id = getProp($global_role, 'task_id');
  10240. $global_task_status = getProp($global_role, 'task_status');
  10241. // 满足条件:1.角色名相同 2.描述相同 3.pic_prompt相同 4.剧集中URL为空
  10242. if ($episode_role_name == $global_role_name
  10243. && $episode_description == $global_description
  10244. && $episode_pic_prompt == $global_pic_prompt) {
  10245. // 继承 task_id、task_status 和 url
  10246. if (!empty($global_task_id)) {
  10247. $episode_role['task_id'] = $global_task_id;
  10248. $episode_role['task_status'] = $global_task_status;
  10249. $roles_updated = true;
  10250. dLog('deepseek')->info('剧集角色继承全局task_id', [
  10251. 'episode_id' => $episode_id,
  10252. 'role' => $episode_role_name,
  10253. 'task_id' => $global_task_id,
  10254. 'task_status' => $global_task_status
  10255. ]);
  10256. }
  10257. if (!empty($global_url)) {
  10258. $episode_role['url'] = $global_url;
  10259. $roles_updated = true;
  10260. dLog('deepseek')->info('剧集角色继承全局url', [
  10261. 'episode_id' => $episode_id,
  10262. 'role' => $episode_role_name,
  10263. 'url' => $global_url
  10264. ]);
  10265. }
  10266. break;
  10267. }
  10268. }
  10269. }
  10270. // 继承场景的task_id、task_status和url
  10271. foreach ($episode_scenes as &$episode_scene) {
  10272. $episode_scene_name = getProp($episode_scene, 'scene');
  10273. $episode_description = getProp($episode_scene, 'description');
  10274. $episode_pic_prompt = getProp($episode_scene, 'pic_prompt');
  10275. $episode_url = getProp($episode_scene, 'url');
  10276. // 如果剧集中已有URL,跳过
  10277. if (!empty($episode_url)) {
  10278. continue;
  10279. }
  10280. // 遍历全局场景数据,查找匹配的场景
  10281. foreach ($global_scenes as $global_scene) {
  10282. $global_scene_name = getProp($global_scene, 'scene');
  10283. $global_description = getProp($global_scene, 'description');
  10284. $global_pic_prompt = getProp($global_scene, 'pic_prompt');
  10285. $global_url = getProp($global_scene, 'url');
  10286. $global_task_id = getProp($global_scene, 'task_id');
  10287. $global_task_status = getProp($global_scene, 'task_status');
  10288. // 满足条件:1.场景名相同 2.描述相同 3.pic_prompt相同 4.剧集中URL为空
  10289. if ($episode_scene_name == $global_scene_name
  10290. && $episode_description == $global_description
  10291. && $episode_pic_prompt == $global_pic_prompt) {
  10292. // 继承 task_id、task_status 和 url
  10293. if (!empty($global_task_id)) {
  10294. $episode_scene['task_id'] = $global_task_id;
  10295. $episode_scene['task_status'] = $global_task_status;
  10296. $scenes_updated = true;
  10297. dLog('deepseek')->info('剧集场景继承全局task_id', [
  10298. 'episode_id' => $episode_id,
  10299. 'scene' => $episode_scene_name,
  10300. 'task_id' => $global_task_id,
  10301. 'task_status' => $global_task_status
  10302. ]);
  10303. }
  10304. if (!empty($global_url)) {
  10305. $episode_scene['url'] = $global_url;
  10306. $scenes_updated = true;
  10307. dLog('deepseek')->info('剧集场景继承全局url', [
  10308. 'episode_id' => $episode_id,
  10309. 'scene' => $episode_scene_name,
  10310. 'url' => $global_url
  10311. ]);
  10312. }
  10313. break;
  10314. }
  10315. }
  10316. }
  10317. // 如果有更新,则保存到数据库
  10318. if ($roles_updated || $scenes_updated) {
  10319. $update_data = ['updated_at' => date('Y-m-d H:i:s')];
  10320. if ($roles_updated) {
  10321. $update_data['roles'] = json_encode($episode_roles, 256);
  10322. }
  10323. if ($scenes_updated) {
  10324. $update_data['scenes'] = json_encode($episode_scenes, 256);
  10325. }
  10326. DB::table('mp_anime_episodes')->where('id', $episode_id)->update($update_data);
  10327. dLog('deepseek')->info('剧集继承全局图片数据完成', [
  10328. 'episode_id' => $episode_id,
  10329. 'roles_updated' => $roles_updated,
  10330. 'scenes_updated' => $scenes_updated
  10331. ]);
  10332. }
  10333. return true;
  10334. }
  10335. public function chatChangeImg($data) {
  10336. $segment = getProp($data, 'segment');
  10337. $segment_content = getProp($segment, 'segment_content');
  10338. $prompt = getProp($data, 'prompt');
  10339. $ref_img = getProp($data, 'ref_img');
  10340. $question = "请修改已有的内容{$segment_content}\n修改要求如下:\n{$prompt}";
  10341. // 处理文本模型
  10342. $model = getProp($data, 'model');
  10343. if (!$model) {
  10344. // 用户没有输入,从anime表获取
  10345. $segment_id = getProp($segment, 'segment_id');
  10346. if ($segment_id) {
  10347. $episode_id = DB::table('mp_episode_segments')->where('segment_id', $segment_id)->value('episode_id');
  10348. if ($episode_id) {
  10349. $anime_id = DB::table('mp_anime_episodes')->where('id', $episode_id)->value('anime_id');
  10350. if ($anime_id) {
  10351. $model = DB::table('mp_animes')->where('id', $anime_id)->value('model');
  10352. }
  10353. }
  10354. }
  10355. if (!$model) {
  10356. // anime表也没有,使用默认值
  10357. $model = 'doubao-seed-2-0-mini-260215';
  10358. }
  10359. }
  10360. // 验证模型是否在可用模型表中
  10361. if (!DB::table('mp_text_models')->where('model', $model)->where('is_enabled', 1)->exists()) {
  10362. $model = 'doubao-seed-2-0-mini-260215';
  10363. }
  10364. $messages[] =
  10365. [
  10366. 'role' => 'user',
  10367. 'content' => $question
  10368. ];
  10369. $post_data = [
  10370. 'model' => $model,
  10371. 'messages' => $messages,
  10372. // 'max_tokens' => 8192,
  10373. 'temperature' => 0.7,
  10374. 'frequency_penalty' => 0,
  10375. 'presence_penalty' => 0,
  10376. 'response_format' => ['type' => 'text'],
  10377. 'stream' => false // 是否启用流式输出
  10378. ];
  10379. if (in_array($model, ['deepseek-reasoner', 'deepseek-chat', 'deepseek-v4-flash', 'deepseek-v4-pro'])) {
  10380. // DeepSeek 官方模型使用 DeepSeek API
  10381. $chatResult = $this->chatOnly($post_data);
  10382. } else if (in_array($model, $this->gpt_text_models)) {
  10383. // GPT-5.4 模型使用 TokenRouter API
  10384. $chatResult = $this->gpt54ChatOnly($post_data);
  10385. } else {
  10386. // 其他模型使用火山引擎API
  10387. $chatResult = $this->volcEngineChatCompletion($post_data);
  10388. }
  10389. if (is_array($chatResult)) {
  10390. extract($chatResult);
  10391. }else {
  10392. Utils::throwError('20003: 接口调用失败!');
  10393. }
  10394. return [
  10395. 'type' => 'done',
  10396. // 'episode' => $episode,
  10397. 'answer' => $fullContent,
  10398. 'reasoning' => $fullReasoningContent,
  10399. 'usage' => $usage
  10400. ];
  10401. }
  10402. // 仅调用deepseek对话接口
  10403. private function chatOnly($post_data) {
  10404. $post_data['max_tokens'] = 300000;
  10405. $client = new Client(['timeout' => 1800, 'verify' => false]);
  10406. $result = $client->post($this->url, ['json' => $post_data, 'headers' => $this->headers]);
  10407. $response = $result->getBody()->getContents();
  10408. $response_arr = json_decode($response, true);
  10409. dLog('deepseek')->info('deepseek请求完成', ['response' => $response_arr]);
  10410. $fullContent = '';
  10411. $fullReasoningContent = [];
  10412. $usage = isset($response_arr['usage']) ? $response_arr['usage'] : [];
  10413. if (isset($response_arr['choices']) && count($response_arr['choices']) > 0) {
  10414. $fullContent = isset($response_arr['choices'][0]['message']['content']) ? $response_arr['choices'][0]['message']['content'] : '';
  10415. $fullReasoningContent = isset($response_arr['choices'][0]['message']['reasoning_content']) ? $response_arr['choices'][0]['message']['reasoning_content'] : '';
  10416. }
  10417. return [
  10418. 'fullContent' => $fullContent,
  10419. 'fullReasoningContent' => $fullReasoningContent,
  10420. 'usage' => $usage
  10421. ];
  10422. }
  10423. // 仅调用 GPT-5.4 对话接口(非流式)
  10424. private function gpt54ChatOnly($post_data) {
  10425. $apiKey = env('GPT_54_API_KEY');
  10426. if (empty($apiKey)) {
  10427. Utils::throwError('20003:GPT-5.4 API Key未配置');
  10428. }
  10429. $client = new Client(['timeout' => 1800, 'verify' => false]);
  10430. $headers = [
  10431. 'Authorization' => 'Bearer ' . $apiKey,
  10432. 'Content-Type' => 'application/json'
  10433. ];
  10434. // 移除 thinking 参数,GPT-5.4 不支持
  10435. if (isset($post_data['thinking'])) {
  10436. unset($post_data['thinking']);
  10437. }
  10438. if (isset($post_data['reasoning_effort'])) {
  10439. unset($post_data['reasoning_effort']);
  10440. }
  10441. $post_data['max_completion_tokens'] = 100000;
  10442. // 确保 stream 为 false
  10443. $post_data['stream'] = false;
  10444. // 备用中转站地址: https://token.ithinkai.cn/v1/chat/completions
  10445. // 备用中转站地址2: https://api.nonelinear.com/v1/chat/completions
  10446. $result = $client->post('https://ai-api.kkidc.com/v1/chat/completions', [
  10447. 'json' => $post_data,
  10448. 'headers' => $headers
  10449. ]);
  10450. $response = $result->getBody()->getContents();
  10451. $response_arr = json_decode($response, true);
  10452. dLog('deepseek')->info('GPT-5.4请求完成', ['response' => $response_arr]);
  10453. $fullContent = '';
  10454. $usage = isset($response_arr['usage']) ? $response_arr['usage'] : [];
  10455. if (isset($response_arr['choices']) && count($response_arr['choices']) > 0) {
  10456. $fullContent = isset($response_arr['choices'][0]['message']['content']) ? $response_arr['choices'][0]['message']['content'] : '';
  10457. }
  10458. return [
  10459. 'fullContent' => $fullContent,
  10460. 'fullReasoningContent' => '', // GPT-5.4 不支持思考链
  10461. 'usage' => $usage
  10462. ];
  10463. }
  10464. private function splitContent($content) {
  10465. // 根据章节标题来拆分章节内容,返回包含标题和内容的二维数组
  10466. $chapters = [];
  10467. // 匹配章节标题格式:
  10468. // 1. 标题独占一行
  10469. // 2. 格式支持:
  10470. // - **第一集**
  10471. // - 第一集:
  10472. // - ###第1章 重生
  10473. // - ##第2章 相救
  10474. // - 第三章 共处一室
  10475. // 3. 支持阿拉伯数字(0-9)和中文数字(一二两三四五六七八九十百千万)
  10476. // 4. 支持章节关键字:章、节、集、幕、场、回、话
  10477. // 5. "第"字与数字、数字与章节关键字之间可能有空格
  10478. // 6. 前后可能有#、*、·等修饰符号,后面可能有冒号、空格、标题文字等
  10479. $pattern = '/^[#*·\s\-_=]*第\s*([一二两三四五六七八九十百千万0-9]+)\s*([章节集幕场回话]).*$/mu';
  10480. // 先用正则找出所有章节标题的位置
  10481. if (preg_match_all($pattern, $content, $matches, PREG_OFFSET_CAPTURE)) {
  10482. $titleCount = count($matches[0]);
  10483. for ($i = 0; $i < $titleCount; $i++) {
  10484. // 获取当前章节标题
  10485. $titleLine = trim($matches[0][$i][0]);
  10486. // 去除标题开头的特殊符号
  10487. $cleanTitle = preg_replace('/^[#*·\-_=\s]+/', '', $titleLine);
  10488. // 去除标题结尾的冒号
  10489. $cleanTitle = preg_replace('/[#*·::\s]+$/', '', $cleanTitle);
  10490. $cleanTitle = trim($cleanTitle);
  10491. // 获取当前标题的结束位置
  10492. $titleEndPos = $matches[0][$i][1] + strlen($matches[0][$i][0]);
  10493. // 获取下一个章节标题的开始位置(如果存在)
  10494. $nextTitlePos = ($i < $titleCount - 1) ? $matches[0][$i + 1][1] : strlen($content);
  10495. // 提取章节内容(从标题结束到下一个标题开始)
  10496. $chapterContent = substr($content, $titleEndPos, $nextTitlePos - $titleEndPos);
  10497. // 清理内容:去除前后空白和分隔线
  10498. $chapterContent = trim($chapterContent);
  10499. $chapterContent = preg_replace('/^[\-\s]+/u', '', $chapterContent);
  10500. $chapterContent = preg_replace('/[\-\s]+$/u', '', $chapterContent);
  10501. if (!empty($cleanTitle) && !empty($chapterContent)) {
  10502. $chapters[] = [
  10503. 'title' => $cleanTitle,
  10504. 'content' => $chapterContent
  10505. ];
  10506. }
  10507. }
  10508. } else {
  10509. // 如果没有匹配到章节,返回整个内容
  10510. $chapters[] = [
  10511. 'title' => '',
  10512. 'content' => $content
  10513. ];
  10514. }
  10515. return $chapters;
  10516. }
  10517. public function getContentByBid($bid) {
  10518. $chapters = DB::table('chapters as c')->select('c.id', 'c.name', 'cc.content')
  10519. ->leftJoin('chapter_contents as cc', 'c.chapter_content_id', '=', 'cc.id')
  10520. ->where('c.bid', $bid)->where('c.is_check', 1)->where('c.is_deleted', 0)->orderBy('c.sequence')->get();
  10521. $content = '';
  10522. foreach ($chapters as $chapter) {
  10523. $content .= $chapter->content . PHP_EOL . PHP_EOL;
  10524. }
  10525. return $content;
  10526. }
  10527. public function getContentByScriptId($script_id) {
  10528. $content = '';
  10529. $contents = DB::table('mp_script_episode_group')->where('script_id', $script_id)->pluck('content')->toArray();
  10530. if ($contents) {
  10531. $content = implode(PHP_EOL, $contents);
  10532. }else {
  10533. $content = DB::table('mp_scripts')->where('id', $script_id)->value('content');
  10534. }
  10535. return $content;
  10536. }
  10537. /**
  10538. * 根据文件类型提取文本内容
  10539. *
  10540. * @param mixed $file 文件对象或文件路径
  10541. * @return string
  10542. */
  10543. public function extractFileContent($file) {
  10544. // 获取文件路径和扩展名
  10545. if (is_string($file)) {
  10546. $filePath = $file;
  10547. $extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
  10548. } else {
  10549. // Laravel UploadedFile 对象
  10550. $filePath = $file->getRealPath();
  10551. $extension = strtolower($file->getClientOriginalExtension());
  10552. }
  10553. $content = '';
  10554. switch ($extension) {
  10555. case 'txt':
  10556. $content = $this->extractTxtContent($filePath);
  10557. break;
  10558. case 'pdf':
  10559. $content = $this->extractPdfContent($filePath);
  10560. break;
  10561. case 'doc':
  10562. case 'docx':
  10563. $content = $this->extractWordContent($filePath);
  10564. break;
  10565. // case 'jpg':
  10566. // case 'jpeg':
  10567. // case 'png':
  10568. // case 'gif':
  10569. // case 'bmp':
  10570. // case 'webp':
  10571. // $content = $this->extractImageContent($filePath);
  10572. // break;
  10573. default:
  10574. Utils::throwError('20003:不支持的文件格式,支持:txt、pdf');
  10575. }
  10576. return $content;
  10577. }
  10578. /**
  10579. * 提取 TXT 文件内容
  10580. */
  10581. private function extractTxtContent($filePath) {
  10582. if (!file_exists($filePath)) {
  10583. Utils::throwError('20003:文件不存在');
  10584. }
  10585. $content = file_get_contents($filePath);
  10586. // 尝试检测并转换编码
  10587. $encoding = mb_detect_encoding($content, ['UTF-8', 'GBK', 'GB2312', 'BIG5'], true);
  10588. if ($encoding && $encoding !== 'UTF-8') {
  10589. $content = mb_convert_encoding($content, 'UTF-8', $encoding);
  10590. }
  10591. return trim($content);
  10592. }
  10593. /**
  10594. * 提取 PDF 文件内容
  10595. */
  10596. private function extractPdfContent($filePath) {
  10597. if (!file_exists($filePath)) {
  10598. Utils::throwError('20003:文件不存在');
  10599. }
  10600. try {
  10601. $parser = new PdfParser();
  10602. $pdf = $parser->parseFile($filePath);
  10603. $content = $pdf->getText();
  10604. return trim($content);
  10605. } catch (\Exception $e) {
  10606. dLog('deepseek')->error('PDF解析失败: ' . $e->getMessage());
  10607. logDB('deepseek', 'error', 'PDF解析失败', ['error' => $e->getMessage()]);
  10608. Utils::throwError('20003:PDF解析失败,请确保文件格式正确');
  10609. }
  10610. }
  10611. /**
  10612. * 提取 Word 文件内容(支持 doc 和 docx)
  10613. */
  10614. private function extractWordContent($filePath) {
  10615. if (!file_exists($filePath)) {
  10616. Utils::throwError('20003:文件不存在');
  10617. }
  10618. try {
  10619. $phpWord = WordIOFactory::load($filePath);
  10620. $content = '';
  10621. foreach ($phpWord->getSections() as $section) {
  10622. foreach ($section->getElements() as $element) {
  10623. $content .= $this->extractWordElementText($element);
  10624. }
  10625. }
  10626. return trim($content);
  10627. } catch (\Exception $e) {
  10628. dLog('deepseek')->error('Word解析失败: ' . $e->getMessage());
  10629. logDB('deepseek', 'error', 'Word解析失败', ['error' => $e->getMessage()]);
  10630. Utils::throwError('20003:Word解析失败,请确保文件格式正确');
  10631. }
  10632. }
  10633. /**
  10634. * 递归提取 Word 元素中的文本
  10635. */
  10636. private function extractWordElementText($element) {
  10637. $text = '';
  10638. if (method_exists($element, 'getText')) {
  10639. $text .= $element->getText() . "\n";
  10640. } elseif (method_exists($element, 'getElements')) {
  10641. foreach ($element->getElements() as $childElement) {
  10642. $text .= $this->extractWordElementText($childElement);
  10643. }
  10644. }
  10645. return $text;
  10646. }
  10647. /**
  10648. * 提取图片内容(使用火山引擎 OCR)
  10649. */
  10650. private function extractImageContent($filePath) {
  10651. if (!file_exists($filePath)) {
  10652. Utils::throwError('20003:文件不存在');
  10653. }
  10654. try {
  10655. // 读取图片并转换为 base64
  10656. $imageData = file_get_contents($filePath);
  10657. $base64Image = base64_encode($imageData);
  10658. // 调用火山引擎 OCR 服务
  10659. $content = $this->callVolcEngineOCR($base64Image);
  10660. return trim($content);
  10661. } catch (\Exception $e) {
  10662. dLog('deepseek')->error('图片OCR识别失败: ' . $e->getMessage());
  10663. logDB('deepseek', 'error', '图片OCR识别失败', ['error' => $e->getMessage()]);
  10664. Utils::throwError('20003:图片识别失败');
  10665. }
  10666. }
  10667. /**
  10668. * 调用火山引擎 OCR 服务识别图片文字
  10669. *
  10670. * @param string $base64Image base64 编码的图片数据
  10671. * @return string 识别的文字内容
  10672. */
  10673. private function callVolcEngineOCR($base64Image) {
  10674. $method = 'POST';
  10675. $service = 'cv';
  10676. $host = 'visual.volcengineapi.com';
  10677. $region = env('VOLC_REGION', 'cn-north-1');
  10678. $access_key = env('VOLC_AK');
  10679. $secret_key = env('VOLC_SK');
  10680. $action = 'OCRNormal';
  10681. $version = '2020-08-26';
  10682. if (!$access_key || !$secret_key) {
  10683. Utils::throwError('20003:请配置火山引擎 AK/SK');
  10684. }
  10685. // 请求体
  10686. $body = json_encode([
  10687. 'image_base64' => $base64Image
  10688. ]);
  10689. // 生成签名
  10690. $headers = $this->getVolcEngineSignHeaders(
  10691. $method, $service, $host, $region,
  10692. "Action={$action}&Version={$version}",
  10693. $access_key, $secret_key, $body
  10694. );
  10695. $client = new Client(['timeout' => 1800, 'verify' => false]);
  10696. $response = $client->post("https://{$host}/?Action={$action}&Version={$version}", [
  10697. 'headers' => $headers,
  10698. 'body' => $body
  10699. ]);
  10700. $response_arr = json_decode($response->getBody()->getContents(), true);
  10701. // 提取识别的文字
  10702. $textLines = [];
  10703. if (isset($response_arr['data']['line_texts'])) {
  10704. $textLines = $response_arr['data']['line_texts'];
  10705. } elseif (isset($response_arr['data']['ocr_infos'])) {
  10706. foreach ($response_arr['data']['ocr_infos'] as $info) {
  10707. if (isset($info['text'])) {
  10708. $textLines[] = $info['text'];
  10709. }
  10710. }
  10711. }
  10712. if (empty($textLines)) {
  10713. dLog('deepseek')->warning('OCR识别结果为空', ['response' => $response_arr]);
  10714. return '';
  10715. }
  10716. return implode("\n", $textLines);
  10717. }
  10718. /**
  10719. * 生成火山引擎 API 签名头(用于 OCR 等 POST 请求)
  10720. */
  10721. private function getVolcEngineSignHeaders($method, $service, $host, $region, $queryString, $access_key, $secret_key, $body = '') {
  10722. $contentType = 'application/json';
  10723. $accept = 'application/json';
  10724. // 获取当前UTC时间
  10725. $t = new DateTime('now', new DateTimeZone('UTC'));
  10726. $xDate = $t->format('Ymd\THis\Z');
  10727. $dateStamp = $t->format('Ymd');
  10728. // 计算 body 的 sha256 哈希
  10729. $payloadHash = hash('sha256', $body);
  10730. // 1. 拼接规范请求串
  10731. $canonicalUri = '/';
  10732. $canonicalQueryString = $queryString;
  10733. $canonicalHeaders = "content-type:{$contentType}\nhost:{$host}\nx-content-sha256:{$payloadHash}\nx-date:{$xDate}\n";
  10734. $signedHeaders = 'content-type;host;x-content-sha256;x-date';
  10735. $canonicalRequest = implode("\n", [
  10736. $method,
  10737. $canonicalUri,
  10738. $canonicalQueryString,
  10739. $canonicalHeaders,
  10740. $signedHeaders,
  10741. $payloadHash
  10742. ]);
  10743. // 2. 拼接待签名字符串
  10744. $algorithm = 'HMAC-SHA256';
  10745. $credentialScope = implode('/', [$dateStamp, $region, $service, 'request']);
  10746. $hashedCanonicalRequest = hash('sha256', $canonicalRequest);
  10747. $stringToSign = implode("\n", [
  10748. $algorithm,
  10749. $xDate,
  10750. $credentialScope,
  10751. $hashedCanonicalRequest
  10752. ]);
  10753. // 3. 计算签名
  10754. $signingKey = $this->getSignatureKey($secret_key, $dateStamp, $region, $service);
  10755. $signature = hash_hmac('sha256', $stringToSign, $signingKey);
  10756. // 4. 添加签名到请求头
  10757. $authorizationHeader = sprintf(
  10758. '%s Credential=%s/%s, SignedHeaders=%s, Signature=%s',
  10759. $algorithm,
  10760. $access_key,
  10761. $credentialScope,
  10762. $signedHeaders,
  10763. $signature
  10764. );
  10765. return [
  10766. 'Accept' => $accept,
  10767. 'Content-Type' => $contentType,
  10768. 'Host' => $host,
  10769. 'X-Date' => $xDate,
  10770. 'X-Content-Sha256'=> $payloadHash,
  10771. 'Authorization' => $authorizationHeader
  10772. ];
  10773. }
  10774. public function generateScriptWords($cid, $model = 'r1') {
  10775. ini_set('max_execution_time', 0);
  10776. if (!$cid) Utils::throwError('20003: 请选择章节!');
  10777. $content = DB::table('chapters as c')->leftJoin('chapter_contents as cc', 'c.chapter_content_id', '=', 'cc.id')->where('c.id', $cid)->value('cc.content');
  10778. // 模型兼容性处理和思考模式设置
  10779. $thinkingMode = 'disabled';
  10780. $reasoningEffort = 'high';
  10781. if ($model == 'r1') {
  10782. $model = 'deepseek-v4-flash'; // r1 映射到 deepseek-v4-flash 思考模式
  10783. $thinkingMode = 'enabled';
  10784. } else {
  10785. $model = 'deepseek-v4-flash'; // 默认映射到 deepseek-v4-flash 非思考模式
  10786. $thinkingMode = 'disabled';
  10787. }
  10788. $messages = [
  10789. [
  10790. 'role' => 'system',
  10791. 'content' => '下面有一段小说文本,请帮我将文本中的每句话按从上到下的顺序拆分成角色不同的剧本文稿(不得更改上下文顺序和内容),文稿形式严格按照“角色名(男或女):台词{情感}”输出,需要注意以下几点要求:
  10792. 1.角色名后不要加入任何其他词语,只能加不包括旁白的性别,在男或女中选
  10793. 2.非对话部分请全部用旁白角色代替
  10794. 3.情感必须在【开心、悲伤、生气、惊讶、恐惧、厌恶、激动、冷漠、中性、沮丧、撒娇、害羞、安慰鼓励、咆哮、温柔、自然讲述、情感电台、磁性、广告营销、气泡音、新闻播报、娱乐八卦】中选一个,不得使用其他词语'
  10795. ],
  10796. [
  10797. 'role' => 'user',
  10798. 'content' => $content
  10799. ]
  10800. ];
  10801. $post_data = [
  10802. 'model' => $model,
  10803. 'messages' => $messages,
  10804. // 'max_tokens' => 8192,
  10805. 'temperature' => 1,
  10806. 'frequency_penalty' => 0,
  10807. 'presence_penalty' => 0,
  10808. 'thinking' => ['type' => $thinkingMode],
  10809. 'response_format' => [
  10810. 'type' => 'text'
  10811. ],
  10812. 'stream' => false
  10813. ];
  10814. if ($thinkingMode == 'enabled') $post_data['reasoning_effort'] = $reasoningEffort;
  10815. // 根据模型类型选择调用方法
  10816. $fullContent = '';
  10817. if (in_array($model, ['deepseek-reasoner', 'deepseek-chat', 'deepseek-v4-flash', 'deepseek-v4-pro'])) {
  10818. // DeepSeek 官方模型使用 DeepSeek API
  10819. $chatResult = $this->chatOnly($post_data);
  10820. } else if (in_array($model, $this->gpt_text_models)) {
  10821. // GPT-5.4 模型使用 TokenRouter API
  10822. $chatResult = $this->gpt54ChatOnly($post_data);
  10823. } else {
  10824. // 其他模型使用火山引擎API
  10825. $chatResult = $this->volcEngineChatCompletion($post_data);
  10826. }
  10827. if (is_array($chatResult)) {
  10828. $fullContent = $chatResult['fullContent'];
  10829. }
  10830. return $fullContent;
  10831. }
  10832. /**
  10833. * 智能化解析集数信息
  10834. * 支持多种表达方式,如:改成3集、调整成5集、修改为10集等
  10835. *
  10836. * @param string $prompt 用户输入的提示词
  10837. * @return int|null 解析出的集数,如果没有找到则返回null
  10838. */
  10839. private function extractEpisodeNumber($prompt)
  10840. {
  10841. if (empty($prompt)) {
  10842. return null;
  10843. }
  10844. // 定义各种可能的表达模式
  10845. $patterns = [
  10846. // 基本模式:改成N集、调整成N集、修改成N集
  10847. '/(?:改成|调整成|修改成|变成|设置成|设为|改为|调为|修改为|变为)\s*(\d+)\s*集/u',
  10848. // 扩展模式:分成N集、拆分成N集、分割成N集
  10849. '/(?:分成|拆分成|分割成|划分成|分为)\s*(\d+)\s*集/u',
  10850. // 数量模式:N集、共N集、总共N集
  10851. '/(?:共|总共|一共|总计)?\s*(\d+)\s*集/u',
  10852. // 制作模式:制作N集、生成N集、创建N集
  10853. '/(?:制作|生成|创建|产出|输出)\s*(\d+)\s*集/u',
  10854. // 计划模式:计划N集、预计N集、打算N集
  10855. '/(?:计划|预计|打算|准备|想要)\s*(\d+)\s*集/u',
  10856. // 需要模式:需要N集、要N集
  10857. '/(?:需要|要|需)\s*(\d+)\s*集/u',
  10858. // 中文数字模式:改成三集、调整成五集等
  10859. '/(?:改成|调整成|修改成|变成|设置成|设为|改为|调为|修改为|变为|分成|拆分成|分割成|划分成|分为|制作|生成|创建|产出|输出|计划|预计|打算|准备|想要|需要|要|需)\s*([一二三四五六七八九十]+)\s*集/u',
  10860. // 英文数字模式
  10861. '/(?: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',
  10862. ];
  10863. // 中文数字转阿拉伯数字的映射
  10864. $chineseNumbers = [
  10865. '一' => 1, '二' => 2, '三' => 3, '四' => 4, '五' => 5,
  10866. '六' => 6, '七' => 7, '八' => 8, '九' => 9, '十' => 10,
  10867. '十一' => 11, '十二' => 12, '十三' => 13, '十四' => 14, '十五' => 15,
  10868. '十六' => 16, '十七' => 17, '十八' => 18, '十九' => 19, '二十' => 20
  10869. ];
  10870. // 逐个尝试匹配模式
  10871. foreach ($patterns as $pattern) {
  10872. if (preg_match($pattern, $prompt, $matches)) {
  10873. $number = trim($matches[1]);
  10874. // 如果是中文数字,转换为阿拉伯数字
  10875. if (isset($chineseNumbers[$number])) {
  10876. return $chineseNumbers[$number];
  10877. }
  10878. // 如果是阿拉伯数字,直接返回
  10879. if (is_numeric($number)) {
  10880. $episodeNum = intval($number);
  10881. // 合理性检查:集数应该在1-100之间
  10882. if ($episodeNum >= 1 && $episodeNum <= 100) {
  10883. return $episodeNum;
  10884. }
  10885. }
  10886. }
  10887. }
  10888. return null;
  10889. }
  10890. /**
  10891. * 调用火山引擎对话(Chat) API
  10892. *
  10893. * @param array $params 包含以下参数:
  10894. * - model: 模型ID(必填)
  10895. * - messages: 消息列表(必填)
  10896. * - stream: 是否流式输出(可选,默认false)
  10897. * - max_tokens: 最大输出token数(可选)
  10898. * - temperature: 采样温度(可选,默认1)
  10899. * - top_p: 核采样概率(可选,默认0.7)
  10900. * - frequency_penalty: 频率惩罚系数(可选,默认0)
  10901. * - presence_penalty: 存在惩罚系数(可选,默认0)
  10902. * - stop: 停止词(可选)
  10903. * - tools: 工具列表(可选)
  10904. * @return array|Generator 非流式返回数组,流式返回生成器
  10905. */
  10906. public function volcEngineChatCompletion(array $params)
  10907. {
  10908. $apiKey = env('VOLC_AI_API_KEY');
  10909. if (empty($apiKey)) {
  10910. Utils::throwError('20003:火山引擎API Key未配置,请检查环境变量VOLC_AI_API_KEY');
  10911. }
  10912. // 构建请求参数
  10913. $originalModel = $params['model'] ?? 'deepseek-v3-2-251201';
  10914. $requestData = [
  10915. 'model' => isset(\App\Consts\BaseConst::MODELS_MAP[$originalModel]) ? \App\Consts\BaseConst::MODELS_MAP[$originalModel] : $originalModel,
  10916. 'messages' => $params['messages'] ?? [],
  10917. ];
  10918. if (!in_array($originalModel, $this->valid_text_models)) {
  10919. Utils::throwError('20003:该模型不支持!');
  10920. }
  10921. // 添加可选参数
  10922. if (isset($params['stream'])) {
  10923. $requestData['stream'] = $params['stream'];
  10924. }
  10925. if (isset($params['stream_options'])) {
  10926. $requestData['stream_options'] = $params['stream_options'];
  10927. }
  10928. if (isset($params['max_tokens'])) {
  10929. $requestData['max_tokens'] = $params['max_tokens'];
  10930. }else if (isset($params['max_completion_tokens'])) {
  10931. $requestData['max_completion_tokens'] = $params['max_completion_tokens'];
  10932. }else {
  10933. $requestData['max_completion_tokens'] = 100000;
  10934. }
  10935. if (isset($params['temperature'])) {
  10936. $requestData['temperature'] = $params['temperature'];
  10937. }
  10938. if (isset($params['top_p'])) {
  10939. $requestData['top_p'] = $params['top_p'];
  10940. }
  10941. if (isset($params['frequency_penalty'])) {
  10942. $requestData['frequency_penalty'] = $params['frequency_penalty'];
  10943. }
  10944. if (isset($params['presence_penalty'])) {
  10945. $requestData['presence_penalty'] = $params['presence_penalty'];
  10946. }
  10947. if (isset($params['stop'])) {
  10948. $requestData['stop'] = $params['stop'];
  10949. }
  10950. if (isset($params['tools'])) {
  10951. $requestData['tools'] = $params['tools'];
  10952. }
  10953. if (isset($params['tool_choice'])) {
  10954. $requestData['tool_choice'] = $params['tool_choice'];
  10955. }
  10956. if (isset($params['response_format'])) {
  10957. $requestData['response_format'] = $params['response_format'];
  10958. }
  10959. if (isset($params['thinking'])) {
  10960. // 如果 thinking 已经是数组格式,直接使用;否则转换为正确格式
  10961. if (is_array($params['thinking'])) {
  10962. $requestData['thinking'] = $params['thinking'];
  10963. if (isset($params['reasoning_effort']) && $params['thinking']['type'] == 'enabled') {
  10964. $requestData['reasoning_effort'] = $params['reasoning_effort'];
  10965. }
  10966. } else {
  10967. $requestData['thinking'] = ['type' => $params['thinking']];
  10968. if (isset($params['reasoning_effort']) && $params['thinking'] == 'enabled') {
  10969. $requestData['reasoning_effort'] = $params['reasoning_effort'];
  10970. }
  10971. }
  10972. } else {
  10973. $requestData['thinking'] = ['type' => 'disabled'];
  10974. }
  10975. dLog('deepseek')->info('火山引擎对话API请求参数: ', $requestData);
  10976. $client = new Client(['verify' => false, 'timeout' => 1800]);
  10977. try {
  10978. // 判断是否为流式输出
  10979. $isStream = isset($params['stream']) && $params['stream'] === true;
  10980. if ($isStream) {
  10981. // 流式输出
  10982. return $this->volcEngineChatCompletionStream($client, $apiKey, $requestData);
  10983. } else {
  10984. // 非流式输出
  10985. $response = $client->post('https://ark.cn-beijing.volces.com/api/v3/chat/completions', [
  10986. 'headers' => [
  10987. 'Authorization' => 'Bearer ' . $apiKey,
  10988. 'Content-Type' => 'application/json',
  10989. ],
  10990. 'json' => $requestData
  10991. ]);
  10992. $responseData = json_decode($response->getBody(), true);
  10993. dLog('deepseek')->info('火山引擎对话API响应: ', $responseData);
  10994. $usage = isset($responseData['usage']) ? $responseData['usage'] : [];
  10995. if (isset($responseData['choices']) && count($responseData['choices']) > 0) {
  10996. $fullContent = isset($responseData['choices'][0]['message']['content']) ? $responseData['choices'][0]['message']['content'] : '';
  10997. $fullReasoningContent = isset($responseData['choices'][0]['message']['reasoning_content']) ? $responseData['choices'][0]['message']['reasoning_content'] : '';
  10998. }
  10999. return [
  11000. 'fullContent' => $fullContent,
  11001. 'fullReasoningContent' => $fullReasoningContent,
  11002. 'usage' => $usage
  11003. ];
  11004. }
  11005. } catch (\Exception $e) {
  11006. dLog('deepseek')->error('火山引擎对话API请求异常: ' . $e->getMessage());
  11007. logDB('deepseek', 'error', '火山引擎对话API请求失败', ['error' => $e->getMessage(), 'model' => $requestData['model']]);
  11008. Utils::throwError('20003:火山引擎对话API请求失败: ' . $e->getMessage());
  11009. }
  11010. }
  11011. /**
  11012. * 火山引擎对话API流式输出
  11013. *
  11014. * @param Client $client HTTP客户端
  11015. * @param string $apiKey API密钥
  11016. * @param array $requestData 请求数据
  11017. * @return Generator
  11018. */
  11019. private function volcEngineChatCompletionStream($client, $apiKey, $requestData)
  11020. {
  11021. try {
  11022. $response = $client->post('https://ark.cn-beijing.volces.com/api/v3/chat/completions', [
  11023. 'headers' => [
  11024. 'Authorization' => 'Bearer ' . $apiKey,
  11025. 'Content-Type' => 'application/json',
  11026. ],
  11027. 'json' => $requestData,
  11028. 'stream' => true
  11029. ]);
  11030. $body = $response->getBody();
  11031. $buffer = '';
  11032. $fullContent = '';
  11033. $fullReasoningContent = '';
  11034. $usage = [];
  11035. dLog('deepseek')->info('开始读取火山引擎对话API流式响应');
  11036. // 逐行读取流式响应
  11037. while (!$body->eof()) {
  11038. $chunk = $body->read(1024);
  11039. $buffer .= $chunk;
  11040. // 按行分割
  11041. $lines = explode("\n", $buffer);
  11042. $buffer = array_pop($lines);
  11043. foreach ($lines as $line) {
  11044. $line = trim($line);
  11045. if (empty($line)) {
  11046. continue;
  11047. }
  11048. // 检查是否是SSE数据行
  11049. if (strpos($line, 'data: ') !== 0) {
  11050. continue;
  11051. }
  11052. $data = substr($line, 6);
  11053. if ($data === '[DONE]') {
  11054. dLog('deepseek')->info('收到结束标记');
  11055. break 2;
  11056. }
  11057. try {
  11058. $json = json_decode($data, true);
  11059. if (json_last_error() !== JSON_ERROR_NONE) {
  11060. dLog('deepseek')->warning('JSON解析错误: ' . json_last_error_msg());
  11061. continue;
  11062. }
  11063. if (isset($json['choices'][0]['delta'])) {
  11064. $delta = $json['choices'][0]['delta'];
  11065. // 处理思维链内容
  11066. if (isset($delta['reasoning_content'])) {
  11067. $fullReasoningContent .= $delta['reasoning_content'];
  11068. yield [
  11069. 'type' => 'reasoning',
  11070. 'content' => $delta['reasoning_content'],
  11071. 'full_reasoning' => $fullReasoningContent
  11072. ];
  11073. }
  11074. // 处理回答内容
  11075. if (isset($delta['content'])) {
  11076. $fullContent .= $delta['content'];
  11077. yield [
  11078. 'type' => 'content',
  11079. 'content' => $delta['content'],
  11080. ];
  11081. }
  11082. }
  11083. // 获取使用统计信息
  11084. if (isset($json['usage'])) {
  11085. $usage = $json['usage'];
  11086. dLog('deepseek')->info('收到使用统计', ['usage' => $usage]);
  11087. }
  11088. } catch (\Exception $e) {
  11089. dLog('deepseek')->error('解析流式响应失败: ' . $e->getMessage());
  11090. continue;
  11091. }
  11092. }
  11093. }
  11094. dLog('deepseek')->info('流式读取完成', [
  11095. 'content_length' => strlen($fullContent),
  11096. 'reasoning_length' => strlen($fullReasoningContent)
  11097. ]);
  11098. yield [
  11099. 'type' => 'done',
  11100. 'full_content' => $fullContent,
  11101. 'full_reasoning' => $fullReasoningContent,
  11102. 'usage' => $usage
  11103. ];
  11104. } catch (\Exception $e) {
  11105. dLog('deepseek')->error('火山引擎对话API流式请求异常: ' . $e->getMessage());
  11106. logDB('deepseek', 'error', '火山引擎对话API流式请求失败', ['error' => $e->getMessage()]);
  11107. throw $e;
  11108. }
  11109. }
  11110. /**
  11111. * 检查并创建图片生成任务
  11112. *
  11113. * @param array $merged_items 合并后的资源列表(roles 或 scenes)
  11114. * @param array $global_items 全局资源列表(来自 mp_animes 表)
  11115. * @param string $nameKey 资源名称字段('role' 或 'scene')
  11116. * @param string $art_style 美术风格
  11117. * @return array 更新后的资源列表
  11118. */
  11119. private function checkAndCreateImageTasks(array $merged_items, array $global_items, string $nameKey, string $art_style = ''): array
  11120. {
  11121. // 确保参数为数组
  11122. $merged_items = is_array($merged_items) ? $merged_items : [];
  11123. $global_items = is_array($global_items) ? $global_items : [];
  11124. // 构建全局资源映射表,用于快速查找
  11125. $global_map = [];
  11126. foreach ($global_items as $item) {
  11127. $itemKey = $this->normalizeEpisodeResourceKey(getProp($item, $nameKey));
  11128. if ($itemKey === '') {
  11129. continue;
  11130. }
  11131. $global_map[$itemKey] = $item;
  11132. }
  11133. // 遍历合并后的资源列表,检查是否需要创建图片生成任务
  11134. foreach ($merged_items as &$item) {
  11135. $item_name = getProp($item, $nameKey);
  11136. $item_description = getProp($item, 'description');
  11137. // $item_url = getProp($item, 'url');
  11138. // // 如果已有图片URL,跳过
  11139. // if (!empty($item_url)) {
  11140. // continue;
  11141. // }
  11142. // 如果是旁白角色,跳过
  11143. if ($nameKey === 'role' && $item_name === '旁白') {
  11144. continue;
  11145. }
  11146. $itemKey = $this->normalizeEpisodeResourceKey($item_name);
  11147. if ($itemKey === '') {
  11148. continue;
  11149. }
  11150. $need_create_task = false;
  11151. // 条件1:在全局资源中找不到匹配的名称
  11152. if (!isset($global_map[$itemKey])) {
  11153. $need_create_task = true;
  11154. } else {
  11155. // 条件2:名称匹配但描述或 pic_prompt 不一致
  11156. $global_description = trim((string)getProp($global_map[$itemKey], 'description'));
  11157. $current_description = trim((string)$item_description);
  11158. $global_pic_prompt = trim((string)getProp($global_map[$itemKey], 'pic_prompt'));
  11159. $current_pic_prompt = trim((string)getProp($item, 'pic_prompt'));
  11160. // 如果 description 或 pic_prompt 有变化,需要重新生成
  11161. if ($global_description !== $current_description || $global_pic_prompt !== $current_pic_prompt) {
  11162. $need_create_task = true;
  11163. }
  11164. }
  11165. // 如果需要创建任务,调用图片生成服务
  11166. if ($need_create_task) {
  11167. try {
  11168. // 优先使用 pic_prompt,如果没有则使用 description
  11169. $pic_prompt = getProp($item, 'pic_prompt');
  11170. $description = !empty($pic_prompt) ? trim((string)$pic_prompt) : $item_description;
  11171. // 添加美术风格前缀
  11172. if ($art_style) {
  11173. $prefix = $nameKey === 'role' ? '主体描述' : '场景描述';
  11174. // $description = "美术风格:\n{$art_style}\n\n{$prefix}:{$description}";
  11175. $description = "{$prefix}:{$description}";
  11176. }
  11177. $params = [
  11178. 'prompt' => $description,
  11179. 'ref_img_urls' => []
  11180. ];
  11181. $task = $this->aiImageGenerationService->createImageGenerationTask($params);
  11182. $task_id = $task->id;
  11183. if ($task_id) {
  11184. $item['task_id'] = $task_id;
  11185. $item['task_status'] = 'processing';
  11186. $resource_type = $nameKey === 'role' ? '角色' : '场景';
  11187. dLog('deepseek')->info("{$resource_type}({$item_name})创建图片生成任务成功", ['task_id' => $task_id]);
  11188. }
  11189. } catch (\Exception $e) {
  11190. $resource_type = $nameKey === 'role' ? '角色' : '场景';
  11191. dLog('deepseek')->error("{$resource_type}({$item_name})创建图片生成任务失败: " . $e->getMessage());
  11192. logDB('deepseek', 'error', "{$resource_type}创建图片生成任务失败", ['name' => $item_name, 'error' => $e->getMessage()]);
  11193. // 不抛出异常,继续处理其他资源
  11194. }
  11195. }
  11196. }
  11197. return $merged_items;
  11198. }
  11199. /**
  11200. * 同步新出现的主体和场景到全局
  11201. *
  11202. * @param int $anime_id 动漫ID
  11203. * @param array $merged_roles 合并后的角色列表
  11204. * @param array $merged_scenes 合并后的场景列表
  11205. * @param array $global_roles 全局角色列表
  11206. * @param array $global_scenes 全局场景列表
  11207. * @return void
  11208. */
  11209. private function syncNewResourcesToGlobal(
  11210. int $anime_id,
  11211. array $merged_roles,
  11212. array $merged_scenes,
  11213. array $global_roles,
  11214. array $global_scenes
  11215. ): void {
  11216. // 构建全局角色映射表
  11217. $global_role_map = [];
  11218. foreach ($global_roles as $role) {
  11219. $roleKey = $this->normalizeEpisodeResourceKey(getProp($role, 'role'));
  11220. if ($roleKey !== '') {
  11221. $global_role_map[$roleKey] = $role;
  11222. }
  11223. }
  11224. // 构建全局场景映射表
  11225. $global_scene_map = [];
  11226. foreach ($global_scenes as $scene) {
  11227. $sceneKey = $this->normalizeEpisodeResourceKey(getProp($scene, 'scene'));
  11228. if ($sceneKey !== '') {
  11229. $global_scene_map[$sceneKey] = $scene;
  11230. }
  11231. }
  11232. $need_update = false;
  11233. $new_global_roles = $global_roles;
  11234. $new_global_scenes = $global_scenes;
  11235. // 检查并添加新角色到全局
  11236. foreach ($merged_roles as $role) {
  11237. $role_name = getProp($role, 'role');
  11238. // 跳过旁白
  11239. if ($role_name === '旁白') {
  11240. continue;
  11241. }
  11242. $roleKey = $this->normalizeEpisodeResourceKey($role_name);
  11243. if ($roleKey === '') {
  11244. continue;
  11245. }
  11246. // 如果全局中不存在该角色,添加到全局
  11247. if (!isset($global_role_map[$roleKey])) {
  11248. $new_global_roles[] = [
  11249. 'role' => $role_name,
  11250. 'description' => getProp($role, 'description', ''),
  11251. 'pic_prompt' => getProp($role, 'pic_prompt', ''),
  11252. 'voice_name' => getProp($role, 'voice_name', ''),
  11253. 'voice_type' => getProp($role, 'voice_type', ''),
  11254. 'voice_audio_url' => getProp($role, 'voice_audio_url', ''),
  11255. 'url' => getProp($role, 'url', ''),
  11256. 'task_id' => getProp($role, 'task_id', ''),
  11257. 'task_status' => getProp($role, 'task_status', ''),
  11258. ];
  11259. $global_role_map[$roleKey] = true; // 标记已添加
  11260. $need_update = true;
  11261. dLog('deepseek')->info("新角色同步到全局", [
  11262. 'anime_id' => $anime_id,
  11263. 'role' => $role_name,
  11264. 'task_id' => getProp($role, 'task_id', ''),
  11265. ]);
  11266. }
  11267. // else {
  11268. // // 如果角色已存在,但描述、pic_prompt或图片信息有更新,则更新全局数据
  11269. // $global_role = $global_role_map[$roleKey];
  11270. // if (is_array($global_role)) {
  11271. // $updated = false;
  11272. // $update_fields = [];
  11273. // // 检查描述是否更新
  11274. // $current_description = trim((string)getProp($role, 'description'));
  11275. // $global_description = trim((string)getProp($global_role, 'description'));
  11276. // if ($current_description !== '' && $current_description !== $global_description) {
  11277. // $updated = true;
  11278. // $update_fields[] = 'description';
  11279. // }
  11280. // // 检查pic_prompt是否更新
  11281. // $current_pic_prompt = trim((string)getProp($role, 'pic_prompt'));
  11282. // $global_pic_prompt = trim((string)getProp($global_role, 'pic_prompt'));
  11283. // if ($current_pic_prompt !== '' && $current_pic_prompt !== $global_pic_prompt) {
  11284. // $updated = true;
  11285. // $update_fields[] = 'pic_prompt';
  11286. // }
  11287. // // 检查图片任务信息是否更新
  11288. // $current_task_id = getProp($role, 'task_id');
  11289. // $global_task_id = getProp($global_role, 'task_id');
  11290. // if ($current_task_id && $current_task_id !== $global_task_id) {
  11291. // $updated = true;
  11292. // $update_fields[] = 'task_id';
  11293. // }
  11294. // // 如果有更新,同步到全局
  11295. // if ($updated) {
  11296. // foreach ($new_global_roles as &$global_role_item) {
  11297. // $global_role_key = $this->normalizeEpisodeResourceKey(getProp($global_role_item, 'role'));
  11298. // if ($global_role_key === $roleKey) {
  11299. // if (in_array('description', $update_fields)) {
  11300. // $global_role_item['description'] = $current_description;
  11301. // }
  11302. // if (in_array('pic_prompt', $update_fields)) {
  11303. // $global_role_item['pic_prompt'] = $current_pic_prompt;
  11304. // }
  11305. // if (in_array('task_id', $update_fields)) {
  11306. // $global_role_item['task_id'] = $current_task_id;
  11307. // $global_role_item['task_status'] = getProp($role, 'task_status', '');
  11308. // $global_role_item['url'] = getProp($role, 'url', '');
  11309. // }
  11310. // break;
  11311. // }
  11312. // }
  11313. // $need_update = true;
  11314. // dLog('deepseek')->info("角色信息更新到全局", [
  11315. // 'anime_id' => $anime_id,
  11316. // 'role' => $role_name,
  11317. // 'update_fields' => implode(',', $update_fields),
  11318. // ]);
  11319. // }
  11320. // }
  11321. // }
  11322. }
  11323. // 检查并添加新场景到全局
  11324. foreach ($merged_scenes as $scene) {
  11325. $scene_name = getProp($scene, 'scene');
  11326. $sceneKey = $this->normalizeEpisodeResourceKey($scene_name);
  11327. if ($sceneKey === '') {
  11328. continue;
  11329. }
  11330. // 如果全局中不存在该场景,添加到全局
  11331. if (!isset($global_scene_map[$sceneKey])) {
  11332. $new_global_scenes[] = [
  11333. 'scene' => $scene_name,
  11334. 'description' => getProp($scene, 'description', ''),
  11335. 'pic_prompt' => getProp($scene, 'pic_prompt', ''),
  11336. 'url' => getProp($scene, 'url', ''),
  11337. 'task_id' => getProp($scene, 'task_id', ''),
  11338. 'task_status' => getProp($scene, 'task_status', ''),
  11339. ];
  11340. $global_scene_map[$sceneKey] = true; // 标记已添加
  11341. $need_update = true;
  11342. dLog('deepseek')->info("新场景同步到全局", [
  11343. 'anime_id' => $anime_id,
  11344. 'scene' => $scene_name,
  11345. 'task_id' => getProp($scene, 'task_id', ''),
  11346. ]);
  11347. }
  11348. // else {
  11349. // // 如果场景已存在,但描述、pic_prompt或图片信息有更新,则更新全局数据
  11350. // $global_scene = $global_scene_map[$sceneKey];
  11351. // if (is_array($global_scene)) {
  11352. // $updated = false;
  11353. // $update_fields = [];
  11354. // // 检查描述是否更新
  11355. // $current_description = trim((string)getProp($scene, 'description'));
  11356. // $global_description = trim((string)getProp($global_scene, 'description'));
  11357. // if ($current_description !== '' && $current_description !== $global_description) {
  11358. // $updated = true;
  11359. // $update_fields[] = 'description';
  11360. // }
  11361. // // 检查pic_prompt是否更新
  11362. // $current_pic_prompt = trim((string)getProp($scene, 'pic_prompt'));
  11363. // $global_pic_prompt = trim((string)getProp($global_scene, 'pic_prompt'));
  11364. // if ($current_pic_prompt !== '' && $current_pic_prompt !== $global_pic_prompt) {
  11365. // $updated = true;
  11366. // $update_fields[] = 'pic_prompt';
  11367. // }
  11368. // // 检查图片任务信息是否更新
  11369. // $current_task_id = getProp($scene, 'task_id');
  11370. // $global_task_id = getProp($global_scene, 'task_id');
  11371. // if ($current_task_id && $current_task_id !== $global_task_id) {
  11372. // $updated = true;
  11373. // $update_fields[] = 'task_id';
  11374. // }
  11375. // // 如果有更新,同步到全局
  11376. // if ($updated) {
  11377. // foreach ($new_global_scenes as &$global_scene_item) {
  11378. // $global_scene_key = $this->normalizeEpisodeResourceKey(getProp($global_scene_item, 'scene'));
  11379. // if ($global_scene_key === $sceneKey) {
  11380. // if (in_array('description', $update_fields)) {
  11381. // $global_scene_item['description'] = $current_description;
  11382. // }
  11383. // if (in_array('pic_prompt', $update_fields)) {
  11384. // $global_scene_item['pic_prompt'] = $current_pic_prompt;
  11385. // }
  11386. // if (in_array('task_id', $update_fields)) {
  11387. // $global_scene_item['task_id'] = $current_task_id;
  11388. // $global_scene_item['task_status'] = getProp($scene, 'task_status', '');
  11389. // $global_scene_item['url'] = getProp($scene, 'url', '');
  11390. // }
  11391. // break;
  11392. // }
  11393. // }
  11394. // $need_update = true;
  11395. // dLog('deepseek')->info("场景信息更新到全局", [
  11396. // 'anime_id' => $anime_id,
  11397. // 'scene' => $scene_name,
  11398. // 'update_fields' => implode(',', $update_fields),
  11399. // ]);
  11400. // }
  11401. // }
  11402. // }
  11403. }
  11404. // 如果有新增或更新,更新数据库
  11405. if ($need_update) {
  11406. $update_data = [
  11407. 'roles' => json_encode($new_global_roles, 256),
  11408. 'scenes' => json_encode($new_global_scenes, 256),
  11409. 'updated_at' => date('Y-m-d H:i:s'),
  11410. ];
  11411. $result = DB::table('mp_animes')->where('id', $anime_id)->update($update_data);
  11412. if ($result !== false) {
  11413. dLog('deepseek')->info("全局角色和场景更新成功", [
  11414. 'anime_id' => $anime_id,
  11415. 'roles_count' => count($new_global_roles),
  11416. 'scenes_count' => count($new_global_scenes),
  11417. ]);
  11418. } else {
  11419. dLog('deepseek')->error("全局角色和场景更新失败", ['anime_id' => $anime_id]);
  11420. logDB('deepseek', 'error', '全局角色和场景更新失败', ['anime_id' => $anime_id]);
  11421. }
  11422. }
  11423. }
  11424. }