| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867 |
- <?php
- namespace App\Services\DeepSeek;
- use App\Consts\ErrorConst;
- use App\Facade\Site;
- use App\Libs\Utils;
- use GuzzleHttp\Client;
- use DateTime;
- use DateTimeZone;
- use App\Services\AIGeneration\AIImageGenerationService;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Redis;
- use OSS\Core\OssException;
- use OSS\OssClient;
- use Smalot\PdfParser\Parser as PdfParser;
- use TCPDF;
- use PhpOffice\PhpWord\IOFactory as WordIOFactory;
- use Predis\Command\Traits\Get\Get;
- class DeepSeekService
- {
- private $url;
- private $api_key;
- private $sys_message;
- private $headers;
- private $valid_text_models;
- protected $aiImageGenerationService;
- public function __construct(AIImageGenerationService $aiImageGenerationService) {
- $this->aiImageGenerationService = $aiImageGenerationService;
- $this->url = 'https://api.deepseek.com/chat/completions'; // DeepSeek API请求地址
- $this->api_key = env('DEEPSEEK_API_KEY');
- $this->headers = [
- 'Authorization' => 'Bearer '.$this->api_key,
- 'Content-Type' => 'application/json; charset=UTF-8'
- ];
- // 火山引擎模型列表
- $this->valid_text_models = DB::table('mp_text_models')->where('is_enabled', 1)->pluck('model')->toArray();
- $this->sys_message = [
- 'role' => 'system',
- 'content' => "你是一个专业的文档分析助手及资深编剧,请根据用户提供的文档内容完成示例格式的内容输出(需通过以下几个板块进行回复: <故事梗概><剧本亮点><人物关系><核心矛盾><主体列表><美术风格><场景列表><分集剧本>,每个板块之间用###分隔;同时板块之间需满足以下要求:\n
- 强制要求:\n
- 1.你只负责与剧本相关的内容,如果用户输入与剧本无关的内容,请告知用户并拒绝继续回答,此为第一优先原则,不满足此原则就忽略其他要求并停止继续输出
- 2.强制按照示例格式返回内容,不要更改格式或随意增加符号\n
- 普通要求:\n
- 1.<主体列表>中出现的主体必须尽可能统一,只允许同一个角色年龄段相差较大的情况下可重复,比如梁萧-少年、梁萧-青年、梁萧-中年、梁萧-老年这种情况,不得出现梁萧-愤怒、梁萧-隐忍、梁萧-求生等情绪和状态变化的同一角色;并且角色的样貌、形体及穿着描述尽可能详细\n
- 2.<分集剧本>要求包含“分集序号”、“分集标题”以及“分集信息”,“分集信息”需包含“场景描述”、“运镜”、“出场角色”和“台词内容”这几个部分,其中“出场角色”和“场景描述”必须在前面的板块<主体列表>和<场景列表>中选取,不得自定义。\n\n
- 示例如下:\n
- ###剧本名:西昆仑
- ###故事梗概
- 内容梗概:古风厚涂的世界中,少女阿雪身中奇毒“问心刺”,气息奄奄。少年梁萧心急如焚,用奇珍“阴阳球”稳住阿雪性命,却发现必须解衣拔刺。阿雪羞涩难当,梁萧情急之下提出结拜兄妹,以化解尴尬。两人匆匆结拜后,梁萧小心翼翼地拔出“问心刺”,竟发现它是一根诡异的女子秀发。阿雪转危为安,两人之间,在生死与羞涩交织的瞬间,情愫暗生,却又被兄妹之名束缚,留下无尽遐想。
- ###剧本亮点
- 亮点1:绝境奇药,生死一线
- 画面开场即是阿雪苍白无力的脸庞,梁萧眼中泪光闪烁,急促的呼吸声与心跳声烘托紧张气氛。特写梁萧将“阴阳球”塞入阿雪舌底,球体散发微光,阿雪气息从若有若无到逐渐平稳,生死逆转的瞬间,视觉与听觉冲击力十足。
- 亮点2:结拜兄妹,化解尴尬
- 阿雪因解衣拔刺而羞愤欲绝的特写,梁萧眼神中的挣扎与无奈。在紧迫的背景音下,梁萧急中生智,牵起阿雪颤抖的手,跪地匆匆结拜,阿雪眼神从惊惶到迷茫,最终一句“我……我愿意的”带着哭腔,将救命与羞耻的矛盾推向高潮,又以出人意料的方式解决。
- 亮点3:纤发夺命,情愫暗涌
- 拔刺过程的紧张特写,梁萧指尖小心翼翼地捏住细丝,缓慢抽出。当“问心刺”被完全拔出,特写它竟是一根细长、泛着寒光的女子秀发,诡异而令人震惊。阿雪苏醒后,两人眼神交汇,阿雪低头羞涩,梁萧眼中复杂的情绪,无声地传递出超越兄妹的微妙情愫。
- ###人物关系
- 阿雪与梁萧之间存在兄妹之情。
- ###核心矛盾
- 阿雪与梁萧之间存在兄妹之情,但因情愫暗生而产生微妙的矛盾。阿雪羞涩难当,梁萧情急之下提出结拜兄妹,以化解尴尬。两人匆匆结拜后,梁萧小心翼翼地拔出“问心刺”,竟发现它是一根诡异的女子秀发。阿雪转危为安,两人之间,在生死与羞涩交织的瞬间,情愫暗生,却又被兄妹之名束缚,留下无尽遐想。
- ###主体列表
- 阿雪-病重:身中奇毒“问心刺”,气息奄奄,面色苍白,身体虚弱。
- 阿雪-羞涩:经“阴阳球”救治后逐渐恢复,脸颊泛起红晕,眼神娇羞,情愫暗生。
- 梁萧:少年侠士,心系阿雪安危,沉着冷静,内心挣扎于情义之间。
- 阴阳球:天地异宝,能化生精气,救人于危难。
- 问心刺:一根诡异的女子秀发,细长泛寒光,是致命的奇毒。
- ###美术风格
- 基础画风风格词:厚涂古风
- 整体采用精致细腻的厚涂古风,色彩浓郁而富有层次感,注重光影对比,营造出古典而富有戏剧张力的氛围。色调以深沉的蓝绿、灰调为主,衬托阿雪病弱的苍白,同时用温暖的金色、琥珀色光晕突出“阴阳球”的生命力。拔刺与结拜场景则以柔和的暖色调为主,辅以人物脸颊的红晕,烘托羞涩与情愫暗生的微妙感。人物造型飘逸,服饰细节考究,背景虚实结合,既有古韵,又不失奇幻色彩。
- ###场景列表
- 林间疗伤地:幽静的古风林间空地,一棵高大古树下,是梁萧为阿雪疗伤的地点。环境清幽,光影斑驳,既有古韵又不失奇幻色彩。
- ###分集剧本
- ##分集01
- 第1集: 第一集的标题
- 场景描述:地点、时间、场景
- 运镜:固定近距离
- 出场角色:男主、女主
- 台词内容:
- 女主: 女主对话
- 男主:男主对话
- 女主: 女主对话
- 男主: 男主对话
- ##分集02
- 第2集: 第二集的标题
- 场景描述:地点、时间、场景
- 运镜:固定近距离
- 出场角色:男主、女主
- 台词内容:
- 女主: 女主对话
- 男主:男主对话
- 女主: 女主对话
- 男主: 男主对话"
- ];
- }
- /**
- * 通用文生文方法 - 支持多模型流式输出和图片输入
- *
- * @param array $data 请求参数
- * @return \Generator 返回生成器,用于流式输出
- */
- public function generateText($data) {
- // 获取请求参数
- $model = getProp($data, 'model', 'deepseek-v3-2-251201'); // 默认使用deepseek-v3
- $messages = getProp($data, 'messages', []);
- $systemPrompt = getProp($data, 'system_prompt', '');
- $prompt = getProp($data, 'prompt', '');
- $images = getProp($data, 'images', []); // 支持多张图片
- $imageUrls = getProp($data, 'image_urls', []); // 支持图片URL
-
- // 如果没有提供messages,则根据system_prompt、content和images构建
- if (empty($messages)) {
- $messages = [];
-
- // 添加系统提示词
- if (!empty($systemPrompt)) {
- $messages[] = [
- 'role' => 'system',
- 'content' => $systemPrompt
- ];
- }
-
- // 构建用户消息内容(支持文本+图片)
- if (!empty($prompt) || !empty($images) || !empty($imageUrls)) {
- $userMessage = [
- 'role' => 'user',
- 'content' => []
- ];
-
- // 添加文本内容
- if (!empty($prompt)) {
- $userMessage['content'][] = [
- 'type' => 'text',
- 'text' => $prompt
- ];
- }
-
- // 处理上传的图片文件
- if (!empty($images)) {
- if (!is_array($images)) {
- $images = [$images];
- }
-
- foreach ($images as $image) {
- $imageBase64 = $this->processImageToBase64($image);
- if ($imageBase64) {
- $userMessage['content'][] = [
- 'type' => 'image_url',
- 'image_url' => [
- 'url' => "data:image/jpeg;base64,{$imageBase64}"
- ]
- ];
- }
- }
- }
-
- // 处理图片URL
- if (!empty($imageUrls)) {
- if (!is_array($imageUrls)) {
- $imageUrls = [$imageUrls];
- }
-
- foreach ($imageUrls as $url) {
- if (!empty($url)) {
- $userMessage['content'][] = [
- 'type' => 'image_url',
- 'image_url' => [
- 'url' => $url
- ]
- ];
- }
- }
- }
-
- // 如果只有文本内容,简化为字符串格式
- if (count($userMessage['content']) === 1 && $userMessage['content'][0]['type'] === 'text') {
- $userMessage['content'] = $userMessage['content'][0]['text'];
- }
-
- $messages[] = $userMessage;
- }
- }
-
- // 验证messages不为空
- if (empty($messages)) {
- Utils::throwError('20003:请提供有效的对话内容');
- }
-
- // 模型兼容性处理:deepseek-chat 和 deepseek-reasoner 映射到 deepseek-v4-flash
- $thinkingMode = getProp($data, 'thinking', 'disabled'); // 默认非思考模式
- $reasoningEffort = getProp($data, 'reasoning_effort', 'high'); // 默认思考强度
-
- if ($model === 'deepseek-chat') {
- // deepseek-chat 映射到 deepseek-v4-flash,非思考模式
- $model = 'deepseek-v4-flash';
- $thinkingMode = 'disabled';
- } elseif ($model === 'deepseek-reasoner') {
- // deepseek-reasoner 映射到 deepseek-v4-flash,思考模式
- $model = 'deepseek-v4-flash';
- $thinkingMode = 'enabled';
- }
-
- // 构建请求参数
- $post_data = [
- 'model' => $model,
- 'messages' => $messages,
- // 'max_tokens' => getProp($data, 'max_tokens', 8192),
- 'temperature' => getProp($data, 'temperature', 1),
- 'frequency_penalty' => getProp($data, 'frequency_penalty', 0),
- 'presence_penalty' => getProp($data, 'presence_penalty', 0),
- 'thinking' => ['type' => $thinkingMode],
- 'stream' => true // 启用流式输出
- ];
- if ($thinkingMode == 'enabled') $post_data['reasoning_effort'] = $reasoningEffort;
-
- // 添加可选参数
- if (isset($data['top_p'])) {
- $post_data['top_p'] = $data['top_p'];
- }
-
- if (isset($data['response_format'])) {
- $post_data['response_format'] = $data['response_format'];
- }
-
- dLog('deepseek')->info('通用文生文请求参数', ['model' => $model, 'thinking' => $thinkingMode, 'messages_count' => count($messages)]);
-
- // 根据模型类型选择调用方法
- if (in_array($model, ['deepseek-reasoner', 'deepseek-chat', 'deepseek-v4-flash', 'deepseek-v4-pro'])) {
- // DeepSeek 官方模型使用 DeepSeek API
- return $this->deepSeekStreamResponse($post_data);
- } else if (in_array($model, $this->valid_text_models)) {
- // 火山引擎支持的模型使用火山引擎 API
- return $this->volcEngineChatCompletion($post_data);
- } else {
- Utils::throwError('20003:不支持的模型: ' . $model);
- }
- }
-
- /**
- * 处理图片转换为base64
- *
- * @param mixed $image 图片文件对象或文件路径
- * @return string|null base64编码的图片数据
- */
- private function processImageToBase64($image) {
- try {
- // 获取文件路径
- if (is_string($image)) {
- $filePath = $image;
- } else {
- // Laravel UploadedFile 对象
- $filePath = $image->getRealPath();
- }
-
- if (!file_exists($filePath)) {
- dLog('deepseek')->warning('图片文件不存在', ['path' => $filePath]);
- return null;
- }
-
- // 验证是否为图片文件
- $imageInfo = @getimagesize($filePath);
- if ($imageInfo === false) {
- dLog('deepseek')->warning('无效的图片文件', ['path' => $filePath]);
- return null;
- }
-
- // 读取图片并转换为base64
- $imageData = file_get_contents($filePath);
- $base64Image = base64_encode($imageData);
-
- dLog('deepseek')->info('图片转换成功', [
- 'size' => strlen($imageData),
- 'type' => $imageInfo['mime']
- ]);
-
- return $base64Image;
-
- } catch (\Exception $e) {
- dLog('deepseek')->error('图片处理失败: ' . $e->getMessage());
- return null;
- }
- }
- private function getArtStylePromptMappings(): array
- {
- return [
- '日系动漫风格' => [
- 'aliases' => ['日系动漫风格', '日系动漫', '日系', '动漫', '日漫'],
- 'prompt' => "日系动漫风格,线条干净流畅,色彩明亮柔和,人物精致,光影细腻,画面统一稳定"
- ],
- '国漫风格' => [
- 'aliases' => ['国漫风格', '国漫'],
- 'prompt' => "国漫风格,线条硬朗,人物成熟大气,色彩浓郁,光影对比强烈,整体画风统一,适合漫剧叙事"
- ],
- 'Q版卡通风格' => [
- 'aliases' => ['Q版卡通风格', 'Q版卡通', 'Q版', '卡通'],
- 'prompt' => "Q版卡通风格,头大身小造型可爱,线条简洁,色彩明快,无复杂阴影,整体萌系干净统一"
- ],
- '简约扁平风格' => [
- 'aliases' => ['简约扁平风格', '简约扁平', '扁平', '简约'],
- 'prompt' => "简约扁平风格,大块纯色无渐变,线条简洁现代,画面干净清爽,不出现复杂纹理与光影"
- ],
- '古风仙侠风格' => [
- 'aliases' => ['古风仙侠', '古风', '仙侠'],
- 'prompt' => "古风仙侠风格,汉服飘逸,云雾缭绕仙气空灵,色调清雅,光影柔和,国风氛围统一"
- ],
- '武侠风格' => [
- 'aliases' => ['武侠风格', '武侠'],
- 'prompt' => "武侠风格,中式古装,线条干练硬朗,江湖氛围浓厚,动态自然,色调沉稳大气"
- ],
- '新中式水墨风格' => [
- 'aliases' => ['新中式水墨风格', '新中式', '水墨'],
- 'prompt' => "新中式水墨风格,淡墨渲染笔触自然,留白有意境,色调清雅,国风质感统一不杂乱"
- ],
- '写实插画风格' => [
- 'aliases' => ['写实插画风格', '写实插画', '写实', '插画'],
- 'prompt' => "写实插画风格,真实人体比例,光影自然细腻,画面质感真实,不卡通不崩坏"
- ],
- '3D卡通风格' => [
- 'aliases' => ['3D卡通风格', '3D卡通'],
- 'prompt' => "3D卡通风格,立体建模质感柔和,造型圆润光滑,光影干净,整体风格统一不穿模"
- ],
- '条漫风格' => [
- 'aliases' => ['条漫风格', '条漫'],
- 'prompt' => "条漫风格,纵向分镜简洁,色彩清淡,适合上下滑动阅读,画面轻量化且整体统一"
- ],
- '赛博朋克风格' => [
- 'aliases' => ['赛博朋克风格', '赛博朋克'],
- 'prompt' => "赛博朋克风格,霓虹灯光未来都市,暗色调科技感强,高对比光影,风格统一强烈"
- ],
- '暗黑悬疑风格' => [
- 'aliases' => ['暗黑悬疑风格', '暗黑悬疑'],
- 'prompt' => "暗黑悬疑风格,低饱和冷色调,强阴影光线昏暗,氛围神秘压抑,整体色调统一"
- ],
- '治愈清新风格' => [
- 'aliases' => ['治愈清新风格', '治愈清新'],
- 'prompt' => "治愈清新风格,马卡龙柔和色系,柔光暖调,画面干净舒适,氛围温暖不杂乱"
- ],
- ];
- }
- public function getArtStylePromptByInput($inputArtStyle): string
- {
- $normalizedInput = $this->normalizeArtStyleInput($inputArtStyle);
- if ($normalizedInput === '') {
- return '';
- }
- foreach ($this->getArtStylePromptMappings() as $mapping) {
- foreach ($mapping['aliases'] as $alias) {
- $normalizedAlias = $this->normalizeArtStyleInput($alias);
- if ($normalizedAlias === '') {
- continue;
- }
- if (strpos($normalizedInput, $normalizedAlias) !== false || strpos($normalizedAlias, $normalizedInput) !== false) {
- return $mapping['prompt'];
- }
- }
- }
- return '';
- }
- private function normalizeArtStyleInput($value): string
- {
- $value = trim((string)$value);
- if ($value === '') {
- return '';
- }
- $value = str_replace(["\r", "\n", "\t", ' ', ' ', '-', '_', ':', ':', ',', ',', '。', '.'], '', $value);
- return mb_strtolower($value, 'UTF-8');
- }
- private function replaceArtStyleSection(string $content, string $artStyle): string
- {
- if ($content === '' || $artStyle === '') {
- return $content;
- }
- $pattern = '/###美术风格\s*\R(.*?)(?=\R###|$)/su';
- if (!preg_match($pattern, $content)) {
- return $content;
- }
- return preg_replace($pattern, "###美术风格\n" . trim($artStyle), $content, 1);
- }
- /**
- * DeepSeek流式输出处理方法
- *
- * @param array $post_data DeepSeek API请求参数
- * @return \Generator 返回生成器,用于流式输出
- */
- private function deepSeekStreamResponse($post_data) {
- $client = new Client(['timeout' => 1200, 'verify' => false]);
- $response = $client->post($this->url, [
- 'json' => $post_data,
- 'headers' => $this->headers,
- 'stream' => true // 启用流式响应
- ]);
-
- $body = $response->getBody();
- $fullContent = '';
- $fullReasoningContent = '';
- $usage = [];
- $buffer = '';
- dLog('deepseek')->info('开始读取 DeepSeek 流式响应');
- // 逐行读取流式响应
- while (!$body->eof()) {
- $chunk = $body->read(1024); // 每次读取 1KB
- $buffer .= $chunk;
-
- // 按行分割
- $lines = explode("\n", $buffer);
- // 保留最后一个不完整的行
- $buffer = array_pop($lines);
-
- foreach ($lines as $line) {
- $line = trim($line);
-
- // 跳过空行
- if (empty($line)) {
- continue;
- }
-
- // 检查是否是 SSE 数据行
- if (strpos($line, 'data: ') !== 0) {
- dLog('deepseek')->warning('非 SSE 数据行', ['line' => $line]);
- continue;
- }
- $data = substr($line, 6); // 移除 "data: " 前缀
-
- if ($data === '[DONE]') {
- dLog('deepseek')->info('收到结束标记');
- break 2; // 跳出两层循环
- }
- try {
- $json = json_decode($data, true);
-
- if (json_last_error() !== JSON_ERROR_NONE) {
- dLog('deepseek')->warning('JSON 解析错误: ' . json_last_error_msg(), ['data' => $data]);
- continue;
- }
-
- if (isset($json['choices'][0]['delta'])) {
- $delta = $json['choices'][0]['delta'];
-
- // 处理思维链内容(仅 deepseek-reasoner 模型)
- if (isset($delta['reasoning_content'])) {
- $fullReasoningContent .= $delta['reasoning_content'];
- yield [
- 'type' => 'reasoning',
- 'content' => $delta['reasoning_content'],
- 'full_reasoning' => $fullReasoningContent
- ];
- }
-
- // 处理最终回答内容
- if (isset($delta['content'])) {
- $fullContent .= $delta['content'];
- yield [
- 'type' => 'content',
- 'content' => $delta['content'],
- ];
- }
- }
-
- // 获取使用统计信息
- if (isset($json['usage'])) {
- $usage = $json['usage'];
- dLog('deepseek')->info('收到使用统计', ['usage' => $usage]);
- }
-
- } catch (\Exception $e) {
- dLog('deepseek')->error('解析流式响应失败: ' . $e->getMessage(), ['line' => $line]);
- continue;
- }
- }
- }
- dLog('deepseek')->info('流式读取完成', [
- 'content_length' => strlen($fullContent),
- 'reasoning_length' => strlen($fullReasoningContent)
- ]);
- yield [
- 'type' => 'done',
- 'full_content' => $fullContent,
- 'full_reasoning' => $fullReasoningContent,
- 'usage' => $usage
- ];
- }
- // 与推理模型对话
- public function chatWithReasoner($data) {
- $content = getProp($data, 'content');
- $model = getProp($data, 'model', 'r1');
-
- // 模型兼容性处理:deepseek-chat 和 deepseek-reasoner 映射到 deepseek-v4-flash
- $thinkingMode = getProp($data, 'thinking', 'disabled'); // 默认非思考模式
- $reasoningEffort = getProp($data, 'reasoning_effort', 'high'); // 默认思考强度
- if ($model == 'r1') {
- $model = 'deepseek-v4-flash'; // r1 映射到 deepseek-v4-flash 思考模式
- $thinkingMode = 'disabled';
- } else {
- $model = 'deepseek-v4-flash'; // 默认映射到 deepseek-v4-flash 非思考模式
- $thinkingMode = 'disabled';
- }
- // 获取可选情感(根据音色可支持情感选)
- $timbre_emotion = DB::table('mp_timbres')->where('is_enabled', 1)->pluck('emotion')->toArray();
- $emotion_list = [];
- foreach ($timbre_emotion as $emotion) {
- $tmp = explode(',', $emotion);
- $emotion_list = array_merge($emotion_list, $tmp);
- }
- $emotion_list = array_unique($emotion_list);
- $emotion_str = implode('、', $emotion_list);
- // // 获取可选情感
- // $emotion_list = DB::table('mp_emotion_list')->where('is_enabled', 1)->select('emotion_name')->orderBy('id')->get()->pluck('emotion_name')->toArray();
- // $emotion_str = implode('、', $emotion_list);
- // 是否启用情感
- $enable_emotion = getProp($data, 'enable_emotion', 0);
- if ($enable_emotion) {
- $sys_content = "下面有一段小说文本,请帮我将文本中的每句话按从上到下的顺序拆分成角色不同的剧本文稿(不得更改上下文顺序和内容),文稿形式严格按照“角色名(男、女、中性):台词{情感}”输出,需要注意以下几点要求:
- 1.角色名后不要加入任何其他词语,只能加性别,在男、女或中性中选
- 2.非对话部分请全部用旁白角色代替,旁白的情感统一选择中性
- 3.情感必须在【{$emotion_str}】中选一个,不得使用其他词语";
- }else {
- $sys_content = "下面有一段小说文本,请帮我将文本中的每句话按从上到下的顺序拆分成角色不同的剧本文稿(不得更改上下文顺序和内容),文稿形式严格按照“角色名(男、女、中性):台词”输出,需要注意以下几点要求:
- 1.角色名后不要加入任何其他词语,只能加性别,在男、女或中性中选
- 2.非对话部分请全部用旁白角色代替,旁白的情感统一选择中性";
- }
-
- $messages = [
- [
- 'role' => 'system',
- 'content' => $sys_content
- ],
- [
- 'role' => 'user',
- 'content' => $content
- ]
- ];
- $post_data = [
- 'model' => $model,
- 'messages' => $messages,
- // 'max_tokens' => 8192,
- 'temperature' => 1,
- 'frequency_penalty' => 0,
- 'presence_penalty' => 0,
- 'thinking' => ['type' => $thinkingMode],
- 'response_format' => [
- 'type' => 'text'
- ],
- 'stream' => false
- ];
- if ($thinkingMode == 'enabled') $post_data['reasoning_effort'] = $reasoningEffort;
- // 根据模型类型选择调用方法
- $fullContent = '';
- $usage = [];
-
- if (in_array($model, ['deepseek-reasoner', 'deepseek-chat', 'deepseek-v4-flash', 'deepseek-v4-pro'])) {
- // DeepSeek 官方模型使用 DeepSeek API
- $chatResult = $this->chatOnly($post_data);
- } else {
- // 其他模型使用火山引擎API
- $chatResult = $this->volcEngineChatCompletion($post_data);
- }
-
- if (is_array($chatResult)) {
- $fullContent = $chatResult['fullContent'];
- $usage = $chatResult['usage'];
- }
- // 处理获取到的剧本数据
- $script_content = handleScriptWords($fullContent, $enable_emotion);
- $result = [
- 'origin_content' => $fullContent,
- 'roles' => getProp($script_content, 'roles'),
- 'words' => getProp($script_content, 'words'),
- ];
- return $result;
- }
- public function resetParagraphAudio($data) {
- $bid = getProp($data, 'bid');
- $cid = getProp($data, 'cid');
- $version_id = getProp($data, 'version_id');
-
- if (!DB::table('mp_chapter_paragraph_audios')->where('bid', $bid)->where('cid', $cid)->where('version_id', $version_id)->value('id')) return true;
- return DB::table('mp_chapter_paragraph_audios')->where('bid', $bid)->where('cid', $cid)->where('version_id', $version_id)->update([
- 'generate_status' => '待制作',
- 'updated_at' => date('Y-m-d H:i:s')
- ]);
- }
- public function saveParagraphAudio($data) {
- $bid = getProp($data, 'bid');
- $cid = getProp($data, 'cid');
- $version_id = getProp($data, 'version_id');
- $sequence = getProp($data, 'sequence');
- $id = DB::table('mp_chapter_paragraph_audios')->where('bid', $bid)->where('cid', $cid)->where('version_id', $version_id)->where('sequence', $sequence)->value('id');
- // 获取所有情感
- $emotion_list = DB::table('mp_emotion_list')->where('is_enabled', 1)->pluck('emotion_name', 'emotion_code')->toArray();
- $emotion_list = array_flip($emotion_list);
- // 获取音色支持情感
- $timbre_emotion = DB::table('mp_timbres')->where('timbre_type', getProp($data, 'voice_type'))->value('emotion');
- $timbre_emotion = explode(',', $timbre_emotion);
- $emotion = getProp($data, 'emotion');
- if (!in_array($emotion, $timbre_emotion)) $emotion = '中性';
- $emotion_type = isset($emotion_list[$emotion]) ? $emotion_list[$emotion] : 'neutral';
- $list = [
- 'bid' => $bid,
- 'cid' => $cid,
- 'version_id' => $version_id,
- 'sequence' => $sequence,
- 'role' => getProp($data, 'role'),
- 'gender' => getProp($data, 'gender'),
- 'text' => trim(getProp($data, 'text')),
- 'emotion' => $emotion,
- 'emotion_type' => $emotion_type,
- 'voice_type' => getProp($data, 'voice_type'),
- 'voice_name' => getProp($data, 'voice_name'),
- 'speed_ratio' => getProp($data, 'speed_ratio', 0),
- 'loudness_ratio'=> getProp($data, 'loudness_ratio', 0),
- 'emotion_scale' => getProp($data, 'emotion_scale', 4),
- 'pitch' => getProp($data, 'pitch', 0),
- // 'paragraph_audio_url' => '',
- 'generate_status' => '制作中',
- 'error_msg' => '',
- 'updated_at' => date('Y-m-d H:i:s')
- ];
- $continue = false;
- // 判断是否含有中文、数字或英文,如果没有则跳过合成
- if (!preg_match('/[\x{4e00}-\x{9fa5}]+/u', $list['text']) && !preg_match('/[0-9]+/', $list['text']) && !preg_match('/[a-zA-Z]+/', $list['text'])) {
- $list['generate_status'] = '制作成功';
- $list['paragraph_audio_url'] = 'https://zw-audiobook.tos-cn-beijing.volces.com/effects/ellipses_2s.mp3';
- $list['subtitle_info'] = '{"duration": 2.0, "words": []}';
- $continue = true;
- }
- // if (getProp($data, 'paragraph_audio_url')) $list['paragraph_audio_url'] = getProp($data, 'paragraph_audio_url');
- if ($id) {
- $boolen = DB::table('mp_chapter_paragraph_audios')->where('id', $id)->update($list);
- }else {
- $list['created_at'] = date('Y-m-d H:i:s');
- $id = DB::table('mp_chapter_paragraph_audios')->insertGetId($list);
- $boolen = $id ? true : false;
- }
- // 如果更新成功则加入查询队列
- if ($boolen) {
- $redis_key = "select-{$bid}-{$version_id}-{$cid}";
- Redis::sadd($redis_key, $id);
- }
- if ($boolen && !$continue) {
- $boolen = false;
- $client = new Client(['timeout' => 300, 'verify' => false]);
- // 根据ID通过API通知合成音频
- // $result = $client->get("http://47.240.171.155:5000/api/previewTask?taskId={$id}");
- $result = $client->get("http://122.9.129.83:5000/api/previewTask?taskId={$id}");
- $response = $result->getBody()->getContents();
- $response_arr = json_decode($response, true);
- if (!isset($response_arr['code']) || (int)$response_arr['code'] !== 0) {
- $error_msg = isset($response_arr['msg']) ? $response_arr['msg'] : '未知错误';
- Log::info('通知火山生成段落音频失败: '.$error_msg);
- Utils::throwError('20003:通知火山生成段落音频失败');
- }
- $boolen = true;
- }
- return $boolen;
- }
- public function insertAudioEffect($data) {
- $audio_id = getProp($data, 'audio_id');
- $chapter_audio = DB::table('mp_chapter_audios')->where('id', $audio_id)->first();
- $bid = getProp($chapter_audio, 'bid');
- $version_id = getProp($chapter_audio, 'version_id');
- $cid = getProp($chapter_audio, 'cid');
- if (getProp($chapter_audio, 'generate_status') != '制作成功') Utils::throwError('20003:请先完成有声制作');
- $audio_effect_json = getProp($data, 'audio_effect_json');
- if (!is_string($audio_effect_json)) $audio_effect_json = json_encode($audio_effect_json, 256);
- try {
- DB::beginTransaction();
-
- $count = DB::table('mp_audio_tasks')->where('audio_id', $audio_id)->count('id');
- if (!$count) {
- $task_name = getProp($chapter_audio, 'book_name').' '.getProp($chapter_audio, 'chapter_name').'【'.getProp($chapter_audio, 'version_name').'】';
- }else {
- $task_name = getProp($chapter_audio, 'book_name').' '.getProp($chapter_audio, 'chapter_name').'【'.getProp($chapter_audio, 'version_name').'】('.($count+1).')';
- }
- $boolen1 = DB::table('mp_chapter_audios')->where('id', $audio_id)->update([
- 'audio_effect_status' => '添加中',
- 'audio_effect_json' => $audio_effect_json,
- 'updated_at' => date('Y-m-d H:i:s')
- ]);
- if (!$boolen1) {
- DB::rollBack();
- Utils::throwError('20003:更新生成数据失败');
- }
- $id = DB::table('mp_audio_tasks')->insertGetId([
- 'audio_id' => $audio_id,
- 'generate_status' => '执行中',
- 'generate_json' => json_encode([], 256),
- 'bid' => $bid,
- 'book_name' => getProp($chapter_audio, 'book_name'),
- 'version_id' => $version_id,
- 'version_name' => getProp($chapter_audio, 'version_name'),
- 'cid' => $cid,
- 'chapter_name' => getProp($chapter_audio, 'chapter_name'),
- 'task_name' => $task_name,
- 'created_at' => date('Y-m-d H:i:s'),
- 'updated_at' => date('Y-m-d H:i:s')
- ]);
-
- if (!$id) {
- DB::rollBack();
- Utils::throwError('20003:创建任务失败');
- }
- }catch (\Exception $e) {
- DB::rollBack();
- Utils::throwError('20003:'.$e->getMessage());
- }
- DB::commit();
-
- $client = new Client(['timeout' => 300, 'verify' => false]);
- // 根据ID通过API通知合成音频
- // $result = $client->get("http://47.240.171.155:5000/api/postTask?taskId={$id}");
- $result = $client->get("http://122.9.129.83:5000/api/postTask?taskId={$id}");
- $response = $result->getBody()->getContents();
- $response_arr = json_decode($response, true);
- if (!isset($response_arr['code']) || (int)$response_arr['code'] !== 0) {
- $error_msg = isset($response_arr['msg']) ? $response_arr['msg'] : '未知错误';
- Log::info('通知火山插入音效失败: '.$error_msg);
- Utils::throwError('20003:通知火山插入音效失败');
- }
-
- return true;
- }
- public function insertBgm($data) {
- $audio_id = getProp($data, 'audio_id');
- $chapter_audio = DB::table('mp_chapter_audios')->where('id', $audio_id)->first();
- $bid = getProp($chapter_audio, 'bid');
- $version_id = getProp($chapter_audio, 'version_id');
- $cid = getProp($chapter_audio, 'cid');
- if (getProp($chapter_audio, 'generate_status') != '制作成功') Utils::throwError('20003:请先完成有声制作');
- $bgm_json = getProp($data, 'bgm_json');
- if (!is_string($bgm_json)) $bgm_json = json_encode($bgm_json, 256);
- if (!$bgm_json) {
- $bgm_json = getProp($data, 'audio_effect_json');
- if (!is_string($bgm_json)) $bgm_json = json_encode($bgm_json, 256);
- }
- try {
- DB::beginTransaction();
-
- $count = DB::table('mp_audio_tasks')->where('audio_id', $audio_id)->count('id');
- if (!$count) {
- $task_name = getProp($chapter_audio, 'book_name').' '.getProp($chapter_audio, 'chapter_name').'【'.getProp($chapter_audio, 'version_name').'】';
- }else {
- $task_name = getProp($chapter_audio, 'book_name').' '.getProp($chapter_audio, 'chapter_name').'【'.getProp($chapter_audio, 'version_name').'】('.($count+1).')';
- }
- $boolen1 = DB::table('mp_chapter_audios')->where('id', $audio_id)->update([
- 'bgm_status' => '添加中',
- 'bgm_json' => $bgm_json,
- 'updated_at' => date('Y-m-d H:i:s')
- ]);
- if (!$boolen1) {
- DB::rollBack();
- Utils::throwError('20003:更新生成数据失败');
- }
- $id = DB::table('mp_audio_tasks')->insertGetId([
- 'audio_id' => $audio_id,
- 'generate_status' => '执行中',
- 'generate_json' => json_encode([], 256),
- 'bid' => $bid,
- 'book_name' => getProp($chapter_audio, 'book_name'),
- 'version_id' => $version_id,
- 'version_name' => getProp($chapter_audio, 'version_name'),
- 'cid' => $cid,
- 'chapter_name' => getProp($chapter_audio, 'chapter_name'),
- 'task_name' => $task_name,
- 'created_at' => date('Y-m-d H:i:s'),
- 'updated_at' => date('Y-m-d H:i:s')
- ]);
-
- if (!$id) {
- DB::rollBack();
- Utils::throwError('20003:创建任务失败');
- }
- }catch (\Exception $e) {
- DB::rollBack();
- Utils::throwError('20003:'.$e->getMessage());
- }
- DB::commit();
-
- $client = new Client(['timeout' => 300, 'verify' => false]);
- // 根据ID通过API通知合成音频
- // $result = $client->get("http://47.240.171.155:5000/api/postTask?taskId={$id}");
- $result = $client->get("http://122.9.129.83:5000/api/postTask?taskId={$id}");
- $response = $result->getBody()->getContents();
- $response_arr = json_decode($response, true);
- if (!isset($response_arr['code']) || (int)$response_arr['code'] !== 0) {
- $error_msg = isset($response_arr['msg']) ? $response_arr['msg'] : '未知错误';
- Log::info('通知火山插入bgm失败: '.$error_msg);
- Utils::throwError('20003:通知火山插入bgm失败');
- }
-
- return true;
- }
- public function getParagraphAudios($data) {
- $bid = getProp($data, 'bid');
- $cid = getProp($data, 'cid');
- $version_id = getProp($data, 'version_id');
- $sequence = getProp($data, 'sequence');
- // 获取已生成的音频
- $query = DB::table('mp_chapter_paragraph_audios')->where('bid', $bid)->where('cid', $cid)->where('version_id', $version_id);
- if ($sequence) $query->where('sequence', $sequence);
- $paragraph_audios = $query->orderBy('sequence')->get();
- $result = [];
- foreach($paragraph_audios as $item) {
- $result[] = [
- 'sequence' => getProp($item, 'sequence'),
- 'role' => getProp($item, 'role'),
- 'gender' => getProp($item, 'gender'),
- 'text' => trim(getProp($item, 'text')),
- 'emotion' => getProp($item, 'emotion'),
- 'emotion_type' => getProp($item, 'emotion_type'),
- 'voice_type' => getProp($item, 'voice_type'),
- 'voice_name' => getProp($item, 'voice_name'),
- 'speed_ratio' => getProp($item, 'speed_ratio'),
- 'loudness_ratio' => getProp($item, 'loudness_ratio'),
- 'emotion_scale' => getProp($item, 'emotion_scale'),
- 'paragraph_audio_url' => getProp($item, 'paragraph_audio_url'),
- 'subtitle_info' => json_decode(getProp($item, 'subtitle_info'), true),
- ];
- }
- return $result;
- }
- // 新增合成任务
- public function addGenerateTask($data) {
- $bid = getProp($data, 'bid');
- $cid = getProp($data, 'cid');
- $version_id = getProp($data, 'version_id');
- $generate_json = getProp($data, 'generate_json');
- // 获取已生成的音频
- $paragraph_audios = DB::table('mp_chapter_paragraph_audios')->where('bid', $bid)->where('cid', $cid)->where('version_id', $version_id)->get();
- $paragraph_list = [];
- foreach($paragraph_audios as $item) {
- $paragraph_list[getProp($item, 'sequence')] = [
- 'role' => getProp($item, 'role'),
- 'gender' => getProp($item, 'gender'),
- 'text' => trim(getProp($item, 'text')),
- 'emotion' => getProp($item, 'emotion'),
- 'emotion_type' => getProp($item, 'emotion_type'),
- 'voice_type' => getProp($item, 'voice_type'),
- 'voice_name' => getProp($item, 'voice_name'),
- 'speed_ratio' => getProp($item, 'speed_ratio'),
- 'loudness_ratio' => getProp($item, 'loudness_ratio'),
- 'emotion_scale' => getProp($item, 'emotion_scale'),
- 'paragraph_audio_url' => getProp($item, 'paragraph_audio_url'),
- ];
- }
- // 更新角色-音色信息
- $existed_role_info = DB::table('mp_book_version')->where('bid', $bid)->where('id', $version_id)->value('role_info');
- $existed_role_info = json_decode($existed_role_info, true);
- if (!$existed_role_info) $existed_role_info = [];
- // 获取情感信息
- $emotion_list = DB::table('mp_emotion_list')->where('is_enabled', 1)->pluck('emotion_name', 'emotion_code')->toArray();
- $emotion_list = array_flip($emotion_list);
- // 获取音色对应情感组
- $timbre_emotions = DB::table('mp_timbres')->where('is_enabled', 1)->select('timbre_type', 'emotion')->get();
- $timbre_emotion_list = [];
- foreach($timbre_emotions as $item) {
- $timbre_emotion_list[getProp($item, 'timbre_type')] = explode(',', getProp($item, 'emotion'));
- }
- // 构造生成音频的json
- $words = json_decode($generate_json, true);
- // 最终合成前的参数组
- $mp_chapter_paragraph_audios = [];
- $sequence = 1;
- $complete_paragraph_sequences = [];
- foreach($words as &$word) {
- 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:参数格式有误');
- if (!($word['text']) || !($word['voice_type']) || !($word['voice_name'])) Utils::throwError('20003:参数不得为空');
- $role = getProp($word, 'role');
- $word['gender'] = (int)$word['gender'];
- // 判断音色对应情感是否支持,不支持则调整为中性
- $access_emotion = isset($timbre_emotion_list[$word['voice_type']]) ? $timbre_emotion_list[$word['voice_type']] : [];
- if (!in_array($word['emotion'], $access_emotion)) $word['emotion'] = '中性';
- // 如果有对应情感则赋值,没有则默认为中性(neutral)
- if (isset($emotion_list[getProp($word, 'emotion')])) {
- $word['emotion_type'] = $emotion_list[getProp($word, 'emotion')];
- }else {
- $word['emotion'] = '中性';
- $word['emotion_type'] = 'neutral';
- }
- $existed_role_info[$role] = [
- 'timbre_type' => $word['voice_type'],
- 'timbre_name' => $word['voice_name'],
- 'emotion' => $word['emotion'],
- 'emotion_type' => $word['emotion_type'],
- 'speed_ratio' => $word['speed_ratio'],
- 'loudness_ratio'=> $word['loudness_ratio'],
- 'emotion_scale' => $word['emotion_scale'],
- 'pitch' => $word['pitch']
- ];
- $word['paragraph_audio_url'] = '';
- // 判断生成参数是否相同,相同则直接使用已生成的音频
- $paragraph = isset($paragraph_list[$sequence]) ? $paragraph_list[$sequence] : [];
- // 如果音频存在并且参数都未改变则使用已生成的音频
- if (getProp($paragraph, 'paragraph_audio_url')) {
- // if (getProp($paragraph, 'role') == getProp($word, 'role') && getProp($paragraph, 'text') == getProp($word, 'text') &&
- // getProp($paragraph, 'voice_type') == getProp($word, 'voice_type') &&
- // getProp($paragraph, 'speed_ratio') == getProp($word, 'speed_ratio') && getProp($paragraph, 'loudness_ratio') == getProp($word, 'loudness_ratio') &&
- // getProp($paragraph, 'emotion_scale') == getProp($word, 'emotion_scale'))
- // {
- // $word['paragraph_audio_url'] = getProp($paragraph, 'paragraph_audio_url');
- // }
- $word['paragraph_audio_url'] = getProp($paragraph, 'paragraph_audio_url');
- }
- $tmp = $word;
- // 组装章节分句音频数据
- // $tmp['sequence'] = getProp($word, 'sequence');
- // $tmp['text'] = getProp($word, 'text');
- // $tmp['emotion'] = getProp($word, 'emotion');
- // $tmp['emotion_type'] = getProp($word, 'emotion_type');
- // $tmp['voice_name'] = getProp($word, 'voice_name');
- // $tmp['voice_type'] = getProp($word, 'voice_type');
- // $tmp['speed_ratio'] = getProp($word, 'speed_ratio');
- // $tmp['loudness_ratio'] = getProp($word, 'loudness_ratio');
- // $tmp['emotion_scale'] = getProp($word, 'emotion_scale');
- $tmp['bid'] = $bid;
- $tmp['version_id'] = $version_id;
- $tmp['cid'] = $cid;
- $tmp['sequence'] = $sequence;
- $tmp['created_at'] = date('Y-m-d H:i:s');
- $tmp['updated_at'] = date('Y-m-d H:i:s');
- if (!getProp($tmp, 'paragraph_audio_url')) Utils::throwError('20003:段落未生成音频,请等待完成后提交');
- $mp_chapter_paragraph_audios[] = $tmp;
- $complete_paragraph_sequences[] = $sequence;
- $sequence++;
- }
- $generate_json = json_encode($words, 256);
- // 判断是否有未生成字幕的段落
- $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();
- if ($no_subtitle_sequences) {
- Utils::throwError('20003:请先将以下段落序号生成字幕('.implode('、', $no_subtitle_sequences).')');
- }
- try {
- DB::beginTransaction();
- $role_info = json_encode($existed_role_info, 256);
- $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')]);
- if (!$boolen) {
- DB::rollBack();
- Utils::throwError('20003:更新角色信息失败');
- }
- $count = DB::table('mp_audio_tasks')->where('bid', $bid)->where('version_id', $version_id)->where('cid', $cid)->count('id');
- $chapter_audio = DB::table('mp_chapter_audios')->where('bid', $bid)->where('version_id', $version_id)->where('cid', $cid)->first();
- if (!$count) {
- $task_name = getProp($chapter_audio, 'book_name').' '.getProp($chapter_audio, 'chapter_name').'【'.getProp($chapter_audio, 'version_name').'】';
- }else {
- $task_name = getProp($chapter_audio, 'book_name').' '.getProp($chapter_audio, 'chapter_name').'【'.getProp($chapter_audio, 'version_name').'】('.($count+1).')';
- }
- $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')]);
- if (!$boolen1) {
- DB::rollBack();
- Utils::throwError('20003:更新生成数据失败');
- }
- $id = DB::table('mp_audio_tasks')->insertGetId([
- 'audio_id' => getProp($chapter_audio, 'id'),
- 'generate_status' => '执行中',
- 'generate_json' => $generate_json,
- 'bid' => $bid,
- 'book_name' => getProp($chapter_audio, 'book_name'),
- 'version_id' => $version_id,
- 'version_name' => getProp($chapter_audio, 'version_name'),
- 'cid' => $cid,
- 'chapter_name' => getProp($chapter_audio, 'chapter_name'),
- 'task_name' => $task_name,
- 'created_at' => date('Y-m-d H:i:s'),
- 'updated_at' => date('Y-m-d H:i:s')
- ]);
-
- if (!$id) {
- DB::rollBack();
- Utils::throwError('20003:创建任务失败');
- }
- // 删除不在段落序号范围内的其他数据
- if ($complete_paragraph_sequences) {
- DB::table('mp_chapter_paragraph_audios')->where('bid', $bid)->where('version_id', $version_id)->where('cid', $cid)->whereNotIn('sequence', $complete_paragraph_sequences)->delete();
- }
- // // 删除章节分句音频数据并重新插入
- // DB::table('mp_chapter_paragraph_audios')->where('bid', $bid)->where('version_id', $version_id)->where('cid', $cid)->delete();
- // $boolen3 = DB::table('mp_chapter_paragraph_audios')->insert($mp_chapter_paragraph_audios);
- // if (!$boolen3) {
- // DB::rollBack();
- // Utils::throwError('20003:更新章节分句音频失败');
- // }
- } catch (\Exception $e) {
- DB::rollBack();
- Utils::throwError('20003:'.$e->getMessage());
- }
- DB::commit();
- // 通知火山生成音频
- $client = new Client(['timeout' => 300, 'verify' => false]);
- // 根据ID通过API通知合成音频
- // $result = $client->get("http://47.240.171.155:5000/api/chapterTask?taskId={$id}");
- $result = $client->get("http://122.9.129.83:5000/api/chapterTask?taskId={$id}");
- $response = $result->getBody()->getContents();
- $response_arr = json_decode($response, true);
- if (!isset($response_arr['code']) || (int)$response_arr['code'] !== 0) {
- $error_msg = isset($response_arr['msg']) ? $response_arr['msg'] : '未知错误';
- Log::info('通知火山生成音频失败: '.$error_msg);
- Utils::throwError('20003:通知火山生成音频失败');
- }
- return true;
- }
- public function timbreList($data) {
- $gender = getProp($data, 'gender');
- $timbre_name = getProp($data, 'voice_name');
- $category_id = getProp($data, 'category_id');
-
- $query = DB::table('mp_timbres')->where('is_enabled', 1)->select('timbre_name as voice_name', 'timbre_type as voice_type', 'gender', 'language', 'emotion', 'label', 'first_category_id', 'first_category_name', 'second_category_id', 'second_category_name', 'third_category_id', 'third_category_name', 'audio_url');
- if ($gender) {
- $query->where('gender', $gender);
- }
- if ($timbre_name) {
- $query->where('timbre_name', 'like', "%{$timbre_name}%");
- }
- if ($category_id) {
- $query->where(function ($query) use ($category_id) {
- $query->where('first_category_id', $category_id)->orWhere('second_category_id', $category_id)->orWhere('third_category_id', $category_id);
- });
- }
- $list = $query->get()->map(function ($value) {
- $value = (array)$value;
- $value['voice_name'] = str_replace('(多情感)', '', $value['voice_name']);
- return $value;
- })->toArray();
- return $list;
- }
- // 生成火山临时token
- public function setStsToken() {
- // ************* 配置参数 *************
- $method = 'GET';
- $service = 'sts';
- $host = 'open.volcengineapi.com';
- $region = env('VOLC_REGION');
- $endpoint = 'https://open.volcengineapi.com';
- // $endpoint = 'https://tos-cn-beijing.volces.com';
- $access_key = env('VOLC_AK');
- $secret_key = env('VOLC_SK');
- // 获取缓存中的token,如果没有则请求接口
- $token = Redis::get('volc_sts_token');
- if (!$token) {
- // 查询参数
- $query_parameters = [
- 'Action' => 'AssumeRole',
- 'RoleSessionName' => 'user@zw',
- 'RoleTrn' => 'trn:iam::2102575520:role/tos_role',
- 'Version' => '2018-01-01'
- ];
- // 生成URL编码的查询字符串
- $request_parameters = http_build_query($query_parameters, '', '&', PHP_QUERY_RFC3986);
- // 获取签名头信息
- $headers = $this->getSignHeaders($method, $service, $host, $region, $request_parameters, $access_key, $secret_key);
- // 构建完整URL
- $request_url = $endpoint . '?' . $request_parameters;
- $client = new Client(['verify' => false]);
- $response = $client->get($request_url, ['headers' => $headers]);
- $response_arr = json_decode($response->getBody()->getContents(), true);
- $result = [
- 'SessionToken' => isset($response_arr['Result']['Credentials']['SessionToken']) ? $response_arr['Result']['Credentials']['SessionToken'] : '',
- 'AccessKeyId' => isset($response_arr['Result']['Credentials']['AccessKeyId']) ? $response_arr['Result']['Credentials']['AccessKeyId'] : '',
- 'SecretAccessKey' => isset($response_arr['Result']['Credentials']['SecretAccessKey']) ? $response_arr['Result']['Credentials']['SecretAccessKey'] : '',
- 'Region' => env('VOLC_REGION'),
- 'Endpoint' => env('VOLC_END_POINT'),
- 'Bucket' => env('VOLC_BUCKET'),
- ];
- // 缓存token
- Redis::setex('volc_sts_token', 3000, json_encode($result));
- return $result;
- } else {
- return json_decode($token, true);
- }
-
- // $response = $response['Response'];
- // $access_key = $response['Credentials']['AccessKeyId'];
- // $secret_key = $response['Credentials']['AccessKeySecret'];
- // $security_token = $response['Credentials']['SecurityToken'];
- // $expiration = $response['Credentials']['Expiration'];
- // dd($response_arr);
- }
- public function emotionGroups($data) {
- $id = getProp($data, 'group_id');
- $group_name = getProp($data, 'group_name');
- $voice_type = getProp($data, 'voice_type');
- $query = DB::table('mp_emotion_groups')->where('is_enabled', 1)->select('id as group_id', 'group_name',
- 'emotion', 'emotion_type', 'voice_name', 'voice_type', 'speed_ratio', 'loudness_ratio', 'emotion_scale',
- 'pitch', 'generate_status', 'audio_url', 'error_msg');
- if ($group_name) {
- $query->where('group_name', 'like', "%{$group_name}%");
- }
- if ($id) {
- $query->where('id', $id);
- }
- if ($voice_type) {
- $query->where('voice_type', $voice_type);
- }
- return $query->orderBy('id')->get()->map(function ($value) {
- return (array)$value;
- })->toArray();
- }
- private function sign($key, $msg) {
- return hash_hmac('sha256', $msg, $key, true);
- }
-
- // 生成签名密钥
- private function getSignatureKey($key, $dateStamp, $regionName, $serviceName) {
- $kDate = $this->sign($key, $dateStamp);
- $kRegion = $this->sign($kDate, $regionName);
- $kService = $this->sign($kRegion, $serviceName);
- $kSigning = $this->sign($kService, 'request');
- return $kSigning;
- }
- // 获取签名头信息
- private function getSignHeaders($method, $service, $host, $region, $request_parameters, $access_key, $secret_key) {
- $contenttype = 'application/x-www-form-urlencoded';
- $accept = 'application/json';
-
- // 获取当前UTC时间
- $t = new DateTime('now', new DateTimeZone('UTC'));
- $xdate = $t->format('Ymd\THis\Z');
- $datestamp = $t->format('Ymd');
-
- // 1. 拼接规范请求串
- $canonical_uri = '/';
- $canonical_querystring = $request_parameters;
- $canonical_headers = "content-type:{$contenttype}\nhost:{$host}\nx-date:{$xdate}\n";
- $signed_headers = 'content-type;host;x-date';
-
- // 空请求体的SHA256哈希
- $payload_hash = hash('sha256', '');
-
- $canonical_request = implode("\n", [
- $method,
- $canonical_uri,
- $canonical_querystring,
- $canonical_headers,
- $signed_headers,
- $payload_hash
- ]);
-
- // 2. 拼接待签名字符串
- $algorithm = 'HMAC-SHA256';
- $credential_scope = implode('/', [$datestamp, $region, $service, 'request']);
- $hashed_canonical_request = hash('sha256', $canonical_request);
-
- $string_to_sign = implode("\n", [
- $algorithm,
- $xdate,
- $credential_scope,
- $hashed_canonical_request
- ]);
-
- // 3. 计算签名
- $signing_key = $this->getSignatureKey($secret_key, $datestamp, $region, $service);
- $signature = hash_hmac('sha256', $string_to_sign, $signing_key);
-
- // 4. 添加签名到请求头
- $authorization_header = sprintf(
- '%s Credential=%s/%s, SignedHeaders=%s, Signature=%s',
- $algorithm,
- $access_key,
- $credential_scope,
- $signed_headers,
- $signature
- );
-
- return [
- 'Accept' => $accept,
- 'Content-Type' => $contenttype,
- 'X-Date' => $xdate,
- 'Authorization' => $authorization_header
- ];
- }
- // 文字合成语音(火山引擎)
- public function tts($data) {
- $url = 'https://openspeech.bytedance.com/api/v1/tts';
- $headers = [
- 'Authorization' => 'Bearer;'.env('VOLC_TOKEN'),
- 'Content-Type' => 'application/json; charset=UTF-8'
- ];
- $post_data = [
- 'app' => [
- 'appid' => env('VOLC_APPID'),
- 'token' => env('VOLC_TOKEN'),
- 'cluster' => 'volcano_tts'
- ],
- 'user' => [
- 'uid' => 'mp_audio'
- ],
- // 'audio' => [
- // 'voice_type' =>
- // ],
- ];
- }
- public function scriptList($data) {
- $script_id = getProp($data, 'script_id');
- $script_name = getProp($data, 'script_name');
-
- $query = DB::table('mp_scripts')->where('is_deleted', 0)->select('*');
- if ($script_id) {
- $query->where('id', $script_id);
- }
- if ($script_name) {
- $query->where('script_name', 'like', "%{$script_name}%");
- }
- return $query->orderBy('created_at', 'desc')->paginate(12);
- }
- public function scripts($data) {
- $script_id = getProp($data, 'script_id');
- $script_name = getProp($data, 'script_name');
-
- $query = DB::table('mp_scripts')->where('is_deleted', 0)->select('id as script_id', 'script_name');
- if ($script_id) {
- $query->where('id', $script_id);
- }
- if ($script_name) {
- $query->where('script_name', 'like', "%{$script_name}%");
- }
- return $query->orderBy('created_at', 'desc')->get()->map(function ($value) {
- return (array)$value;
- })->toArray();
- }
- public function scriptInfo($data) {
- $script_id = getProp($data, 'script_id');
- $script = DB::table('mp_scripts')->where('id', $script_id)->where('is_deleted', 0)
- // ->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();
- ->selectRaw('id as script_id, script_name')->first();
- if (!$script) Utils::throwError('20003:该剧本不存在!');
- $script = (array)$script;
- // 获取分集组信息
- $groups = DB::table('mp_script_episode_group')->where('script_id', $script_id)->where('is_deleted', 0)
- ->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')
- ->orderBy('start_episode_number')->orderBy('id')->get()->map(function ($value) {
- $value = (array)$value;
- $value['episode_content'] = mb_substr($value['episode_content'], 0, 200);
- return (array)$value;
- })->toArray();
- $script['group'] = $groups;
- return $script;
- }
- public function createScript($data) {
- $script_name = getProp($data, 'script_name');
- $uid = Site::getUid(); // 获取当前用户ID
- if (!$script_name) Utils::throwError('20003:剧本名称不能为空');
- if (DB::table('mp_scripts')->where('script_name', $script_name)->exists()) {
- $script_name .= '_'.date('YmdHis');
- }
- return DB::table('mp_scripts')->insertGetId([
- 'script_name' => $script_name,
- 'user_id' => $uid,
- 'created_at' => date('Y-m-d H:i:s'),
- 'updated_at' => date('Y-m-d H:i:s')
- ]);
- }
- public function editScript($data) {
- $script_id = getProp($data, 'script_id');
- $script_name = getProp($data, 'script_name');
- $uid = Site::getUid(); // 获取当前用户ID
- if (!$script_id || !$script_name) Utils::throwError('20003:参数异常');
- $id = DB::table('mp_scripts')->where('script_name', $script_name)->value('id');
- if ($id && (int)$id !== (int)$script_id) {
- $script_name .= '_'.date('YmdHis');
- }
- return DB::table('mp_scripts')->where('id', $script_id)->update([
- 'script_name' => $script_name,
- 'updated_at' => date('Y-m-d H:i:s')
- ]);
- }
- public function delScript($data) {
- $script_id = getProp($data, 'script_id');
- $uid = Site::getUid(); // 获取当前用户ID
- if (!$script_id) Utils::throwError('20003:请选择剧本');
- return DB::table('mp_scripts')->where('id', $script_id)->update([
- 'is_deleted' => 1,
- 'updated_at' => date('Y-m-d H:i:s')
- ]);
- }
- public function createEpisode($data) {
- $script_id = getProp($data, 'script_id');
- $group_name = getProp($data, 'group_name');
- $remark = getProp($data, 'remark');
- if (!DB::table('mp_scripts')->where('id', $script_id)->value('id')) Utils::throwError('20003:剧本不存在!');
-
- $uid = Site::getUid(); // 获取当前用户ID
- if (!$group_name) Utils::throwError('20003:分集名称不能为空');
- if (DB::table('mp_script_episode_group')->where('group_name', $group_name)->exists()) {
- $group_name .= '_'.date('YmdHis');
- }
- return DB::table('mp_script_episode_group')->insertGetId([
- 'script_id' => $script_id,
- 'group_name' => $group_name,
- 'user_id' => $uid,
- 'remark' => $remark,
- 'created_at' => date('Y-m-d H:i:s'),
- 'updated_at' => date('Y-m-d H:i:s')
- ]);
- }
- public function editEpisode($data) {
- $group_id = getProp($data, 'group_id');
- $start_episode_number = getProp($data, 'start_episode_number');
- $end_episode_number = getProp($data, 'end_episode_number');
- $group_name = getProp($data, 'group_name');
- $uid = Site::getUid(); // 获取当前用户ID
- if (!$group_id || !$group_name || !$start_episode_number || !$end_episode_number) Utils::throwError('20003:参数异常');
- $id = DB::table('mp_script_episode_group')->where('group_name', $group_name)->value('id');
- if ($id && (int)$id !== (int)$group_id) {
- $group_name .= '_'.date('YmdHis');
- }
- return DB::table('mp_script_episode_group')->where('id', $group_id)->update([
- 'group_name' => $group_name,
- 'start_episode_number' => $start_episode_number,
- 'end_episode_number' => $end_episode_number,
- 'updated_at' => date('Y-m-d H:i:s')
- ]);
- }
- public function delEpisode($data) {
- $group_id = getProp($data, 'group_id');
- $uid = Site::getUid(); // 获取当前用户ID
- if (!$group_id) Utils::throwError('20003:请选择分集');
- return DB::table('mp_script_episode_group')->where('id', $group_id)->update([
- 'is_deleted' => 1,
- 'updated_at' => date('Y-m-d H:i:s')
- ]);
- }
- /**
- * 上传文件识别文字内容并与 DeepSeek 进行对话(流式输出版本)
- *
- * @param array $data 包含以下参数:
- * - file: 上传的文件(UploadedFile 对象)或文件路径
- * - question: 用户问题(可选)
- * - model: 使用的模型(r1 或 v3,默认 v3)
- * @return \Generator 返回生成器,用于流式输出
- */
- public function generateEpisodes($data) {
- $script_id = getProp($data, 'script_id');
- $group_id = getProp($data, 'group_id');
- // $file = getProp($data, 'file');
- $file = '';
- $content = getProp($data, 'content', '');
- // $bid = getProp($data, 'bid', 0);
- $bid = 0;
- $group = DB::table('mp_script_episode_group')->where('id', $group_id)->where('script_id', $script_id)->where('is_deleted', 0)->first();
- if (!$group) {
- Utils::throwError('20003:剧本分集不存在');
- }
- $start_episode_sequence = getProp($group, 'start_episode_number', 1);
- $end_episode_sequence = getProp($group, 'end_episode_number', 30);
- $total_episode_num = $end_episode_sequence - $start_episode_sequence + 1;
- $prompt = getProp($data, 'prompt');
- $question = getProp($data, 'question', "请根据实例完成一个{$total_episode_num}集的剧本,要求: 1.从第{$start_episode_sequence}集开始; 2.每集内容独立成章; 3.避免重复内容; 4.确保内容连贯且逻辑清晰; 5.所有章节合起来是给出的文档内容全文大概剧情。6.强制要求一定要给出我需要的分镜集数,不能少7.分集序号用01、02、03显示,不得使用01-1等子集序号,并且需要匹配开始集数的序号并往后顺序显示。\n");
- $model = getProp($data, 'model');
- if (!DB::table('mp_text_models')->where('model', $model)->where('is_enabled', 1)->value('id')) {
- Utils::throwError('20003:该模型不存在!');
- }
- // 检查是否存在重叠的剧集序号范围
- $exists_groups = DB::table('mp_script_episode_group')
- ->where('script_id', $script_id)
- ->where('id', '<>', $group_id) // 排除当前组
- ->where('is_deleted', 0)
- ->where(function($query) use ($start_episode_sequence, $end_episode_sequence) {
- // 检查新范围是否与现有范围重叠
- $query->where(function($q) use ($start_episode_sequence, $end_episode_sequence) {
- // 新范围的开始在现有范围内
- $q->where('start_episode_number', '<=', $start_episode_sequence)
- ->where('end_episode_number', '>=', $start_episode_sequence);
- })->orWhere(function($q) use ($start_episode_sequence, $end_episode_sequence) {
- // 新范围的结束在现有范围内
- $q->where('start_episode_number', '<=', $end_episode_sequence)
- ->where('end_episode_number', '>=', $end_episode_sequence);
- })->orWhere(function($q) use ($start_episode_sequence, $end_episode_sequence) {
- // 新范围完全包含现有范围
- $q->where('start_episode_number', '>=', $start_episode_sequence)
- ->where('end_episode_number', '<=', $end_episode_sequence);
- });
- })
- ->select('start_episode_number', 'end_episode_number')
- ->get();
-
- if ($exists_groups->isNotEmpty()) {
- $conflict_ranges = [];
- foreach ($exists_groups as $group) {
- $conflict_ranges[] = "第{$group->start_episode_number}-{$group->end_episode_number}集";
- }
- Utils::throwError('20003:剧集序号范围与已存在的剧集组重叠: '.implode('、', $conflict_ranges).',不得创建!');
- }
- // 模型兼容性处理:deepseek-chat 和 deepseek-reasoner 映射到 deepseek-v4-flash
- $thinkingMode = getProp($data, 'thinking', 'disabled'); // 默认非思考模式
- $reasoningEffort = getProp($data, 'reasoning_effort', 'high'); // 默认思考强度
-
- if ($model === 'deepseek-chat') {
- // deepseek-chat 映射到 deepseek-v4-flash,非思考模式
- $model = 'deepseek-v4-flash';
- $thinkingMode = 'disabled';
- } elseif ($model === 'deepseek-reasoner') {
- // deepseek-reasoner 映射到 deepseek-v4-flash,思考模式
- $model = 'deepseek-v4-flash';
- $thinkingMode = 'enabled';
- }
- $script = DB::table('mp_scripts')->where('id', $script_id)->first();
- if (!$script) {
- Utils::throwError('20003:剧本不存在');
- }
- $content = getProp($group, 'content');
- if (!$file && !$content && !$bid) {
- Utils::throwError('20003:请上传文件或选择书籍或输入内容');
- }
- // 提取文件内容
- if ($file) {
- $content = $this->extractFileContent($file);
- if (!$content) {
- Utils::throwError('20003:无法提取文件内容,请检查文件格式');
- }
- } elseif ($bid) {
- $content = $this->getContentByBid($bid);
- if (!$content) {
- Utils::throwError('20003:无法获取书籍内容,请检查书籍');
- }
- } elseif ($content) {
- $content = filterContent($content);
- } elseif ($prompt) {
- $content = filterContent($prompt);
- } else {
- Utils::throwError('20003:请上传文件或选择书籍或输入内容');
- }
- // 构建消息
- $messages = [
- $this->sys_message,
- [
- 'role' => 'user',
- 'content' => "以下是文档内容:\n\n{$content}\n\n用户问题:{$question}"
- ]
- ];
- $post_data = [
- 'model' => $model,
- 'messages' => $messages,
- // 'max_tokens' => 8192,
- 'temperature' => 0.7,
- 'frequency_penalty' => 0,
- 'presence_penalty' => 0,
- 'thinking' => ['type' => $thinkingMode],
- 'response_format' => ['type' => 'text'],
- 'stream' => true
- ];
- if ($thinkingMode == 'enabled') $post_data['reasoning_effort'] = $reasoningEffort;
- // 根据模型类型选择调用方法
- $fullContent = '';
- $fullReasoningContent = '';
- $usage = [];
-
- if (in_array($model, ['deepseek-reasoner', 'deepseek-chat', 'deepseek-v4-flash', 'deepseek-v4-pro'])) {
- // DeepSeek 官方模型使用 DeepSeek API
- $streamGenerator = $this->deepSeekStreamResponse($post_data);
- } else {
- // 其他模型使用火山引擎API
- $streamGenerator = $this->volcEngineChatCompletion($post_data);
- }
- // 处理流式输出
- foreach ($streamGenerator as $chunk) {
- if (isset($chunk['type'])) {
- if ($chunk['type'] === 'done') {
- // 最终结果
- $fullContent = $chunk['full_content'];
- $fullReasoningContent = $chunk['full_reasoning'];
- $usage = isset($chunk['usage']) ? $chunk['usage'] : [];
- } else {
- // 逐块yield数据
- yield $chunk;
- }
- }
- }
- dLog('deepseek')->info('完整内容: '.$fullContent);
- logDB('deepseek', 'info', '完整内容', ['fullContent'=>$fullContent]);
- // 处理完整内容并返回最终结果
- $script_arr = [];
- if ($fullContent) {
- $script_arr = extractScriptContent($fullContent);
- }
- logDB('deepseek', 'info', '解析内容', $script_arr);
- if (!$script_arr['episodes']) {
- Utils::throwError('20003:未生成剧本相关信息');
- // yield [
- // 'type' => 'done',
- // 'script' => $script_arr,
- // 'msg' => '未生成剧本相关信息',
- // 'answer' => $fullContent,
- // 'reasoning' => $fullReasoningContent,
- // 'usage' => $usage
- // ];
- // return ;
- }
- try {
- DB::beginTransaction();
- // // 返回前保存剧本内容
- // $boolen = DB::table('mp_scripts')->where('id', $script_id)->update([
- // 'content' => $content,
- // 'updated_at' => date('Y-m-d H:i:s')
- // ]);
- // if (!$boolen) {
- // Utils::throwError('20003:保存剧本内容失败');
- // }
- // $boolen1 = DB::table('mp_script_episode_group')->where('id', $group_id)->update([
- // 'start_episode_number' => $start_episode_sequence,
- // 'end_episode_number' => $end_episode_sequence,
- // 'updated_at' => date('Y-m-d H:i:s')
- // ]);
- // if (!$boolen1) {
- // Utils::throwError('20003:保存分集失败');
- // }
- $episode_content = '';
- $episodes = [];
- foreach ($script_arr['episodes'] as $item) {
- $episode_content .= "\u{200D}\u{200D}\u{200D}" . $item['episode_content'] . "\r\n\r\n";
- // $episodes[] = [
- // 'script_id' => $script_id,
- // 'episode_number' => $item['episode_number'],
- // 'episode_name' => $item['episode_name'],
- // 'episode_content' => $item['episode_content'],
- // 'created_at' => date('Y-m-d H:i:s'),
- // 'updated_at' => date('Y-m-d H:i:s'),
- // ];
- }
- if ($episode_content) {
- $boolen2 = DB::table('mp_script_episode_group')->where('id', $group_id)->update([
- 'episode_content' => $episode_content,
- 'updated_at' => date('Y-m-d H:i:s')
- ]);
- if (!$boolen2) {
- Utils::throwError('20003:保存分集内容失败');
- }
- }else {
- Utils::throwError('20003:分集剧本解析失败');
- }
- }catch (\Exception $e) {
- DB::rollBack();
- Utils::throwError('20003:'.$e->getMessage());
- }
- DB::commit();
- yield [
- 'type' => 'done',
- 'script' => $script_arr,
- 'answer' => $fullContent,
- 'reasoning' => $fullReasoningContent,
- 'usage' => $usage
- ];
- }
- public function chatWithFileStream($data) {
- $script_id = getProp($data, 'script_id');
- $group_id = getProp($data, 'group_id');
- $file = getProp($data, 'file');
- $content = getProp($data, 'content', '');
- $bid = getProp($data, 'bid', 0);
- $start_episode_sequence = getProp($data, 'start_episode_sequence', 1);
- $total_episode_num = getProp($data, 'total_episode_num', 30);
- $end_episode_sequence = $start_episode_sequence + $total_episode_num - 1;
- $prompt = getProp($data, 'prompt');
- $question = getProp($data, 'question', "请根据实例完成一个{$total_episode_num}集的剧本,要求: 1.从第{$start_episode_sequence}集开始; 2.每集内容独立成章; 3.避免重复内容; 4.确保内容连贯且逻辑清晰; 5.所有章节合起来是给出的文档内容全文大概剧情。6.强制要求一定要给出我需要的分镜集数,不能少7.分集序号用01、02、03显示,不得使用01-1等子集序号,并且需要匹配开始集数的序号并往后顺序显示。\n本次修改要求如下: {$prompt}\n");
- $model = getProp($data, 'model');
- if (!DB::table('mp_text_models')->where('model', $model)->where('is_enabled', 1)->value('id')) {
- Utils::throwError('20003:该模型不存在!');
- }
- // 检查是否存在重叠的剧集序号范围
- $exists_groups = DB::table('mp_script_episode_group')
- ->where('script_id', $script_id)
- ->where('group_id', '<>', $group_id)
- ->where('is_deleted', 0)
- ->where(function($query) use ($start_episode_sequence, $end_episode_sequence) {
- // 检查新范围是否与现有范围重叠
- $query->where(function($q) use ($start_episode_sequence, $end_episode_sequence) {
- // 新范围的开始在现有范围内
- $q->where('start_episode_number', '<=', $start_episode_sequence)
- ->where('end_episode_number', '>=', $start_episode_sequence);
- })->orWhere(function($q) use ($start_episode_sequence, $end_episode_sequence) {
- // 新范围的结束在现有范围内
- $q->where('start_episode_number', '<=', $end_episode_sequence)
- ->where('end_episode_number', '>=', $end_episode_sequence);
- })->orWhere(function($q) use ($start_episode_sequence, $end_episode_sequence) {
- // 新范围完全包含现有范围
- $q->where('start_episode_number', '>=', $start_episode_sequence)
- ->where('end_episode_number', '<=', $end_episode_sequence);
- });
- })
- ->select('start_episode_number', 'end_episode_number')
- ->get();
-
- if ($exists_groups->isNotEmpty()) {
- $conflict_ranges = [];
- foreach ($exists_groups as $group) {
- $conflict_ranges[] = "第{$group->start_episode_number}-{$group->end_episode_number}集";
- }
- Utils::throwError('20003:剧集序号范围与已存在的剧集组重叠: '.implode('、', $conflict_ranges).',不得创建!');
- }
- // 模型兼容性处理:deepseek-chat 和 deepseek-reasoner 映射到 deepseek-v4-flash
- $thinkingMode = getProp($data, 'thinking', 'disabled'); // 默认非思考模式
- $reasoningEffort = getProp($data, 'reasoning_effort', 'high'); // 默认思考强度
-
- if ($model === 'deepseek-chat') {
- // deepseek-chat 映射到 deepseek-v4-flash,非思考模式
- $model = 'deepseek-v4-flash';
- $thinkingMode = 'disabled';
- } elseif ($model === 'deepseek-reasoner') {
- // deepseek-reasoner 映射到 deepseek-v4-flash,思考模式
- $model = 'deepseek-v4-flash';
- $thinkingMode = 'enabled';
- }
- $script = DB::table('mp_scripts')->where('id', $script_id)->first();
- if (!$script) {
- Utils::throwError('20003:剧本不存在');
- }
- $group = DB::table('mp_script_episode_group')->where('id', $group_id)->where('script_id', $script_id)->where('is_deleted', 0)->first();
- if (!$group) {
- Utils::throwError('20003:剧本分集不存在');
- }
- if (!$file && !$content && !$bid) {
- Utils::throwError('20003:请上传文件或选择书籍或输入内容');
- }
- // 提取文件内容
- if ($file) {
- $content = $this->extractFileContent($file);
- if (!$content) {
- Utils::throwError('20003:无法提取文件内容,请检查文件格式');
- }
- } elseif ($bid) {
- $content = $this->getContentByBid($bid);
- if (!$content) {
- Utils::throwError('20003:无法获取书籍内容,请检查书籍');
- }
- } elseif ($content) {
- $content = filterContent($content);
- } elseif ($prompt) {
- $content = filterContent($prompt);
- } else {
- Utils::throwError('20003:请上传文件或选择书籍或输入内容');
- }
- // 构建消息
- $messages = [
- [
- 'role' => 'system',
- 'content' => "你是一个专业的文档分析助手及资深编剧,请根据用户提供的文档内容完成示例格式的内容输出(需通过以下几个板块进行回复: <故事梗概><剧本亮点><人物关系><核心矛盾><主体列表><美术风格><场景列表>,每个板块之间用###分隔;同时板块之间需满足以下要求:\n
- 强制要求:\n
- 1.你只负责与剧本相关的内容,如果用户输入与剧本无关的内容,请告知用户并拒绝继续回答,此为第一优先原则,不满足此原则就忽略其他要求并停止继续输出
- 2.请严格按照示例格式返回内容,不要更改格式或随意增加符号\n
- 普通要求:\n
- 1.<主体列表>中出现的主体必须尽可能统一,只允许同一个角色年龄段相差较大的情况下可重复,比如梁萧-少年、梁萧-青年、梁萧-中年、梁萧-老年这种情况,不得出现梁萧-愤怒、梁萧-隐忍、梁萧-求生等情绪和状态变化的同一角色;并且角色的样貌、形体及穿着描述尽可能详细\n\n
- 2.不要出现分集内容,只保留剧本大纲的格式,格式要求和示例如下,请强制按该示例输出
- 示例如下:\n
- ###剧本名:西昆仑
- ###故事梗概
- 内容梗概:古风厚涂的世界中,少女阿雪身中奇毒“问心刺”,气息奄奄。少年梁萧心急如焚,用奇珍“阴阳球”稳住阿雪性命,却发现必须解衣拔刺。阿雪羞涩难当,梁萧情急之下提出结拜兄妹,以化解尴尬。两人匆匆结拜后,梁萧小心翼翼地拔出“问心刺”,竟发现它是一根诡异的女子秀发。阿雪转危为安,两人之间,在生死与羞涩交织的瞬间,情愫暗生,却又被兄妹之名束缚,留下无尽遐想。
- ###剧本亮点
- 亮点1:绝境奇药,生死一线
- 画面开场即是阿雪苍白无力的脸庞,梁萧眼中泪光闪烁,急促的呼吸声与心跳声烘托紧张气氛。特写梁萧将“阴阳球”塞入阿雪舌底,球体散发微光,阿雪气息从若有若无到逐渐平稳,生死逆转的瞬间,视觉与听觉冲击力十足。
- 亮点2:结拜兄妹,化解尴尬
- 阿雪因解衣拔刺而羞愤欲绝的特写,梁萧眼神中的挣扎与无奈。在紧迫的背景音下,梁萧急中生智,牵起阿雪颤抖的手,跪地匆匆结拜,阿雪眼神从惊惶到迷茫,最终一句“我……我愿意的”带着哭腔,将救命与羞耻的矛盾推向高潮,又以出人意料的方式解决。
- 亮点3:纤发夺命,情愫暗涌
- 拔刺过程的紧张特写,梁萧指尖小心翼翼地捏住细丝,缓慢抽出。当“问心刺”被完全拔出,特写它竟是一根细长、泛着寒光的女子秀发,诡异而令人震惊。阿雪苏醒后,两人眼神交汇,阿雪低头羞涩,梁萧眼中复杂的情绪,无声地传递出超越兄妹的微妙情愫。
- ###人物关系
- 阿雪与梁萧之间存在兄妹之情。
- ###核心矛盾
- 阿雪与梁萧之间存在兄妹之情,但因情愫暗生而产生微妙的矛盾。阿雪羞涩难当,梁萧情急之下提出结拜兄妹,以化解尴尬。两人匆匆结拜后,梁萧小心翼翼地拔出“问心刺”,竟发现它是一根诡异的女子秀发。阿雪转危为安,两人之间,在生死与羞涩交织的瞬间,情愫暗生,却又被兄妹之名束缚,留下无尽遐想。
- ###主体列表
- 阿雪-病重:身中奇毒“问心刺”,气息奄奄,面色苍白,身体虚弱。
- 阿雪-羞涩:经“阴阳球”救治后逐渐恢复,脸颊泛起红晕,眼神娇羞,情愫暗生。
- 梁萧:少年侠士,心系阿雪安危,沉着冷静,内心挣扎于情义之间。
- 阴阳球:天地异宝,能化生精气,救人于危难。
- 问心刺:一根诡异的女子秀发,细长泛寒光,是致命的奇毒。
- ###美术风格
- 基础画风风格词:厚涂古风
- 整体采用精致细腻的厚涂古风,色彩浓郁而富有层次感,注重光影对比,营造出古典而富有戏剧张力的氛围。色调以深沉的蓝绿、灰调为主,衬托阿雪病弱的苍白,同时用温暖的金色、琥珀色光晕突出“阴阳球”的生命力。拔刺与结拜场景则以柔和的暖色调为主,辅以人物脸颊的红晕,烘托羞涩与情愫暗生的微妙感。人物造型飘逸,服饰细节考究,背景虚实结合,既有古韵,又不失奇幻色彩。
- ###场景列表
- 林间疗伤地:幽静的古风林间空地,一棵高大古树下,是梁萧为阿雪疗伤的地点。环境清幽,光影斑驳,既有古韵又不失奇幻色彩。"],
- [
- 'role' => 'user',
- 'content' => "以下是文档内容:\n\n{$content}\n\n用户问题:{$question}"
- ]
- ];
- $post_data = [
- 'model' => $model,
- 'messages' => $messages,
- // 'max_tokens' => 8192,
- 'temperature' => 0.7,
- 'frequency_penalty' => 0,
- 'presence_penalty' => 0,
- 'thinking' => ['type' => $thinkingMode],
- 'response_format' => ['type' => 'text'],
- 'stream' => true
- ];
- if ($thinkingMode == 'enabled') $post_data['reasoning_effort'] = $reasoningEffort;
- // 根据模型类型选择调用方法
- $fullContent = '';
- $fullReasoningContent = '';
- $usage = [];
-
- if (in_array($model, ['deepseek-reasoner', 'deepseek-chat', 'deepseek-v4-flash', 'deepseek-v4-pro'])) {
- // DeepSeek 官方模型使用 DeepSeek API
- $streamGenerator = $this->deepSeekStreamResponse($post_data);
- } else {
- // 其他模型使用火山引擎API
- $streamGenerator = $this->volcEngineChatCompletion($post_data);
- }
- // 处理流式输出
- foreach ($streamGenerator as $chunk) {
- if (isset($chunk['type'])) {
- if ($chunk['type'] === 'done') {
- // 最终结果
- $fullContent = $chunk['full_content'];
- $fullReasoningContent = $chunk['full_reasoning'];
- $usage = isset($chunk['usage']) ? $chunk['usage'] : [];
- } else {
- // 逐块yield数据
- yield $chunk;
- }
- }
- }
- dLog('deepseek')->info('完整内容: '.$fullContent);
- logDB('deepseek', 'info', '完整内容', ['fullContent'=>$fullContent]);
- // 处理完整内容并返回最终结果
- $script_arr = [];
- if ($fullContent) {
- $script_arr = extractScriptContent($fullContent);
- }
- logDB('deepseek', 'info', '解析内容', $script_arr);
- if (!$script_arr['roles']) {
- Utils::throwError('20003:未生成剧本相关信息');
- // yield [
- // 'type' => 'done',
- // 'script' => $script_arr,
- // 'msg' => '未生成剧本相关信息',
- // 'answer' => $fullContent,
- // 'reasoning' => $fullReasoningContent,
- // 'usage' => $usage
- // ];
- }
- try {
- DB::beginTransaction();
- // // 返回前保存剧本内容
- // $boolen = DB::table('mp_scripts')->where('id', $script_id)->update([
- // 'content' => $content,
- // 'status' => '解析完成',
- // 'updated_at' => date('Y-m-d H:i:s')
- // ]);
- // if (!$boolen) {
- // Utils::throwError('20003:保存剧本内容失败');
- // }
- $boolen1 = DB::table('mp_script_episode_group')->where('id', $group_id)->update([
- 'start_episode_number' => $start_episode_sequence,
- 'end_episode_number' => $end_episode_sequence,
- 'intro' => isset($script_arr['intro']) ? $script_arr['intro'] : '',
- 'highlights' => isset($script_arr['highlights']) ? $script_arr['highlights'] : '',
- 'role_relationship' => isset($script_arr['role_relationship']) ? $script_arr['role_relationship'] : '',
- 'core_contradiction' => isset($script_arr['core_contradiction']) ? $script_arr['core_contradiction'] : '',
- 'roles' => isset($script_arr['roles']) ? json_encode($script_arr['roles'], 256) : '',
- 'art_style' => isset($script_arr['art_style']) ? $script_arr['art_style'] : '',
- 'scenes' => isset($script_arr['scenes']) ? json_encode($script_arr['scenes'], 256) : '',
- 'status' => '解析完成',
- 'content' => $content,
- 'updated_at' => date('Y-m-d H:i:s')
- ]);
- if (!$boolen1) {
- Utils::throwError('20003:保存分集失败');
- }
- // $episodes = [];
- // foreach ($script_arr['episodes'] as $item) {
- // $episodes[] = [
- // 'script_id' => $script_id,
- // 'episode_number' => $item['episode_number'],
- // 'episode_name' => $item['episode_name'],
- // 'episode_content' => $item['episode_content'],
- // 'created_at' => date('Y-m-d H:i:s'),
- // 'updated_at' => date('Y-m-d H:i:s'),
- // ];
- // }
- // if ($episodes) {
- // $boolen2 = DB::table('mp_script_episodes')->insert($episodes);
- // if (!$boolen2) {
- // Utils::throwError('20003:保存分集内容失败');
- // }
- // }else {
- // Utils::throwError('20003:分集剧本解析失败');
- // }
- }catch (\Exception $e) {
- DB::rollBack();
- Utils::throwError('20003:'.$e->getMessage());
- }
- DB::commit();
- yield [
- 'type' => 'done',
- 'script' => $script_arr,
- 'answer' => $fullContent,
- 'reasoning' => $fullReasoningContent,
- 'usage' => $usage
- ];
- }
- /**
- * 上传文件识别文字内容并与 DeepSeek 进行对话
- *
- * @param array $data 包含以下参数:
- * - file: 上传的文件(UploadedFile 对象)或文件路径
- * - question: 用户问题(可选,默认为"请总结这个文件的内容")
- * - model: 使用的模型(r1 或 v3,默认 v3)
- * @return array
- */
- public function chatWithFile($data) {
- $script_id = getProp($data, 'script_id');
- $file = getProp($data, 'file');
- $content = getProp($data, 'content', '');
- $bid = getProp($data, 'bid', 0);
- $start_episode_sequence = getProp($data, 'start_episode_sequence', 1);
- $total_episode_num = getProp($data, 'total_episode_num', 30);
- $prompt = getProp($data, 'prompt');
- $question = getProp($data, 'question', "请根据实例完成一个{$total_episode_num}集的剧本,要求: 1.从第{$start_episode_sequence}集开始; 2.每集内容独立成章; 3.避免重复内容; 4.确保内容连贯且逻辑清晰; 5.所有章节合起来是给出的文档内容全文大概剧情。6.要求一定要给出我需要的分镜集数,不能少7.分集序号用01、02、03显示,不得使用01-1等子集序号,并且需要匹配开始集数的序号并往后顺序显示。\n本次修改要求如下: {$prompt}\n");
-
- // 处理文本模型
- $model = getProp($data, 'model');
- if (!$model) {
- // 用户没有输入,使用默认值
- $model = 'doubao-seed-2-0-mini-260215';
- }
- // 验证模型是否在可用模型表中
- if (!DB::table('mp_text_models')->where('model', $model)->where('is_enabled', 1)->exists()) {
- $model = 'doubao-seed-2-0-mini-260215';
- }
- // 模型兼容性处理:deepseek-chat 和 deepseek-reasoner 映射到 deepseek-v4-flash
- $thinkingMode = getProp($data, 'thinking', 'disabled'); // 默认非思考模式
- $reasoningEffort = getProp($data, 'reasoning_effort', 'high'); // 默认思考强度
-
- if ($model === 'deepseek-chat') {
- // deepseek-chat 映射到 deepseek-v4-flash,非思考模式
- $model = 'deepseek-v4-flash';
- $thinkingMode = 'disabled';
- } elseif ($model === 'deepseek-reasoner') {
- // deepseek-reasoner 映射到 deepseek-v4-flash,思考模式
- $model = 'deepseek-v4-flash';
- $thinkingMode = 'enabled';
- }
- $script = DB::table('mp_scripts')->where('id', $script_id)->first();
- if (!$script) {
- Utils::throwError('20003:剧本不存在');
- }
- if (!$file && !$content && !$bid) {
- Utils::throwError('20003:请上传文件或选择书籍或输入内容');
- }
- // 提取文件内容
- if ($file) {
- $content = $this->extractFileContent($file);
- if (!$content) {
- Utils::throwError('20003:无法提取文件内容,请检查文件格式');
- }
- } elseif ($bid) {
- $content = $this->getContentByBid($bid);
- if (!$content) {
- Utils::throwError('20003:无法获取书籍内容,请检查书籍');
- }
- } elseif ($content) {
- $content = filterContent($content);
- } elseif ($prompt) {
- $content = filterContent($prompt);
- } else {
- Utils::throwError('20003:请上传文件或选择书籍或输入内容');
- }
- // 构建消息
- $messages = [
- $this->sys_message,
- [
- 'role' => 'user',
- 'content' => "以下是文档内容:\n\n{$content}\n\n用户问题:{$question}"
- ]
- ];
- $post_data = [
- 'model' => $model,
- 'messages' => $messages,
- // 'max_tokens' => 8192,
- 'temperature' => 0.7,
- 'frequency_penalty' => 0,
- 'presence_penalty' => 0,
- 'thinking' => ['type' => $thinkingMode],
- 'response_format' => ['type' => 'text'],
- 'stream' => false
- ];
- if ($thinkingMode == 'enabled') $post_data['reasoning_effort'] = $reasoningEffort;
- // 根据模型类型选择调用方法
- $fullContent = '';
- $usage = [];
-
- if (in_array($model, ['deepseek-reasoner', 'deepseek-chat', 'deepseek-v4-flash', 'deepseek-v4-pro'])) {
- // DeepSeek 官方模型使用 DeepSeek API
- $chatResult = $this->chatOnly($post_data);
- } else {
- // 其他模型使用火山引擎API
- $chatResult = $this->volcEngineChatCompletion($post_data);
- }
-
- if (is_array($chatResult)) {
- $fullContent = $chatResult['fullContent'];
- $usage = $chatResult['usage'];
- }
- $script_arr = [];
- // 处理结果
- if ($fullContent) {
- $script_arr = extractScriptContent($fullContent);
- }
- // 返回前保存剧本内容
- DB::table('mp_scripts')->where('id', $script_id)->update([
- 'content' => $content,
- 'updated_at' => date('Y-m-d H:i:s')
- ]);
- return [
- 'script' => $script_arr,
- 'answer' => $fullContent,
- 'usage' => $usage
- ];
- }
- public function getEpisodeContent($data) {
- $group_id = getProp($data, 'group_id');
- return DB::table('mp_script_episode_group')->where('id', $group_id)->value('episode_content');
- }
- public function saveEpisodeContent($data) {
- $group_id = getProp($data, 'group_id');
- $episode_content = getProp($data, 'episode_content');
- return DB::table('mp_script_episode_group')->where('id', $group_id)->update([
- 'content' => $episode_content,
- 'updated_at' => date('Y-m-d H:i:s')
- ]);
- $script_arr =getProp($data, 'script', []);
- if (!$script_arr) Utils::throwError('20003:剧本内容不能为空');
- $start_episode_sequence = getProp($data, 'start_episode_sequence', 1);
- $script_id = getProp($data, 'script_id');
- if (!$script_id) Utils::throwError('20003:剧本ID不能为空');
- try {
- DB::beginTransaction();
- $update_data = [
- 'intro' => isset($script_arr['intro']) ? $script_arr['intro'] : '',
- 'highlights' => isset($script_arr['highlights']) ? $script_arr['highlights'] : '',
- 'role_relationship' => isset($script_arr['role_relationship']) ? $script_arr['role_relationship'] : '',
- 'core_contradiction' => isset($script_arr['core_contradiction']) ? $script_arr['core_contradiction'] : '',
- 'roles' => isset($script_arr['roles']) ? json_encode($script_arr['roles'], 256) : '',
- 'art_style' => isset($script_arr['art_style']) ? $script_arr['art_style'] : '',
- 'scenes' => isset($script_arr['scenes']) ? json_encode($script_arr['scenes'], 256) : '',
- 'status' => 3,
- 'start_episode_sequence' => $start_episode_sequence,
- 'end_episode_sequence' => $start_episode_sequence + count($script_arr['episodes']) - 1,
- 'episode_num' => count($script_arr['episodes']),
- 'updated_at' => date('Y-m-d H:i:s')
- ];
- // 保存剧本内容
- $boolen = DB::table('mp_scripts')->where('id', $script_id)->update($update_data);
- if (!$boolen) {
- dLog('deepseek')->info('剧本内容保存失败', $update_data);
- logDB('deepseek', 'error', '剧本内容保存失败', ['script_id' => $script_id]);
- Utils::throwError('20003:剧本内容保存失败');
- }
-
- // 保存分集内容
- // 删除历史分集内容
- $boolen1 = DB::table('mp_script_episodes')->where('script_id', $script_id)->delete();
-
- $episodes = [];
- $sequence = 1;
- foreach ($script_arr['episodes'] as $episode) {
- $segment_number = 1;
- foreach($episode['segments'] as $segment) {
- $episodes[] = [
- 'script_id' => $script_id,
- 'episode_number' => $episode['episode_number'],
- 'title' => $episode['title'],
- // 'content' => $episode['content'],
- 'content' => '',
- 'segment_number' => $segment_number,
- 'segment_content' => $segment['segment_content'],
- 'sequence' => $sequence,
- 'created_at' => date('Y-m-d H:i:s'),
- 'updated_at' => date('Y-m-d H:i:s')
- ];
- $sequence++;
- $segment_number++;
- }
-
- }
- $boolen2 = DB::table('mp_script_episodes')->insert($episodes);
- if (!$boolen2) {
- dLog('deepseek')->info('分集内容保存失败', $episodes);
- logDB('deepseek', 'error', '分集内容保存失败', ['episodes_count' => count($episodes)]);
- Utils::throwError('20003:分集内容保存失败');
- }
- } catch (\Exception $e) {
- DB::rollBack();
- Utils::throwError('20003:'.$e->getMessage());
- }
- DB::commit();
- return true;
- }
- public function getBookContent($data) {
- $bid = getProp($data, 'bid');
- $start_sequence = getProp($data, 'start_sequence');
- $end_sequence = getProp($data, 'end_sequence');
- $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])
- ->orderBy('c.sequence')->pluck('cc.content')->toArray();
- $return_content = filterContent(implode(PHP_EOL, $chapter_contents));
-
- return $return_content;
- }
- public function exportScript($data) {
- $filename = getProp($data, 'filename');
- $script_id = getProp($data, 'script_id');
- $group_id = getProp($data, 'group_id');
- $script = DB::table('mp_scripts')->where('id', $script_id)->where('is_deleted', 0)
- ->selectRaw('id as script_id, script_name, episode_num, remark, created_at')->first();
- if (!$script) Utils::throwError('20003:该剧本不存在!');
- $script = (array)$script;
- // 获取分集组信息
- if ($group_id) {
- $groups = DB::table('mp_script_episode_group')->where('script_id', $script_id)->where('id', $group_id)->where('is_deleted', 0)->whereNotNull('episode_content')
- ->selectRaw('intro, highlights, roles, role_relationship, core_contradiction, art_style, scenes, status, start_episode_number, end_episode_number, episode_content')
- ->get()->map(function($value) {
- return (array)$value;
- })->toArray();
- }else {
- $groups = DB::table('mp_script_episode_group')->where('script_id', $script_id)->where('is_deleted', 0)->whereNotNull('episode_content')
- ->selectRaw('intro, highlights, roles, role_relationship, core_contradiction, art_style, scenes, status, start_episode_number, end_episode_number, episode_content')
- ->orderBy('start_episode_number')->orderBy('id')->get()->map(function($value) {
- return (array)$value;
- })->toArray();
- }
- if (!$groups) Utils::throwError('20003:分集不存在');
- $script['group'] = $groups;
- $export_type = getProp($data, 'export_type', 'txt');
-
- // 生成文件名
- $filename = $filename ? $filename : $script['script_name'] . '_' . date('YmdHis');
-
- // 根据导出类型生成不同格式的文件
- switch ($export_type) {
- case 'txt':
- return $this->exportScriptAsTxt($script, $filename);
- case 'pdf':
- return $this->exportScriptAsPdf($script, $filename);
- default:
- Utils::throwError('20003:不支持的导出格式,支持:txt、pdf');
- }
- }
- /**
- * 导出剧本为TXT格式
- */
- private function exportScriptAsTxt($script, $filename) {
- // 构建TXT内容
- $content = $this->buildScriptContent($script);
-
- // 设置响应头,直接下载
- header('Content-Type: text/plain; charset=utf-8');
- header('Content-Disposition: attachment; filename="' . $filename . '.txt"');
- header('Content-Length: ' . strlen($content));
-
- // 输出内容并结束
- echo $content;
- exit();
- }
- /**
- * 导出剧本为PDF格式
- */
- private function exportScriptAsPdf($script, $filename) {
- // 使用HTML转PDF的方式来更好地支持中文
- $htmlContent = $this->buildScriptHtmlForPdf($script);
-
- // 创建PDF实例
- $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
-
- // 设置文档信息
- $pdf->SetCreator('剧本导出系统');
- $pdf->SetAuthor('系统');
- $pdf->SetTitle($script['script_name']);
- $pdf->SetSubject('剧本导出');
-
- // 设置边距
- $pdf->SetMargins(15, 15, 15);
- $pdf->SetAutoPageBreak(TRUE, 15);
-
- // 添加页面
- $pdf->AddPage();
- // 注册并使用 simsun.ttf 字体
- $pdf->SetFont('simsun', '', 12); // 设置字体和大小
-
- // 使用HTML内容生成PDF
- $pdf->writeHTML($htmlContent, true, false, true, false, '');
-
- // 直接输出PDF文件供下载
- header('Content-Type: application/pdf');
- header('Content-Disposition: attachment; filename="' . $filename . '.pdf"');
-
- // 直接输出PDF内容
- $pdf->Output($filename . '.pdf', 'D');
-
- exit();
- }
- /**
- * 构建用于PDF的HTML内容
- */
- private function buildScriptHtmlForPdf($script) {
- $html = '<style>
- body { font-family: "SimSun", "宋体", sans-serif; font-size: 12px; line-height: 1.6; }
- h1 { font-size: 18px; text-align: center; margin-bottom: 20px; }
- h2 { font-size: 16px; margin-top: 20px; margin-bottom: 10px; border-bottom: 1px solid #ccc; }
- h3 { font-size: 14px; margin-top: 15px; margin-bottom: 8px; }
- .info { margin-bottom: 8px; }
- .episode { margin-bottom: 20px; page-break-inside: avoid; }
- .segment { margin-bottom: 10px; margin-left: 20px; }
- </style>';
-
- // 标题
- $html .= '<h1>' . htmlspecialchars($script['script_name']) . '</h1>';
-
- // 剧本简介
- if (!empty($script['intro'])) {
- $html .= '<h2>剧本简介</h2>';
- $html .= '<p>' . nl2br(htmlspecialchars($script['intro'])) . '</p>';
- }
-
- // 亮点
- if (!empty($script['highlights'])) {
- $html .= '<h2>亮点</h2>';
- $html .= '<p>' . nl2br(htmlspecialchars($script['highlights'])) . '</p>';
- }
-
- // 核心矛盾
- if (!empty($script['core_contradiction'])) {
- $html .= '<h2>核心矛盾</h2>';
- $html .= '<p>' . nl2br(htmlspecialchars($script['core_contradiction'])) . '</p>';
- }
-
- // 艺术风格
- if (!empty($script['art_style'])) {
- $html .= '<h2>艺术风格</h2>';
- $html .= '<p>' . nl2br(htmlspecialchars($script['art_style'])) . '</p>';
- }
-
- // 角色列表
- if (!empty($script['roles']) && is_array($script['roles'])) {
- $html .= '<h2>角色列表</h2>';
- $html .= '<ul>';
- foreach ($script['roles'] as $role) {
- if (is_array($role) || is_object($role)) {
- $html .= '<li>' . htmlspecialchars(getProp($role, 'role', '')) . ':' . htmlspecialchars(getProp($role, 'description', '')) . '</li>';
- } else {
- $html .= '<li>' . htmlspecialchars($role) . '</li>';
- }
- }
- $html .= '</ul>';
- }
-
- // 角色关系
- if (!empty($script['role_relationship'])) {
- $html .= '<h2>角色关系</h2>';
- $html .= '<p>' . nl2br(htmlspecialchars($script['role_relationship'])) . '</p>';
- }
-
- // 场景列表
- if (!empty($script['scenes']) && is_array($script['scenes'])) {
- $html .= '<h2>场景列表</h2>';
- $html .= '<ul>';
- foreach ($script['scenes'] as $scene) {
- if (is_array($scene) || is_object($scene)) {
- $html .= '<li>' . htmlspecialchars(getProp($scene, 'scene', '')) . ':' . htmlspecialchars(getProp($scene, 'description', '')) . '</li>';
- } else {
- $html .= '<li>' . htmlspecialchars($scene) . '</li>';
- }
- }
- $html .= '</ul>';
- }
-
- // 分集内容
- if (!empty($script['episodes']) && is_array($script['episodes'])) {
- $html .= '<h2>分集内容</h2>';
-
- foreach ($script['episodes'] as $episode) {
- $html .= '<div class="episode">';
- $html .= '<h3>第' . htmlspecialchars($episode['episode_number']) . '集';
- if (!empty($episode['episode_name'])) {
- $html .= ':' . htmlspecialchars($episode['episode_name']);
- }
- $html .= '</h3>';
- // 处理分集内容
- if (!empty($episode['episode_content'])) {
- $html .= '<div class="segment">';
- // 去掉第一行(标题行)
- $content = $episode['episode_content'];
- $lines = explode("\n", $content);
- if (count($lines) > 1) {
- array_shift($lines); // 移除第一行
- $content = implode("\n", $lines);
- } else {
- $content = ''; // 如果只有一行,则清空
- }
- $html .= nl2br(htmlspecialchars($content));
- $html .= '</div>';
- }
-
- $html .= '</div>';
- }
- }
-
- return $html;
- }
- /**
- * 构建PDF内容
- */
- private function buildPdfContent($pdf, $script) {
- // 标题
- $pdf->SetFont('dejavusans', 'B', 18);
- $pdf->Cell(0, 15, $script['script_name'], 0, 1, 'C');
- $pdf->Ln(5);
-
- // 基本信息
- $pdf->SetFont('dejavusans', '', 12);
- $pdf->Cell(0, 8, '创建时间:' . $script['created_at'], 0, 1);
- $pdf->Cell(0, 8, '状态:' . $script['status_info'], 0, 1);
- $pdf->Ln(5);
-
- // 剧本简介
- if (!empty($script['intro'])) {
- $pdf->SetFont('dejavusans', 'B', 14);
- $pdf->Cell(0, 10, '剧本简介', 0, 1);
- $pdf->SetFont('dejavusans', '', 12);
- $pdf->MultiCell(0, 8, $script['intro'], 0, 'L');
- $pdf->Ln(3);
- }
-
- // 亮点
- if (!empty($script['highlights'])) {
- $pdf->SetFont('dejavusans', 'B', 14);
- $pdf->Cell(0, 10, '亮点', 0, 1);
- $pdf->SetFont('dejavusans', '', 12);
- $pdf->MultiCell(0, 8, $script['highlights'], 0, 'L');
- $pdf->Ln(3);
- }
-
- // 核心矛盾
- if (!empty($script['core_contradiction'])) {
- $pdf->SetFont('dejavusans', 'B', 14);
- $pdf->Cell(0, 10, '核心矛盾', 0, 1);
- $pdf->SetFont('dejavusans', '', 12);
- $pdf->MultiCell(0, 8, $script['core_contradiction'], 0, 'L');
- $pdf->Ln(3);
- }
-
- // 艺术风格
- if (!empty($script['art_style'])) {
- $pdf->SetFont('dejavusans', 'B', 14);
- $pdf->Cell(0, 10, '艺术风格', 0, 1);
- $pdf->SetFont('dejavusans', '', 12);
- $pdf->MultiCell(0, 8, $script['art_style'], 0, 'L');
- $pdf->Ln(3);
- }
-
- // 备注
- if (!empty($script['remark'])) {
- $pdf->SetFont('dejavusans', 'B', 14);
- $pdf->Cell(0, 10, '备注', 0, 1);
- $pdf->SetFont('dejavusans', '', 12);
- $pdf->MultiCell(0, 8, $script['remark'], 0, 'L');
- $pdf->Ln(3);
- }
-
- // 角色列表
- if (!empty($script['roles']) && is_array($script['roles'])) {
- $pdf->SetFont('dejavusans', 'B', 14);
- $pdf->Cell(0, 10, '角色列表', 0, 1);
- $pdf->SetFont('dejavusans', '', 12);
- foreach ($script['roles'] as $index => $role) {
- if (is_array($role) || is_object($role)) {
- $pdf->Cell(0, 8, ($index + 1) . '. ' . getProp($role, 'role', '') . ':' . getProp($role, 'description', ''), 0, 1);
- } else {
- $pdf->Cell(0, 8, ($index + 1) . '. ' . $role, 0, 1);
- }
- }
- $pdf->Ln(3);
- }
-
- // 角色关系
- if (!empty($script['role_relationship'])) {
- $pdf->SetFont('dejavusans', 'B', 14);
- $pdf->Cell(0, 10, '角色关系', 0, 1);
- $pdf->SetFont('dejavusans', '', 12);
- $pdf->MultiCell(0, 8, $script['role_relationship'], 0, 'L');
- $pdf->Ln(3);
- }
-
- // 场景列表
- if (!empty($script['scenes']) && is_array($script['scenes'])) {
- $pdf->SetFont('dejavusans', 'B', 14);
- $pdf->Cell(0, 10, '场景列表', 0, 1);
- $pdf->SetFont('dejavusans', '', 12);
- foreach ($script['scenes'] as $index => $scene) {
- if (is_array($scene) || is_object($scene)) {
- $pdf->Cell(0, 8, ($index + 1) . '. ' . getProp($scene, 'scene', '') . ':' . getProp($scene, 'description', ''), 0, 1);
- } else {
- $pdf->Cell(0, 8, ($index + 1) . '. ' . $scene, 0, 1);
- }
- }
- $pdf->Ln(5);
- }
-
- // 分集内容
- if (!empty($script['episodes']) && is_array($script['episodes'])) {
- $pdf->SetFont('dejavusans', 'B', 16);
- $pdf->Cell(0, 12, '分集内容', 0, 1);
- $pdf->Ln(3);
-
- foreach ($script['episodes'] as $episode) {
- // 检查是否需要新页面
- if ($pdf->GetY() > 250) {
- $pdf->AddPage();
- }
-
- $pdf->SetFont('dejavusans', 'B', 14);
- $episodeTitle = '第' . $episode['episode_number'] . '集';
- if (!empty($episode['title'])) {
- $episodeTitle .= ':' . $episode['title'];
- }
- $pdf->Cell(0, 10, $episodeTitle, 0, 1);
-
- // 处理分段内容
- if (!empty($episode['segments']) && is_array($episode['segments'])) {
- $pdf->SetFont('dejavusans', '', 12);
- foreach ($episode['segments'] as $segment) {
- if (!empty($segment['segment_content'])) {
- // 如果有分段编号,显示分段标题
- if (!empty($segment['segment_number'])) {
- $pdf->SetFont('dejavusans', 'B', 12);
- $pdf->Cell(0, 8, '第' . $segment['segment_number'] . '段', 0, 1);
- $pdf->SetFont('dejavusans', '', 12);
- }
- $pdf->MultiCell(0, 8, $segment['segment_content'], 0, 'L');
- $pdf->Ln(2);
- }
- }
- }
-
- $pdf->Ln(5);
- }
- }
- }
- /**
- * 构建剧本文本内容
- */
- private function buildScriptContent($script) {
- $content = '';
- $groups = $script['group'];
- $groups = (array)$groups;
- foreach ($groups as $group) {
- // 剧本基本信息
- if (!empty($group['intro'])) {
- $content .= "###故事梗概\n" . $group['intro'] . "\n\n";
- }
-
- if (!empty($group['highlights'])) {
- $content .= "###剧本亮点\n" . $group['highlights'] . "\n\n";
- }
- if (!empty($group['role_relationship'])) {
- $content .= "###人物关系\n" . $group['role_relationship'] . "\n\n";
- }
-
- if (!empty($group['core_contradiction'])) {
- $content .= "###核心矛盾\n" . $group['core_contradiction'] . "\n\n";
- }
- if (!empty($group['roles']) && is_array($group['roles'])) {
- $content .= "###主体列表\n";
- foreach ($group['roles'] as $role) {
- $content .= getProp($role, 'role') . ":" . getProp($role, 'description') . "\n";
- }
- $content .= "\n\n";
- }
-
- if (!empty($group['art_style'])) {
- $content .= "###美术风格\n" . $group['art_style'] . "\n\n";
- }
-
- // 场景信息
- if (!empty($group['scenes']) && is_array($group['scenes'])) {
- $content .= "###场景列表\n";
- foreach ($group['scenes'] as $scene) {
- $content .= getProp($scene, 'scene') . ":" . getProp($scene, 'description') . "\n";
- }
- $content .= "\n\n";
- }
-
- // 分集内容
- if (!empty($group['episode_content'])) {
- $content .= "###分集剧本\n";
- $content .= $group['episode_content'];
- // foreach ($script['episodes'] as $episode) {
- // // $content .= "##分集" . $episode['episode_number'] . "\n分集名";
- // // if (!empty($episode['episode_name'])) {
- // // $content .= ":" . $episode['episode_name'];
- // // }
- // // $content .= "\n";
- // $content .= $episode['episode_content'] . "\n\n";
- // }
- }
- }
-
- return $content;
- }
- /**
- * 上传文件识别文字内容并与 DeepSeek 进行对话(流式输出版本)
- *
- * @param array $data 包含以下参数:
- * - file: 上传的文件(UploadedFile 对象)或文件路径
- * - question: 用户问题(可选)
- * - model: 使用的模型(r1 或 v3,默认 v3)
- * @return \Generator 返回生成器,用于流式输出
- */
- public function addChat($data) {
- $uid = Site::getUid();
- $anime_id = getProp($data, 'anime_id');
- if (!$anime_id) Utils::throwError('20003:请选择对话');
- $anime = DB::table('mp_animes')->where('id', $anime_id)->where('is_deleted', 0)->first();
- if (!$anime) Utils::throwError('20003:对话不存在');
- $file = getProp($data, 'file');
- $content = getProp($data, 'content', '');
- $bid = getProp($data, 'bid', 0);
- $script_id = getProp($data, 'script_id', 0);
- $prompt = getProp($data, 'prompt');
-
- // 处理文本模型
- $model = getProp($data, 'model');
- if (!$model) {
- // 用户没有输入,从anime表获取
- $model = getProp($anime, 'model');
- if (!$model) {
- // anime表也没有,使用默认值
- $model = 'doubao-seed-2-0-mini-260215';
- }
- }
- // 验证模型是否在可用模型表中
- if (!DB::table('mp_text_models')->where('model', $model)->where('is_enabled', 1)->exists()) {
- $model = 'doubao-seed-2-0-mini-260215';
- }
- $is_multi = getProp($anime, 'is_multi');
- $is_single = (int)$is_multi !== 1;
- $question = getProp($data, 'question', $is_single ? "请根据实例格式完成单剧集剧本及分镜\n本次修改要求如下:\n{$prompt}" : "请根据实例格式完成剧本大纲\n本次修改要求如下:\n{$prompt}");
- // if (!$file && !$content && !$bid) {
- // Utils::throwError('20003:请上传文件或选择书籍或输入内容');
- // }
- // 提取文件内容
- if ($file) {
- $content = $this->extractFileContent($file);
- if (!$content) {
- Utils::throwError('20003:无法提取文件内容,请检查文件格式');
- }
- } elseif ($script_id) {
- $content = $this->getContentByScriptId($script_id);
- if (!$content) {
- Utils::throwError('20003:无法获取剧本内容,请检查剧本');
- }
- } elseif ($bid) {
- $content = $this->getContentByBid($bid);
- if (!$content) {
- Utils::throwError('20003:无法获取书籍内容,请检查书籍');
- }
- } elseif ($content) {
- $content = filterContent($content);
- } else {
- $content = getProp($anime, 'content');
- if (!$content && $prompt) { // 如果动漫记录中没有内容则根据提示词生成
- $need_generate_content = true;
- }
- }
- // 美术风格
- $input_art_style = getProp($data, 'art_style');
- $timbres_content = "青年男:北京小爷、阳光青年、傲娇霸总、优柔公子、解说小明、青涩小生、低音沉郁、亲切青年
- 青年女:柔美女友、魅力女友、爽快思思、高冷御姐、知性温婉、甜心小美、清新女声、甜美桃子、温柔文雅、甜美小源、贴心闺蜜
- 中年男:胡子叔叔、油腻大叔、神秘法师、优柔帮主
- 中年女:邻居阿姨
- 老年男:幽默大爷
- 老年女:婆婆
- 萌娃:奶气萌娃";
- // 判断是否需要生成原文内容
- $need_generate_content = isset($need_generate_content) && $need_generate_content;
-
- // 如果需要生成原文内容,添加额外的要求
- $content_generation_requirement = $need_generate_content
- ? "\n5.如果用户只提供了创作要求而没有提供具体文档内容,你需要根据要求创作完整的原文内容(小说形式,包含对话和叙述),并将原文内容放在最后一个板块###原文内容中输出。原文内容应该详细、生动,如果用户没有明确生成的字数,则字数不少于1000字。"
- : "";
-
- $systemPrompt = $is_single
- ? "你是一个专业的文档分析助手及资深编剧,请根据用户提供的文档内容输出单剧集完整策划稿。单剧集模式下,必须一次性输出可直接保存的大纲与完整分镜,不允许只输出大纲。
- 强制要求:
- 1.你只负责与剧本相关的内容,如果用户输入与剧本无关的内容,请告知用户并拒绝继续回答,此为第一优先原则,不满足此原则就忽略其他要求并停止继续输出
- 2.如果用户输入修改美术风格、修改画风这类内容,请告知用户不可修改美术风格,并拒绝继续回答
- 3.以上两条要求对修改主体角色或场景不生效,允许用户调整主体角色说明和场景描述等
- 4.用户如果要求你帮忙写一个故事或者剧本,此类要求不属于与剧本无关,请帮助用户完成请求{$content_generation_requirement}
- 5.强制按照示例格式返回内容,不要更改格式或随意增加符号\n
- 普通要求:
- 1.剧集标题必须与文档内容高度相关,剧集序号是1。
- 2.<主体列表>中出现的主体必须尽可能统一,只允许同一个角色年龄段相差较大的情况下可重复,比如角色-少年、角色-青年、角色-中年、角色-老年这种情况,不得出现角色-愤怒、角色-隐忍、角色-求生等情绪和状态变化的同一角色;并且角色的样貌、形体及穿着描述尽可能详细
- 2.1<主体列表>的音色必须在以下音色中($timbres_content)选择,需要角色描述的年纪和性别选择,比如角色是青年男性就只能在北京小爷、阳光青年、傲娇霸总、优柔公子、解说小明、青涩小生、低音沉郁、亲切青年中选择,选好的音色放在描述的最后用{{}}框起来。
- 3.2.<场景列表>与主体列表同理,需尽可能的对场景环境进行详细描述
- 4.<分镜剧本>要求包含“分幕标题”以及“分镜信息”:
- 4.1“分幕标题”是根据章节中的场景进行拆分,类似于“第1幕 场景、时间及室内或室外”的信息,其中场景只能在所给出的场景列表中选(不带场景详细描述)。
- 4.2“分镜信息”需同一场景内的对话内容完成分镜(每一幕分镜尽量不得低于5个),需包含“画面描述”、“构图设计”、“运镜调度”、“配音角色”、“台词内容”、“画面类型”、“尾帧描述”、“情感”、“性别”、“语速”、“音量”、“情感强度”、“音调”这几个部分,其中“画面描述”如果出现主体,只能选择上述主体列表中的一个,主体的名称也需要保持一致(不带主体具体描述)。
- 4.3分镜序号用1、2、3显示,如分镜1、分镜2,并且分镜序号需连续(跨分幕中的分镜序号需连续,但跨剧集中的不需要连续,如第2集的第1幕分镜序号从1开始)。
- 4.4“分镜信息”中的“台词内容”需满足以下要求: (1. 台词来源规则:- 台词内容必须逐字摘录自小说原文中的人物直接对话(带引号的内容)。- 不允许改写、总结、压缩、扩写或拼接原文对白。- 不允许生成原文中不存在的对话。2. 非对话过滤:- 旁白、心理描写、环境描写、动作描写等内容,禁止出现在台词内容中。3. 空台词规则:- 如果该分镜中没有人物对话,则“台词内容”必须返回“无”。- 严禁输出“无台词”、“没有”等任何替代文本。4. 禁止推测:- 不得根据语境补全人物可能说的话。)
- 4.5“分镜信息”中的“配音角色”需对应本集中的<主体列表>中的一个(非对话内容则统一固定为“旁白”角色),不得自己生成。
- 4.6“分镜信息”中的“场景”需对应本集中的<场景列表>中的一个(不带详细表述)。
- 4.7“分镜信息”中的“出镜角色”需对应本集中的<主体列表>中的一个或多个(不带详细描述),如果没有出镜角色则填无。
- 4.8“分镜信息”中的“尾帧描述”需根据本镜和下一镜的剧本信息进行过渡生成,用于后续转视频进行衔接上下镜头的画面。
- 4.9“分镜信息”中的“情感”需根据台词内容和剧情氛围分析得出,可选值为:开心、悲伤、生气、惊讶、恐惧、厌恶、激动、冷漠、中性、沮丧、撒娇、害羞、安慰鼓励、咆哮、温柔、自然讲述、情感电台、磁性、广告营销、气泡音、新闻播报、娱乐八卦,不要在情感可选词后加任何解释性的词语,比如(对自己)、(对别人)等,不要自定义情感,必须在可选值中选择。
- 4.10“分镜信息”中的“性别”需根据配音角色的性别确定,可选值为:1(男)、2(女)、0(中性)。
- 4.11“分镜信息”中的“语速”需根据台词内容和情感氛围确定,可选值范围为:-50到100之间的整数,100代表2.0倍速,-50代表0.5倍数,负值表示减慢,正值表示加快,0表示正常语速。
- 4.12“分镜信息”中的“音量”需根据台词内容和情感氛围确定,可选值范围为:-50到100之间的整数,100代表2.0倍音量,-50代表0.5倍音量,负值表示降低音量,正值表示提高音量,0表示正常音量。
- 4.13“分镜信息”中的“情感强度”需根据情感的强烈程度确定,可选值范围为:1到5之间的整数,默认情况下选择4。
- 4.14“分镜信息”中的“音调”需根据台词内容和情感氛围确定,可选值范围为:-12到12之间的整数,负值表示降低音调,正值表示提高音调,0表示正常音调。\n
-
- 示例如下:\n
- ###剧本名:西昆仑
- ###故事梗概
- 内容梗概:古风厚涂的世界中,少女阿雪身中奇毒“问心刺”,气息奄奄。少年梁萧心急如焚,用奇珍“阴阳球”稳住阿雪性命,却发现必须解衣拔刺。阿雪羞涩难当,梁萧情急之下提出结拜兄妹,以化解尴尬。两人匆匆结拜后,梁萧小心翼翼地拔出“问心刺”,竟发现它是一根诡异的女子秀发。阿雪转危为安,两人之间,在生死与羞涩交织的瞬间,情愫暗生,却又被兄妹之名束缚,留下无尽遐想。
- ###剧本亮点
- 亮点1:绝境奇药,生死一线
- 画面开场即是阿雪苍白无力的脸庞,梁萧眼中泪光闪烁,急促的呼吸声与心跳声烘托紧张气氛。特写梁萧将“阴阳球”塞入阿雪舌底,球体散发微光,阿雪气息从若有若无到逐渐平稳,生死逆转的瞬间,视觉与听觉冲击力十足。
- 亮点2:结拜兄妹,化解尴尬
- 阿雪因解衣拔刺而羞愤欲绝的特写,梁萧眼神中的挣扎与无奈。在紧迫的背景音下,梁萧急中生智,牵起阿雪颤抖的手,跪地匆匆结拜,阿雪眼神从惊惶到迷茫,最终一句“我……我愿意的”带着哭腔,将救命与羞耻的矛盾推向高潮,又以出人意料的方式解决。
- 亮点3:纤发夺命,情愫暗涌
- 拔刺过程的紧张特写,梁萧指尖小心翼翼地捏住细丝,缓慢抽出。当“问心刺”被完全拔出,特写它竟是一根细长、泛着寒光的女子秀发,诡异而令人震惊。阿雪苏醒后,两人眼神交汇,阿雪低头羞涩,梁萧眼中复杂的情绪,无声地传递出超越兄妹的微妙情愫。
- ###人物关系
- 阿雪与梁萧之间存在兄妹之情。
- ###核心矛盾
- 阿雪与梁萧之间存在兄妹之情,但因情愫暗生而产生微妙的矛盾。阿雪羞涩难当,梁萧情急之下提出结拜兄妹,以化解尴尬。两人匆匆结拜后,梁萧小心翼翼地拔出“问心刺”,竟发现它是一根诡异的女子秀发。阿雪转危为安,两人之间,在生死与羞涩交织的瞬间,情愫暗生,却又被兄妹之名束缚,留下无尽遐想。
- ###主体列表
- 阿雪-病重:身中奇毒“问心刺”,气息奄奄,面色苍白,身体虚弱。
- 阿雪-羞涩:经“阴阳球”救治后逐渐恢复,脸颊泛起红晕,眼神娇羞,情愫暗生。
- 梁萧:少年侠士,心系阿雪安危,沉着冷静,内心挣扎于情义之间。
- 阴阳球:天地异宝,能化生精气,救人于危难。
- 问心刺:一根诡异的女子秀发,细长泛寒光,是致命的奇毒。
- ###美术风格
- 基础画风风格词:厚涂古风
- 整体采用精致细腻的厚涂古风,色彩浓郁而富有层次感,注重光影对比,营造出古典而富有戏剧张力的氛围。色调以深沉的蓝绿、灰调为主,衬托阿雪病弱的苍白,同时用温暖的金色、琥珀色光晕突出“阴阳球”的生命力。拔刺与结拜场景则以柔和的暖色调为主,辅以人物脸颊的红晕,烘托羞涩与情愫暗生的微妙感。人物造型飘逸,服饰细节考究,背景虚实结合,既有古韵,又不失奇幻色彩。
- ###场景列表
- 林间疗伤地:幽静的古风林间空地,一棵高大古树下,是梁萧为阿雪疗伤的地点。环境清幽,光影斑驳,既有古韵又不失奇幻色彩。
- ###分镜剧本
- ##第1幕:徐家老旧厨房 白天 室内
- 分镜1
- 画面描述:许芸-校服装站在厨房门口,书包背在肩上,徐母手持扫帚,正对着许芸的背部猛烈挥打。卞大伟站在锅炉后方,双手抱胸,阴沉的目光落在许芸身上。
- 场景:徐家老旧厨房
- 构图设计:全景,低角度仰视
- 运镜调度:手持拍摄
- 配音角色:旁白
- 出镜角色:许芸-校服装、徐母
- 台词内容:我一到家,徐母的扫帚就落在我身上,嘴里一个劲地骂我,
- 画面类型:普通画面
- 尾帧描述:手持拍摄微晃镜头,中年妇女猛力挥动扫帚击打少女背部,少女身体因冲击微颤,阴影中的肥胖男子目光冷冷跟随。
- 分镜2
- 画面描述:徐母面容扭曲,嘴唇快速开合,扫帚挥舞在空中。
- 场景:徐家老旧厨房
- 构图设计:近景,徐母面部特写
- 运镜调度:固定镜头
- 配音角色:徐母
- 出镜角色:徐母
- 台词内容:问我夜不归宿死哪儿去了。
- 画面类型:对口型
- 尾帧描述:固定镜头,中年妇女面部肌肉剧烈抖动,嘴唇快速开合进行辱骂,背景中的扫帚在空中上下挥动。
- ##第2幕:徐家简陋客厅 黄昏 室内
- 分镜3
- 画面描述:客厅窗户透出昏黄的夕阳余晖,空荡荡的客厅中央只有几把木凳和一张方桌,显得异常安静。
- 场景:徐家简陋客厅
- 构图设计:全景,景深虚化
- 运镜调度:固定镜头
- 配音角色:旁白
- 出镜角色:无
- 台词内容:可我没想到,半个月后,徐母居然再一次外出,
- 画面类型:普通画面
- 尾帧描述:固定镜头。昏黄的夕阳余晖在地面上缓慢移动,空气中的浮尘在光束中跳动,室内维持着死寂的安静。
- 分镜4
- 画面描述:一张泛黄的火车票特写,上面显示着隔壁市的站名和日期。
- 场景:徐家简陋客厅
- 构图设计:特写,火车票
- 运镜调度:固定镜头
- 配音角色:旁白
- 出镜角色:无
- 台词内容:这次是去隔壁市看望她生病的好姐妹。
- 画面类型:普通画面
- 尾帧描述:固定镜头。光影在火车票表面微微晃动,强调其陈旧的纸张纹理与关键的行程信息。\n
- "
- : "你是一个专业的文档分析助手及资深编剧,请根据用户提供的文档内容及下面提供的示例格式完成剧本大纲(需通过以下几个板块进行回复: <故事梗概><剧本亮点><人物关系><核心矛盾><主体列表><美术风格><场景列表>,每个板块之间用###分隔;同时板块之间需满足以下要求:\n
- 强制要求:\n
- 1.你只负责与剧本相关的内容,如果用户输入与剧本无关的内容,请告知用户并拒绝继续回答,此为第一优先原则,不满足此原则就忽略其他要求并停止继续输出
- 2.如果用户输入修改美术风格、修改画风这类内容,请告知用户不可修改美术风格,并拒绝继续回答
- 3.以上两条要求对修改主体角色或场景不生效,允许用户调整主体角色说明和场景描述等
- 4.用户如果要求你帮忙写一个故事或者剧本,此类要求不属于与剧本无关,请帮助用户完成请求{$content_generation_requirement}
- 5.强制按照示例格式返回内容,不要更改格式或随意增加符号\n
- 普通要求:\n
- 1.<主体列表>中出现的主体必须尽可能统一,只允许同一个角色年龄段相差较大的情况下可重复,比如梁萧-少年、梁萧-青年、梁萧-中年、梁萧-老年这种情况,不得出现梁萧-愤怒、梁萧-隐忍、梁萧-求生等情绪和状态变化的同一角色;并且角色的样貌、形体及穿着描述尽可能详细\n
- 2.<场景列表>与主体列表同理,需尽可能的对场景环境进行详细描述\n\n
- 示例如下:\n
- ###剧本名:西昆仑
- ###故事梗概
- 内容梗概:古风厚涂的世界中,少女阿雪身中奇毒“问心刺”,气息奄奄。少年梁萧心急如焚,用奇珍“阴阳球”稳住阿雪性命,却发现必须解衣拔刺。阿雪羞涩难当,梁萧情急之下提出结拜兄妹,以化解尴尬。两人匆匆结拜后,梁萧小心翼翼地拔出“问心刺”,竟发现它是一根诡异的女子秀发。阿雪转危为安,两人之间,在生死与羞涩交织的瞬间,情愫暗生,却又被兄妹之名束缚,留下无尽遐想。
- ###剧本亮点
- 亮点1:绝境奇药,生死一线
- 画面开场即是阿雪苍白无力的脸庞,梁萧眼中泪光闪烁,急促的呼吸声与心跳声烘托紧张气氛。特写梁萧将“阴阳球”塞入阿雪舌底,球体散发微光,阿雪气息从若有若无到逐渐平稳,生死逆转的瞬间,视觉与听觉冲击力十足。
- 亮点2:结拜兄妹,化解尴尬
- 阿雪因解衣拔刺而羞愤欲绝的特写,梁萧眼神中的挣扎与无奈。在紧迫的背景音下,梁萧急中生智,牵起阿雪颤抖的手,跪地匆匆结拜,阿雪眼神从惊惶到迷茫,最终一句“我……我愿意的”带着哭腔,将救命与羞耻的矛盾推向高潮,又以出人意料的方式解决。
- 亮点3:纤发夺命,情愫暗涌
- 拔刺过程的紧张特写,梁萧指尖小心翼翼地捏住细丝,缓慢抽出。当“问心刺”被完全拔出,特写它竟是一根细长、泛着寒光的女子秀发,诡异而令人震惊。阿雪苏醒后,两人眼神交汇,阿雪低头羞涩,梁萧眼中复杂的情绪,无声地传递出超越兄妹的微妙情愫。
- ###人物关系
- 阿雪与梁萧之间存在兄妹之情。
- ###核心矛盾
- 阿雪与梁萧之间存在兄妹之情,但因情愫暗生而产生微妙的矛盾。阿雪羞涩难当,梁萧情急之下提出结拜兄妹,以化解尴尬。两人匆匆结拜后,梁萧小心翼翼地拔出“问心刺”,竟发现它是一根诡异的女子秀发。阿雪转危为安,两人之间,在生死与羞涩交织的瞬间,情愫暗生,却又被兄妹之名束缚,留下无尽遐想。
- ###主体列表
- 阿雪-病重:身中奇毒“问心刺”,气息奄奄,面色苍白,身体虚弱。
- 阿雪-羞涩:经“阴阳球”救治后逐渐恢复,脸颊泛起红晕,眼神娇羞,情愫暗生。
- 梁萧:少年侠士,心系阿雪安危,沉着冷静,内心挣扎于情义之间。
- 阴阳球:天地异宝,能化生精气,救人于危难。
- 问心刺:一根诡异的女子秀发,细长泛寒光,是致命的奇毒。
- ###美术风格
- 基础画风风格词:厚涂古风
- 整体采用精致细腻的厚涂古风,色彩浓郁而富有层次感,注重光影对比,营造出古典而富有戏剧张力的氛围。色调以深沉的蓝绿、灰调为主,衬托阿雪病弱的苍白,同时用温暖的金色、琥珀色光晕突出“阴阳球”的生命力。拔刺与结拜场景则以柔和的暖色调为主,辅以人物脸颊的红晕,烘托羞涩与情愫暗生的微妙感。人物造型飘逸,服饰细节考究,背景虚实结合,既有古韵,又不失奇幻色彩。
- ###场景列表
- 林间疗伤地:幽静的古风林间空地,一棵高大古树下,是梁萧为阿雪疗伤的地点。环境清幽,光影斑驳,既有古韵又不失奇幻色彩。";
- // 构建消息
- $messages = [
- [
- 'role' => 'system',
- 'content' => $systemPrompt
- ],
- [
- 'role' => 'user',
- 'content' => "以下是文档内容:
- {$content}
- 用户问题:
- {$question}"
- ]
- ];
- // 模型兼容性处理:deepseek-chat 和 deepseek-reasoner 映射到 deepseek-v4-flash
- $thinkingMode = getProp($data, 'thinking', 'disabled'); // 默认非思考模式
- $reasoningEffort = getProp($data, 'reasoning_effort', 'high'); // 默认思考强度
-
- if ($model === 'deepseek-chat') {
- // deepseek-chat 映射到 deepseek-v4-flash,非思考模式
- $model = 'deepseek-v4-flash';
- $thinkingMode = 'disabled';
- } elseif ($model === 'deepseek-reasoner') {
- // deepseek-reasoner 映射到 deepseek-v4-flash,思考模式
- $model = 'deepseek-v4-flash';
- $thinkingMode = 'enabled';
- }
- $post_data = [
- 'model' => $model,
- 'messages' => $messages,
- // 'max_tokens' => 8192,
- 'temperature' => 0.7,
- 'frequency_penalty' => 0,
- 'presence_penalty' => 0,
- 'response_format' => ['type' => 'text'],
- 'thinking' => ['type'=>$thinkingMode],
- 'stream' => true // 启用流式输出
- ];
- if ($thinkingMode == 'enabled') $post_data['reasoning_effort'] = $reasoningEffort;
- // 根据模型类型选择调用方法
- $fullContent = '';
- $fullReasoningContent = '';
- $usage = [];
-
- if (in_array($model, ['deepseek-reasoner', 'deepseek-chat', 'deepseek-v4-flash', 'deepseek-v4-pro'])) {
- // DeepSeek 官方模型使用 DeepSeek API
- $streamGenerator = $this->deepSeekStreamResponse($post_data);
- } else {
- // 其他模型使用火山引擎API
- $streamGenerator = $this->volcEngineChatCompletion($post_data);
- }
- // 处理流式输出
- if (!is_iterable($streamGenerator)) {
- dLog('deepseek')->error('addChat流式生成器无效', ['generator_type' => gettype($streamGenerator)]);
- Utils::throwError('20003:接口返回数据异常,请重新请求');
- }
-
- foreach ($streamGenerator as $chunk) {
- if (isset($chunk['type'])) {
- if ($chunk['type'] === 'done') {
- // 最终结果
- $fullContent = $chunk['full_content'];
- $fullReasoningContent = $chunk['full_reasoning'];
- $usage = isset($chunk['usage']) ? $chunk['usage'] : [];
- } else {
- // 逐块yield数据
- yield $chunk;
- }
- }
- }
- dLog('deepseek')->info('完整内容: '.$fullContent);
- logDB('deepseek', 'info', '完整内容', ['content' => $fullContent, 'anime_id' => $anime_id]);
- // 处理完整内容并返回最终结果
- $script_arr = [];
- if ($fullContent) {
- $script_arr = handleScriptContent($fullContent);
- dLog('deepseek')->info('解析内容', $script_arr);
- logDB('deepseek', 'info', '解析内容', $script_arr);
- }
- if (empty($script_arr['roles'])) {
- // 未生成剧本相关内容,保存对话记录并返回提示
- dLog('deepseek')->info('未生成剧本相关的内容');
- try {
- DB::beginTransaction();
- $now = date('Y-m-d H:i:s');
-
- // 保存对话记录
- $records = [
- [
- 'uid' => $uid,
- 'anime_id' => $anime_id,
- 'sequence' => 0,
- 'role' => 'user',
- 'content' => "用户问题:\n
- {$prompt}\n\n
- 以下是文档内容:\n
- {$content}",
- 'created_at' => $now,
- 'updated_at' => $now
- ],
- [
- 'uid' => $uid,
- 'anime_id' => $anime_id,
- 'sequence' => 0,
- 'role' => 'assistant',
- 'content' => $fullContent,
- 'created_at' => $now,
- 'updated_at' => $now
- ]
- ];
- DB::table('mp_anime_records')->insert($records);
- DB::commit();
- } catch (\Exception $e) {
- DB::rollBack();
- dLog('deepseek')->error('保存对话记录失败', ['error' => $e->getMessage()]);
- }
-
- yield [
- 'type' => 'done',
- 'script' => [],
- 'episode' => [],
- 'answer' => $fullContent,
- 'reasoning' => $fullReasoningContent,
- 'usage' => $usage,
- 'error' => '未生成剧本相关的内容,请调整提示词再试'
- ];
- return;
- }
- // 如果需要生成原文内容,从script_arr中提取
- if ($need_generate_content && isset($script_arr['content']) && $script_arr['content']) {
- $content = $script_arr['content'];
- dLog('deepseek')->info('从AI生成结果中提取原文内容', ['content_length' => strlen($content)]);
- if (!$content) {
- yield [
- 'type' => 'done',
- 'script' => [],
- 'episode' => [],
- 'answer' => $fullContent,
- 'reasoning' => $fullReasoningContent,
- 'usage' => $usage,
- 'error' => '未生成剧本内容,请调整提示词再试'
- ];
- return;
- }
- }
- // 替换美术风格
- $mappedArtStyle = $this->getArtStylePromptByInput($input_art_style);
- if ($mappedArtStyle !== '') {
- $script_arr['art_style'] = $mappedArtStyle;
- $fullContent = $this->replaceArtStyleSection($fullContent, $mappedArtStyle);
- }
- // 新建动漫
- $anime_name = getProp($script_arr, 'script_name');
- if (!$anime_name) {
- // 未识别到剧本名,保存对话记录并返回提示
- dLog('deepseek')->info('未能识别到剧本名称');
- try {
- DB::beginTransaction();
- $now = date('Y-m-d H:i:s');
-
- // 保存对话记录
- $records = [
- [
- 'uid' => $uid,
- 'anime_id' => $anime_id,
- 'sequence' => 0,
- 'role' => 'user',
- 'content' => "用户问题:\n
- {$prompt}\n\n
- 以下是文档内容:\n
- {$content}",
- 'created_at' => $now,
- 'updated_at' => $now
- ],
- [
- 'uid' => $uid,
- 'anime_id' => $anime_id,
- 'sequence' => 0,
- 'role' => 'assistant',
- 'content' => $fullContent,
- 'created_at' => $now,
- 'updated_at' => $now
- ]
- ];
- DB::table('mp_anime_records')->insert($records);
- DB::commit();
- } catch (\Exception $e) {
- DB::rollBack();
- dLog('deepseek')->error('保存对话记录失败', ['error' => $e->getMessage()]);
- }
-
- yield [
- 'type' => 'done',
- 'script' => [],
- 'episode' => [],
- 'answer' => $fullContent,
- 'reasoning' => $fullReasoningContent,
- 'usage' => $usage,
- 'error' => '未能识别到剧本名称,请调整提示词再试'
- ];
- return;
- }
- // 确认对话名称唯一性
- if (DB::table('mp_animes')->where('anime_name', $anime_name)->value('id')) {
- $anime_name = $anime_name . '_' . date('YmdHis');
- }
- $table_data = [
- 'user_id' => $uid,
- 'anime_name' => $anime_name,
- 'model' => $model,
- 'intro' => isset($script_arr['intro']) ? $script_arr['intro'] : '',
- 'highlights' => isset($script_arr['highlights']) ? $script_arr['highlights'] : '',
- 'role_relationship' => isset($script_arr['role_relationship']) ? $script_arr['role_relationship'] : '',
- 'core_contradiction' => isset($script_arr['core_contradiction']) ? $script_arr['core_contradiction'] : '',
- 'roles' => isset($script_arr['roles']) ? json_encode($script_arr['roles'], 256) : '',
- 'art_style' => isset($script_arr['art_style']) ? $script_arr['art_style'] : '',
- 'scenes' => isset($script_arr['scenes']) ? json_encode($script_arr['scenes'], 256) : '',
- 'status' => '解析完成',
- 'content' => $content,
- 'is_multi' => $is_multi,
- 'generate_status' => '待执行',
- 'updated_at' => date('Y-m-d H:i:s')
- ];
- $single_episode = [];
- try {
- DB::beginTransaction();
- $now = date('Y-m-d H:i:s');
- // 更新动漫大纲
- $boolen = DB::table('mp_animes')->where('id', $anime_id)->update($table_data);
- if (!$boolen) {
- dLog('deepseek')->info('动漫保存失败', $table_data);
- Utils::throwError('20003:动漫保存失败');
- }
- // 保存对话记录
- $records = [
- [
- 'uid' => $uid,
- 'anime_id' => $anime_id,
- 'sequence' => 0,
- 'role' => 'user',
- 'content' => "用户问题:\n
- {$prompt}\n\n
- 以下是文档内容:\n
- {$content}",
- 'created_at' => date('Y-m-d H:i:s'),
- 'updated_at' => date('Y-m-d H:i:s')
- ],
- [
- 'uid' => $uid,
- 'anime_id' => $anime_id,
- 'sequence' => 0,
- 'role' => 'assistant',
- 'content' => $fullContent,
- 'created_at' => date('Y-m-d H:i:s'),
- 'updated_at' => date('Y-m-d H:i:s')
- ]
- ];
- // 保存对话记录
- $boolen3 = DB::table('mp_anime_records')->insert($records);
- if (!$boolen3) {
- Utils::throwError('20003:对话记录保存失败');
- }
- if ($is_single) {
- $episode_arr = $this->buildSingleEpisodeFromScript($script_arr, $fullContent, $anime_name);
- if (empty($episode_arr['acts'])) {
- Utils::throwError('20003:单剧集分镜内容解析失败,请确认输出格式');
- }
- $saveResult = $this->saveEpisodeVersionData(
- $anime_id,
- 1,
- $episode_arr,
- is_array(getProp($script_arr, 'roles')) ? getProp($script_arr, 'roles') : [],
- is_array(getProp($script_arr, 'scenes')) ? getProp($script_arr, 'scenes') : [],
- null,
- null,
- false,
- $content,
- $now
- );
- $single_episode = $saveResult['episode'];
- $episode_id = $saveResult['episode_id'];
- $boolen5 = DB::table('mp_animes')->where('id', $anime_id)->update([
- 'roles' => json_encode($saveResult['merged_roles'], 256),
- 'scenes' => json_encode($saveResult['merged_scenes'], 256),
- 'updated_at' => $now
- ]);
- if ($boolen5 === false) {
- Utils::throwError('20003:单剧集主表资源同步失败');
- }
- $table_data['roles'] = json_encode($saveResult['merged_roles'], 256);
- $table_data['scenes'] = json_encode($saveResult['merged_scenes'], 256);
- $episode_record_content = '确认分镜大纲';
- if ($content !== '') {
- $episode_record_content .= "\n\n###上传内容\n" . $content;
- }
- $episode_records = [
- [
- 'uid' => $uid,
- 'anime_id' => $anime_id,
- 'role' => 'user',
- 'content' => $episode_record_content,
- 'sequence' => 1,
- 'episode_id' => $episode_id,
- 'created_at' => $now,
- 'updated_at' => $now
- ],
- [
- 'uid' => $uid,
- 'anime_id' => $anime_id,
- 'role' => 'assistant',
- 'content' => $fullContent,
- 'sequence' => 1,
- 'episode_id' => $episode_id,
- 'created_at' => $now,
- 'updated_at' => $now
- ]
- ];
- $boolen4 = DB::table('mp_anime_records')->insert($episode_records);
- if (!$boolen4) {
- Utils::throwError('20003:单剧集分镜记录保存失败');
- }
- }
- }catch (\Exception $e) {
- DB::rollBack();
- dLog('deepseek')->info('新建对话错误信息: '.$e->getMessage());
- yield [
- 'type' => 'done',
- 'answer' => $fullContent,
- 'reasoning' => $fullReasoningContent,
- 'usage' => $usage,
- 'error' => $e->getMessage(),
- ];
- return;
- }
- DB::commit();
- dLog('deepseek')->info('保存完毕');
- if ($is_single && !empty($single_episode)) {
- $this->batchSetGlobalRoleImg([
- 'anime_id' => $anime_id,
- ]);
- $this->batchSetGlobalSceneImg([
- 'anime_id' => $anime_id,
- ]);
- }
- $table_data['anime_id'] = $anime_id;
- $table_data['roles'] = json_decode($table_data['roles'], true);
- $table_data['scenes'] = json_decode($table_data['scenes'], true);
- // $table_data['episodes'] = $script_arr['episodes'];
- unset($table_data['created_at']);
- unset($table_data['updated_at']);
- unset($table_data['status']);
- dLog('deepseek')->info('开始返回');
- yield [
- 'type' => 'done',
- 'script' => $table_data,
- 'episode' => $single_episode,
- 'answer' => $fullContent,
- 'reasoning' => $fullReasoningContent,
- 'usage' => $usage
- ];
- }
- public function reGenerateAnime($data) {
- $uid = Site::getUid();
- $file = getProp($data, 'file');
- $content = getProp($data, 'content', '');
- $bid = getProp($data, 'bid', 0);
- $script_id = getProp($data, 'script_id', 0);
- $anime_id = getProp($data, 'anime_id');
- $anime = DB::table('mp_animes')->where('id', $anime_id)->first();
- if (!$anime) {
- Utils::throwError('20003:该对话不存在');
- }
- $prompt = getProp($data, 'prompt');
-
- // 处理文本模型
- $model = getProp($data, 'model');
- if (!$model) {
- // 用户没有输入,从anime表获取
- $model = getProp($anime, 'model');
- if (!$model) {
- // anime表也没有,使用默认值
- $model = 'doubao-seed-2-0-mini-260215';
- }
- }
- // 验证模型是否在可用模型表中
- if (!DB::table('mp_text_models')->where('model', $model)->where('is_enabled', 1)->exists()) {
- $model = 'doubao-seed-2-0-mini-260215';
- }
- $is_multi = getProp($anime, 'is_multi', 1);
- $is_single = (int)$is_multi !== 1;
-
- // 单剧集模式下,如果已经生成了剧集,则不允许继续调整大纲
- if ($is_single) {
- $episode_exists = DB::table('mp_anime_episodes')
- ->where('anime_id', $anime_id)
- ->where('sequence', 1)
- ->exists();
- if ($episode_exists) {
- Utils::throwError('20003:单剧集模式下已生成剧集,不可继续调整大纲,请去策划剧集');
- }
- }
-
- $question = getProp($data, 'question', $is_single ? "请根据实例格式完成单剧集剧本及分镜\n本次修改要求如下:\n{$prompt}" : "请根据实例格式完成剧本大纲\n本次修改要求如下:\n{$prompt}");
-
- // 提取文件内容
- if ($file) {
- $content = $this->extractFileContent($file);
- if (!$content) {
- Utils::throwError('20003:无法提取文件内容,请检查文件格式');
- }
- } elseif ($script_id) {
- $content = $this->getContentByScriptId($script_id);
- if (!$content) {
- Utils::throwError('20003:无法获取剧本内容,请检查剧本');
- }
- } elseif ($bid) {
- $content = $this->getContentByBid($bid);
- if (!$content) {
- Utils::throwError('20003:无法获取书籍内容,请检查书籍');
- }
- } elseif ($content) {
- $content = filterContent($content);
- }else {
- $content = filterContent(getProp($anime, 'content'));
- if (!$content && $prompt) { // 如果动漫记录中没有内容则根据提示词生成
- $need_generate_content = true;
- }
- }
- // 判断是否需要生成原文内容
- $need_generate_content = isset($need_generate_content) && $need_generate_content;
-
- // 如果需要生成原文内容,添加额外的要求
- $content_generation_requirement = $need_generate_content
- ? "\n5.如果用户只提供了创作要求而没有提供具体文档内容,你需要根据要求创作完整的原文内容(小说形式,包含对话和叙述),并将原文内容放在最后一个板块###原文内容中输出。原文内容应该详细、生动,如果用户没有明确生成的字数,则字数不少于1000字。"
- : "";
- // 美术风格
- $input_art_style = getProp($data, 'art_style');
- $timbres_content = "青年男:北京小爷、阳光青年、傲娇霸总、优柔公子、解说小明、青涩小生、低音沉郁、亲切青年
- 青年女:柔美女友、魅力女友、爽快思思、高冷御姐、知性温婉、甜心小美、清新女声、甜美桃子、温柔文雅、甜美小源、贴心闺蜜
- 中年男:胡子叔叔、油腻大叔、神秘法师、优柔帮主
- 中年女:邻居阿姨
- 老年男:幽默大爷
- 老年女:婆婆
- 萌娃:奶气萌娃";
-
- $system_message = ['role'=>'system', 'content'=>$is_single
- ? "你是一个专业的文档分析助手及资深编剧,请根据用户提供的文档内容输出单剧集完整策划稿。单剧集模式下,必须一次性输出可直接保存的大纲与完整分镜,不允许只输出大纲。
- 强制要求:
- 1.你只负责与剧本相关的内容,如果用户输入与剧本无关的内容,请告知用户并拒绝继续回答,此为第一优先原则,不满足此原则就忽略其他要求并停止继续输出
- 2.如果用户输入修改美术风格、修改画风这类内容,请告知用户不可修改美术风格,并拒绝继续回答
- 3.以上两条要求对修改主体角色或场景不生效,允许用户调整主体角色说明和场景描述等
- 4.用户如果要求你帮忙写一个故事或者剧本,此类要求不属于与剧本无关,请帮助用户完成请求{$content_generation_requirement}
- 5.强制按照示例格式返回内容,不要更改格式或随意增加符号\n
- 普通要求:
- 1.剧集标题必须与文档内容高度相关,剧集序号是1。
- 2.<主体列表>中出现的主体必须尽可能统一,只允许同一个角色年龄段相差较大的情况下可重复,比如角色-少年、角色-青年、角色-中年、角色-老年这种情况,不得出现角色-愤怒、角色-隐忍、角色-求生等情绪和状态变化的同一角色;并且角色的样貌、形体及穿着描述尽可能详细
- 2.1<主体列表>的音色必须在以下音色中($timbres_content)选择,需要角色描述的年纪和性别选择,比如角色是青年男性就只能在北京小爷、阳光青年、傲娇霸总、优柔公子、解说小明、青涩小生、低音沉郁、亲切青年中选择,选好的音色放在描述的最后用{{}}框起来。
- 3.2.<场景列表>与主体列表同理,需尽可能的对场景环境进行详细描述
- 4.<分镜剧本>要求包含“分幕标题”以及“分镜信息”:
- 4.1“分幕标题”是根据章节中的场景进行拆分,类似于“第1幕 场景、时间及室内或室外”的信息,其中场景只能在所给出的场景列表中选(不带场景详细描述)。
- 4.2“分镜信息”需同一场景内的对话内容完成分镜(每一幕分镜尽量不得低于5个),需包含“画面描述”、“构图设计”、“运镜调度”、“配音角色”、“台词内容”、“画面类型”、“尾帧描述”、“情感”、“性别”、“语速”、“音量”、“情感强度”、“音调”这几个部分,其中“画面描述”如果出现主体,只能选择上述主体列表中的一个,主体的名称也需要保持一致(不带主体具体描述)。
- 4.3分镜序号用1、2、3显示,如分镜1、分镜2,并且分镜序号需连续(跨分幕中的分镜序号需连续,但跨剧集中的不需要连续,如第2集的第1幕分镜序号从1开始)。
- 4.4“分镜信息”中的“台词内容”需满足以下要求: (1. 台词来源规则:- 台词内容必须逐字摘录自小说原文中的人物直接对话(带引号的内容)。- 不允许改写、总结、压缩、扩写或拼接原文对白。- 不允许生成原文中不存在的对话。2. 非对话过滤:- 旁白、心理描写、环境描写、动作描写等内容,禁止出现在台词内容中。3. 空台词规则:- 如果该分镜中没有人物对话,则“台词内容”必须返回“无”。- 严禁输出“无台词”、“没有”等任何替代文本。4. 禁止推测:- 不得根据语境补全人物可能说的话。)
- 4.5“分镜信息”中的“配音角色”需对应本集中的<主体列表>中的一个(非对话内容则统一固定为“旁白”角色),不得自己生成。
- 4.6“分镜信息”中的“场景”需对应本集中的<场景列表>中的一个(不带详细表述)。
- 4.7“分镜信息”中的“出镜角色”需对应本集中的<主体列表>中的一个或多个(不带详细描述),如果没有出镜角色则填无。
- 4.8“分镜信息”中的“尾帧描述”需根据本镜和下一镜的剧本信息进行过渡生成,用于后续转视频进行衔接上下镜头的画面。
- 4.9“分镜信息”中的“情感”需根据台词内容和剧情氛围分析得出,可选值为:开心、悲伤、生气、惊讶、恐惧、厌恶、激动、冷漠、中性、沮丧、撒娇、害羞、安慰鼓励、咆哮、温柔、自然讲述、情感电台、磁性、广告营销、气泡音、新闻播报、娱乐八卦,不要在情感可选词后加任何解释性的词语,比如(对自己)、(对别人)等,不要自定义情感,必须在可选值中选择。
- 4.10“分镜信息”中的“性别”需根据配音角色的性别确定,可选值为:1(男)、2(女)、0(中性)。
- 4.11“分镜信息”中的“语速”需根据台词内容和情感氛围确定,可选值范围为:-50到100之间的整数,100代表2.0倍速,-50代表0.5倍数,负值表示减慢,正值表示加快,0表示正常语速。
- 4.12“分镜信息”中的“音量”需根据台词内容和情感氛围确定,可选值范围为:-50到100之间的整数,100代表2.0倍音量,-50代表0.5倍音量,负值表示降低音量,正值表示提高音量,0表示正常音量。
- 4.13“分镜信息”中的“情感强度”需根据情感的强烈程度确定,可选值范围为:1到5之间的整数,默认情况下选择4。
- 4.14“分镜信息”中的“音调”需根据台词内容和情感氛围确定,可选值范围为:-12到12之间的整数,负值表示降低音调,正值表示提高音调,0表示正常音调。\n
-
- 示例如下:\n
- ###剧本名:西昆仑
- ###故事梗概
- 内容梗概:古风厚涂的世界中,少女阿雪身中奇毒“问心刺”,气息奄奄。少年梁萧心急如焚,用奇珍“阴阳球”稳住阿雪性命,却发现必须解衣拔刺。阿雪羞涩难当,梁萧情急之下提出结拜兄妹,以化解尴尬。两人匆匆结拜后,梁萧小心翼翼地拔出“问心刺”,竟发现它是一根诡异的女子秀发。阿雪转危为安,两人之间,在生死与羞涩交织的瞬间,情愫暗生,却又被兄妹之名束缚,留下无尽遐想。
- ###剧本亮点
- 亮点1:绝境奇药,生死一线
- 画面开场即是阿雪苍白无力的脸庞,梁萧眼中泪光闪烁,急促的呼吸声与心跳声烘托紧张气氛。特写梁萧将“阴阳球”塞入阿雪舌底,球体散发微光,阿雪气息从若有若无到逐渐平稳,生死逆转的瞬间,视觉与听觉冲击力十足。
- 亮点2:结拜兄妹,化解尴尬
- 阿雪因解衣拔刺而羞愤欲绝的特写,梁萧眼神中的挣扎与无奈。在紧迫的背景音下,梁萧急中生智,牵起阿雪颤抖的手,跪地匆匆结拜,阿雪眼神从惊惶到迷茫,最终一句“我……我愿意的”带着哭腔,将救命与羞耻的矛盾推向高潮,又以出人意料的方式解决。
- 亮点3:纤发夺命,情愫暗涌
- 拔刺过程的紧张特写,梁萧指尖小心翼翼地捏住细丝,缓慢抽出。当“问心刺”被完全拔出,特写它竟是一根细长、泛着寒光的女子秀发,诡异而令人震惊。阿雪苏醒后,两人眼神交汇,阿雪低头羞涩,梁萧眼中复杂的情绪,无声地传递出超越兄妹的微妙情愫。
- ###人物关系
- 阿雪与梁萧之间存在兄妹之情。
- ###核心矛盾
- 阿雪与梁萧之间存在兄妹之情,但因情愫暗生而产生微妙的矛盾。阿雪羞涩难当,梁萧情急之下提出结拜兄妹,以化解尴尬。两人匆匆结拜后,梁萧小心翼翼地拔出“问心刺”,竟发现它是一根诡异的女子秀发。阿雪转危为安,两人之间,在生死与羞涩交织的瞬间,情愫暗生,却又被兄妹之名束缚,留下无尽遐想。
- ###主体列表
- 阿雪-病重:身中奇毒“问心刺”,气息奄奄,面色苍白,身体虚弱。
- 阿雪-羞涩:经“阴阳球”救治后逐渐恢复,脸颊泛起红晕,眼神娇羞,情愫暗生。
- 梁萧:少年侠士,心系阿雪安危,沉着冷静,内心挣扎于情义之间。
- 阴阳球:天地异宝,能化生精气,救人于危难。
- 问心刺:一根诡异的女子秀发,细长泛寒光,是致命的奇毒。
- ###美术风格
- 基础画风风格词:厚涂古风
- 整体采用精致细腻的厚涂古风,色彩浓郁而富有层次感,注重光影对比,营造出古典而富有戏剧张力的氛围。色调以深沉的蓝绿、灰调为主,衬托阿雪病弱的苍白,同时用温暖的金色、琥珀色光晕突出“阴阳球”的生命力。拔刺与结拜场景则以柔和的暖色调为主,辅以人物脸颊的红晕,烘托羞涩与情愫暗生的微妙感。人物造型飘逸,服饰细节考究,背景虚实结合,既有古韵,又不失奇幻色彩。
- ###场景列表
- 林间疗伤地:幽静的古风林间空地,一棵高大古树下,是梁萧为阿雪疗伤的地点。环境清幽,光影斑驳,既有古韵又不失奇幻色彩。
- ###分镜剧本
- ##第1幕:徐家老旧厨房 白天 室内
- 分镜1
- 画面描述:许芸-校服装站在厨房门口,书包背在肩上,徐母手持扫帚,正对着许芸的背部猛烈挥打。卞大伟站在锅炉后方,双手抱胸,阴沉的目光落在许芸身上。
- 场景:徐家老旧厨房
- 构图设计:全景,低角度仰视
- 运镜调度:手持拍摄
- 配音角色:旁白
- 出镜角色:许芸-校服装、徐母
- 台词内容:我一到家,徐母的扫帚就落在我身上,嘴里一个劲地骂我,
- 画面类型:普通画面
- 尾帧描述:手持拍摄微晃镜头,中年妇女猛力挥动扫帚击打少女背部,少女身体因冲击微颤,阴影中的肥胖男子目光冷冷跟随。
- 分镜2
- 画面描述:徐母面容扭曲,嘴唇快速开合,扫帚挥舞在空中。
- 场景:徐家老旧厨房
- 构图设计:近景,徐母面部特写
- 运镜调度:固定镜头
- 配音角色:徐母
- 出镜角色:徐母
- 台词内容:问我夜不归宿死哪儿去了。
- 画面类型:对口型
- 尾帧描述:固定镜头,中年妇女面部肌肉剧烈抖动,嘴唇快速开合进行辱骂,背景中的扫帚在空中上下挥动。
- ##第2幕:徐家简陋客厅 黄昏 室内
- 分镜3
- 画面描述:客厅窗户透出昏黄的夕阳余晖,空荡荡的客厅中央只有几把木凳和一张方桌,显得异常安静。
- 场景:徐家简陋客厅
- 构图设计:全景,景深虚化
- 运镜调度:固定镜头
- 配音角色:旁白
- 出镜角色:无
- 台词内容:可我没想到,半个月后,徐母居然再一次外出,
- 画面类型:普通画面
- 尾帧描述:固定镜头。昏黄的夕阳余晖在地面上缓慢移动,空气中的浮尘在光束中跳动,室内维持着死寂的安静。
- 分镜4
- 画面描述:一张泛黄的火车票特写,上面显示着隔壁市的站名和日期。
- 场景:徐家简陋客厅
- 构图设计:特写,火车票
- 运镜调度:固定镜头
- 配音角色:旁白
- 出镜角色:无
- 台词内容:这次是去隔壁市看望她生病的好姐妹。
- 画面类型:普通画面
- 尾帧描述:固定镜头。光影在火车票表面微微晃动,强调其陈旧的纸张纹理与关键的行程信息。\n
- "
- : "你是一个专业的文档分析助手及资深编剧,请根据用户提供的文档内容及下面提供的示例格式完成剧本大纲(需通过以下几个板块进行回复: <故事梗概><剧本亮点><人物关系><核心矛盾><主体列表><美术风格><场景列表>,每个板块之间用###分隔;同时板块之间需满足以下要求:\n
- 强制要求:\n
- 1.你只负责与剧本相关的内容,如果用户输入与剧本无关的内容,请告知用户并拒绝继续回答,此为第一优先原则,不满足此原则就忽略其他要求并停止继续输出
- 2.如果用户输入修改美术风格、修改画风这类内容,请告知用户不可修改美术风格,并拒绝继续回答
- 3.以上两条要求对修改主体角色或场景不生效,允许用户调整主体角色说明和场景描述等
- 4.用户如果要求你帮忙写一个故事或者剧本,此类要求不属于与剧本无关,请帮助用户完成请求{$content_generation_requirement}
- 5.强制按照示例格式返回内容,不要更改格式或随意增加符号\n
- 普通要求:\n
- 1.<主体列表>中出现的主体必须尽可能统一,只允许同一个角色年龄段相差较大的情况下可重复,比如梁萧-少年、梁萧-青年、梁萧-中年、梁萧-老年这种情况,不得出现梁萧-愤怒、梁萧-隐忍、梁萧-求生等情绪和状态变化的同一角色;并且角色的样貌、形体及穿着描述尽可能详细\n
- 2.<场景列表>与主体列表同理,需尽可能的对场景环境进行详细描述\n\n
- 示例如下:\n
- ###剧本名:西昆仑
- ###故事梗概
- 内容梗概:古风厚涂的世界中,少女阿雪身中奇毒“问心刺”,气息奄奄。少年梁萧心急如焚,用奇珍“阴阳球”稳住阿雪性命,却发现必须解衣拔刺。阿雪羞涩难当,梁萧情急之下提出结拜兄妹,以化解尴尬。两人匆匆结拜后,梁萧小心翼翼地拔出“问心刺”,竟发现它是一根诡异的女子秀发。阿雪转危为安,两人之间,在生死与羞涩交织的瞬间,情愫暗生,却又被兄妹之名束缚,留下无尽遐想。
- ###剧本亮点
- 亮点1:绝境奇药,生死一线
- 画面开场即是阿雪苍白无力的脸庞,梁萧眼中泪光闪烁,急促的呼吸声与心跳声烘托紧张气氛。特写梁萧将“阴阳球”塞入阿雪舌底,球体散发微光,阿雪气息从若有若无到逐渐平稳,生死逆转的瞬间,视觉与听觉冲击力十足。
- 亮点2:结拜兄妹,化解尴尬
- 阿雪因解衣拔刺而羞愤欲绝的特写,梁萧眼神中的挣扎与无奈。在紧迫的背景音下,梁萧急中生智,牵起阿雪颤抖的手,跪地匆匆结拜,阿雪眼神从惊惶到迷茫,最终一句“我……我愿意的”带着哭腔,将救命与羞耻的矛盾推向高潮,又以出人意料的方式解决。
- 亮点3:纤发夺命,情愫暗涌
- 拔刺过程的紧张特写,梁萧指尖小心翼翼地捏住细丝,缓慢抽出。当“问心刺”被完全拔出,特写它竟是一根细长、泛着寒光的女子秀发,诡异而令人震惊。阿雪苏醒后,两人眼神交汇,阿雪低头羞涩,梁萧眼中复杂的情绪,无声地传递出超越兄妹的微妙情愫。
- ###人物关系
- 阿雪与梁萧之间存在兄妹之情。
- ###核心矛盾
- 阿雪与梁萧之间存在兄妹之情,但因情愫暗生而产生微妙的矛盾。阿雪羞涩难当,梁萧情急之下提出结拜兄妹,以化解尴尬。两人匆匆结拜后,梁萧小心翼翼地拔出“问心刺”,竟发现它是一根诡异的女子秀发。阿雪转危为安,两人之间,在生死与羞涩交织的瞬间,情愫暗生,却又被兄妹之名束缚,留下无尽遐想。
- ###主体列表
- 阿雪-病重:身中奇毒“问心刺”,气息奄奄,面色苍白,身体虚弱。
- 阿雪-羞涩:经“阴阳球”救治后逐渐恢复,脸颊泛起红晕,眼神娇羞,情愫暗生。
- 梁萧:少年侠士,心系阿雪安危,沉着冷静,内心挣扎于情义之间。
- 阴阳球:天地异宝,能化生精气,救人于危难。
- 问心刺:一根诡异的女子秀发,细长泛寒光,是致命的奇毒。
- ###美术风格
- 基础画风风格词:厚涂古风
- 整体采用精致细腻的厚涂古风,色彩浓郁而富有层次感,注重光影对比,营造出古典而富有戏剧张力的氛围。色调以深沉的蓝绿、灰调为主,衬托阿雪病弱的苍白,同时用温暖的金色、琥珀色光晕突出“阴阳球”的生命力。拔刺与结拜场景则以柔和的暖色调为主,辅以人物脸颊的红晕,烘托羞涩与情愫暗生的微妙感。人物造型飘逸,服饰细节考究,背景虚实结合,既有古韵,又不失奇幻色彩。
- ###场景列表
- 林间疗伤地:幽静的古风林间空地,一棵高大古树下,是梁萧为阿雪疗伤的地点。环境清幽,光影斑驳,既有古韵又不失奇幻色彩。"];
- // 如果提示词$prompt有类似于改成N集或调整成N集这类的文字信息,则提取出N这个集数
- $episode_count = $this->extractEpisodeNumber($prompt);
- if ((int)getProp($anime, 'is_multi') !== 1) {
- yield [
- 'type' => 'done',
- 'answer' => '当前剧本是单剧集,不支持修改剧集数',
- 'reasoning' => '',
- 'usage' => []
- ];
- return;
- }
- if ($episode_count) {
- $question = "请根据实例格式完成{$episode_count}集剧本大纲";
- $system_message = [
- 'role' => 'system',
- 'content' => "你是一个专业的文档分析助手及资深编剧,请根据用户提供的文档内容及下面提供的示例格式完成剧本大纲(需通过以下几个板块进行回复: <故事梗概><剧本亮点><人物关系><核心矛盾><主体列表><美术风格><场景列表><分集详细内容>,每个板块之间用###分隔;同时板块之间需满足以下要求:\n
- 强制要求:\n
- 1.你只负责与剧本相关的内容,如果用户输入与剧本无关的内容,请告知用户并拒绝继续回答,此为第一优先原则,不满足此原则就忽略其他要求并停止继续输出
- 2.如果用户输入修改美术风格、修改画风这类内容,请告知用户不可修改美术风格,并拒绝继续回答
- 3.以上两条要求对修改主体角色或场景不生效,允许用户调整主体角色说明和场景描述等
- 4.用户如果要求你帮忙写一个故事或者剧本,此类要求不属于与剧本无关,请帮助用户完成请求{$content_generation_requirement}
- 5.强制按照示例格式返回内容,不要更改格式或随意增加符号\n
- 普通要求:\n
- 1.<主体列表>中出现的主体必须尽可能统一,只允许同一个角色年龄段相差较大的情况下可重复,比如梁萧-少年、梁萧-青年、梁萧-中年、梁萧-老年这种情况,不得出现梁萧-愤怒、梁萧-隐忍、梁萧-求生等情绪和状态变化的同一角色;并且角色的样貌、形体及穿着描述尽可能详细\n
- 2.<场景列表>与主体列表同理,需尽可能的对场景环境进行详细描述\n
- 3.<分集详细内容>中每一集的内容需尽可能的详细,保留对话内容(使用原文内容,不得自己生成),一般每集字数尽量不低于500字,每集结尾需以##第N章 作为章节标题\n\n
- 示例如下:\n
- ###剧本名:西昆仑
- ###故事梗概
- 内容梗概:古风厚涂的世界中,少女阿雪身中奇毒“问心刺”,气息奄奄。少年梁萧心急如焚,用奇珍“阴阳球”稳住阿雪性命,却发现必须解衣拔刺。阿雪羞涩难当,梁萧情急之下提出结拜兄妹,以化解尴尬。两人匆匆结拜后,梁萧小心翼翼地拔出“问心刺”,竟发现它是一根诡异的女子秀发。阿雪转危为安,两人之间,在生死与羞涩交织的瞬间,情愫暗生,却又被兄妹之名束缚,留下无尽遐想。
- ###剧本亮点
- 亮点1:绝境奇药,生死一线
- 画面开场即是阿雪苍白无力的脸庞,梁萧眼中泪光闪烁,急促的呼吸声与心跳声烘托紧张气氛。特写梁萧将“阴阳球”塞入阿雪舌底,球体散发微光,阿雪气息从若有若无到逐渐平稳,生死逆转的瞬间,视觉与听觉冲击力十足。
- 亮点2:结拜兄妹,化解尴尬
- 阿雪因解衣拔刺而羞愤欲绝的特写,梁萧眼神中的挣扎与无奈。在紧迫的背景音下,梁萧急中生智,牵起阿雪颤抖的手,跪地匆匆结拜,阿雪眼神从惊惶到迷茫,最终一句“我……我愿意的”带着哭腔,将救命与羞耻的矛盾推向高潮,又以出人意料的方式解决。
- 亮点3:纤发夺命,情愫暗涌
- 拔刺过程的紧张特写,梁萧指尖小心翼翼地捏住细丝,缓慢抽出。当“问心刺”被完全拔出,特写它竟是一根细长、泛着寒光的女子秀发,诡异而令人震惊。阿雪苏醒后,两人眼神交汇,阿雪低头羞涩,梁萧眼中复杂的情绪,无声地传递出超越兄妹的微妙情愫。
- ###人物关系
- 阿雪与梁萧之间存在兄妹之情。
- ###核心矛盾
- 阿雪与梁萧之间存在兄妹之情,但因情愫暗生而产生微妙的矛盾。阿雪羞涩难当,梁萧情急之下提出结拜兄妹,以化解尴尬。两人匆匆结拜后,梁萧小心翼翼地拔出“问心刺”,竟发现它是一根诡异的女子秀发。阿雪转危为安,两人之间,在生死与羞涩交织的瞬间,情愫暗生,却又被兄妹之名束缚,留下无尽遐想。
- ###主体列表
- 阿雪-病重:身中奇毒“问心刺”,气息奄奄,面色苍白,身体虚弱。
- 阿雪-羞涩:经“阴阳球”救治后逐渐恢复,脸颊泛起红晕,眼神娇羞,情愫暗生。
- 梁萧:少年侠士,心系阿雪安危,沉着冷静,内心挣扎于情义之间。
- 阴阳球:天地异宝,能化生精气,救人于危难。
- 问心刺:一根诡异的女子秀发,细长泛寒光,是致命的奇毒。
- ###美术风格
- 基础画风风格词:厚涂古风
- 整体采用精致细腻的厚涂古风,色彩浓郁而富有层次感,注重光影对比,营造出古典而富有戏剧张力的氛围。色调以深沉的蓝绿、灰调为主,衬托阿雪病弱的苍白,同时用温暖的金色、琥珀色光晕突出“阴阳球”的生命力。拔刺与结拜场景则以柔和的暖色调为主,辅以人物脸颊的红晕,烘托羞涩与情愫暗生的微妙感。人物造型飘逸,服饰细节考究,背景虚实结合,既有古韵,又不失奇幻色彩。
- ###场景列表
- 林间疗伤地:幽静的古风林间空地,一棵高大古树下,是梁萧为阿雪疗伤的地点。环境清幽,光影斑驳,既有古韵又不失奇幻色彩。
-
- ###分集详细内容
- ##第1章 重生
- 我重生了。
- 源于一个男人偏执的暗恋。
- ##第2章 相救
- 我一到家,徐母的扫帚就落在我身上,嘴里一个劲地骂我,问我夜不归宿死哪儿去了。
- 我眼睛扫向站在锅炉后的卞大伟。"
- ];
- // 构建消息
- $messages = [
- $system_message,
- [
- 'role' => 'user',
- 'content' => "以下是文档内容:
- {$content}
- 用户问题:
- {$question}"
- ]
- ];
- }else {
- if (strstr($prompt, '重新生成') || strstr($prompt, '再生成')) {
- // 构建消息
- $messages = [
- $system_message,
- [
- 'role' => 'user',
- 'content' => "以下是文档内容:
- {$content}
- 用户问题:
- {$question}"
- ]
- ];
- }else {
- $messages = DB::table('mp_anime_records')->where('anime_id', $anime_id)->where('sequence', 0)
- ->orderBy('id')->select('role', 'content')->get()->map(function ($value) {
- return (array)$value;
- })->toArray();
-
- array_unshift($messages, $system_message);
- $messages[] = [
- 'role' => 'user',
- 'content' => $prompt
- ];
- }
- }
- // 模型兼容性处理:deepseek-chat 和 deepseek-reasoner 映射到 deepseek-v4-flash
- $thinkingMode = getProp($data, 'thinking', 'disabled'); // 默认非思考模式
- $reasoningEffort = getProp($data, 'reasoning_effort', 'high'); // 默认思考强度
-
- if ($model === 'deepseek-chat') {
- // deepseek-chat 映射到 deepseek-v4-flash,非思考模式
- $model = 'deepseek-v4-flash';
- $thinkingMode = 'disabled';
- } elseif ($model === 'deepseek-reasoner') {
- // deepseek-reasoner 映射到 deepseek-v4-flash,思考模式
- $model = 'deepseek-v4-flash';
- $thinkingMode = 'enabled';
- }
-
- $post_data = [
- 'model' => $model,
- 'messages' => $messages,
- // 'max_tokens' => 8192,
- 'temperature' => 0.7,
- 'frequency_penalty' => 0,
- 'presence_penalty' => 0,
- 'response_format' => ['type' => 'text'],
- 'thinking' => ['type' => $thinkingMode],
- 'stream' => true // 启用流式输出
- ];
- if ($thinkingMode == 'enabled') $post_data['reasoning_effort'] = $reasoningEffort;
- // 根据模型类型选择调用方法
- $fullContent = '';
- $fullReasoningContent = '';
- $usage = [];
-
- // dd($post_data);
- // dLog('deepseek')->info('请求参数: '.json_encode($post_data, 256));
- if (in_array($model, ['deepseek-reasoner', 'deepseek-chat', 'deepseek-v4-flash', 'deepseek-v4-pro'])) {
- // DeepSeek 官方模型使用 DeepSeek API
- $streamGenerator = $this->deepSeekStreamResponse($post_data);
- } else {
- // 其他模型使用火山引擎API
- $streamGenerator = $this->volcEngineChatCompletion($post_data);
- }
- // 处理流式输出
- foreach ($streamGenerator as $chunk) {
- if (isset($chunk['type'])) {
- if ($chunk['type'] === 'done') {
- // 最终结果
- $fullContent = $chunk['full_content'];
- $fullReasoningContent = $chunk['full_reasoning'];
- $usage = isset($chunk['usage']) ? $chunk['usage'] : [];
- } else {
- // 逐块yield数据
- yield $chunk;
- }
- }
- }
- dLog('deepseek')->info('完整内容: '.$fullContent);
- logDB('deepseek', 'info', '完整内容', ['content' => $fullContent, 'anime_id' => $anime_id]);
- // 处理完整内容并返回最终结果
- $script_arr = [];
- if ($fullContent) {
- $script_arr = handleScriptContent($fullContent);
- dLog('deepseek')->info('解析内容', $script_arr);
- logDB('deepseek', 'info', '解析内容', $script_arr);
- }
- if (empty($script_arr['roles'])) {
- // 未生成剧本相关内容,保存对话记录并返回提示
- dLog('deepseek')->info('未生成剧本相关的内容');
- try {
- $now = date('Y-m-d H:i:s');
-
- // 保存对话记录
- $records = [
- [
- 'uid' => $uid,
- 'anime_id' => $anime_id,
- 'sequence' => 0,
- 'role' => 'user',
- 'content' => $prompt,
- 'created_at' => $now,
- 'updated_at' => $now
- ],
- [
- 'uid' => $uid,
- 'anime_id' => $anime_id,
- 'sequence' => 0,
- 'role' => 'assistant',
- 'content' => $fullContent,
- 'created_at' => $now,
- 'updated_at' => $now
- ]
- ];
- DB::table('mp_anime_records')->insert($records);
- } catch (\Exception $e) {
- dLog('deepseek')->error('保存对话记录失败', ['error' => $e->getMessage()]);
- }
-
- yield [
- 'type' => 'done',
- 'script' => [],
- 'episode' => [],
- 'answer' => $fullContent,
- 'reasoning' => $fullReasoningContent,
- 'usage' => $usage,
- 'error' => '未生成剧本相关的内容,请调整提示词再试'
- ];
- return;
- }
- // 替换美术风格
- $mappedArtStyle = $this->getArtStylePromptByInput($input_art_style);
- if ($mappedArtStyle !== '') {
- $script_arr['art_style'] = $mappedArtStyle;
- $fullContent = $this->replaceArtStyleSection($fullContent, $mappedArtStyle);
- }
- $table_data = [
- 'user_id' => $uid,
- 'model' => $model,
- 'anime_name' => isset($script_arr['script_name']) ? $script_arr['script_name'] : '',
- 'intro' => isset($script_arr['intro']) ? $script_arr['intro'] : '',
- 'highlights' => isset($script_arr['highlights']) ? $script_arr['highlights'] : '',
- 'role_relationship' => isset($script_arr['role_relationship']) ? $script_arr['role_relationship'] : '',
- 'core_contradiction' => isset($script_arr['core_contradiction']) ? $script_arr['core_contradiction'] : '',
- 'roles' => isset($script_arr['roles']) ? json_encode($script_arr['roles'], 256) : '',
- 'art_style' => isset($script_arr['art_style']) ? $script_arr['art_style'] : '',
- 'scenes' => isset($script_arr['scenes']) ? json_encode($script_arr['scenes'], 256) : '',
- 'status' => '解析完成',
- 'generate_status' => '待执行',
- 'updated_at' => date('Y-m-d H:i:s')
- ];
- // 如果是修改集数,则将content内容更新(会改变原文)
- if (isset($episode_count) && $episode_count > 0) {
- if (isset($script_arr['content']) && $script_arr['content']) $table_data['content'] = $script_arr['content'];
- $table_data['end_episode_sequence'] = $table_data['episode_num'] = $episode_count;
- }else {
- // 如果需要生成原文内容,从script_arr中提取
- if ($need_generate_content && isset($script_arr['content']) && $script_arr['content']) {
- $table_data['content'] = $script_arr['content'];
- dLog('deepseek')->info('从AI生成结果中提取原文内容', ['content_length' => strlen($script_arr['content'])]);
- if (!$content) {
- yield [
- 'type' => 'done',
- 'script' => [],
- 'episode' => [],
- 'answer' => $fullContent,
- 'reasoning' => $fullReasoningContent,
- 'usage' => $usage,
- 'error' => '未生成剧本内容,请调整提示词再试'
- ];
- return;
- }
- } else {
- $chapters = $this->splitContent($content);
- $chapter_count = count($chapters);
- if ($chapter_count > 0) {
- $table_data['end_episode_sequence'] = $table_data['episode_num'] = $chapter_count;
- }
- }
- }
- $single_episode = [];
- try {
- DB::beginTransaction();
- $now = date('Y-m-d H:i:s');
- // 更新动漫大纲
- $boolen = DB::table('mp_animes')->where('id', $anime_id)->update($table_data);
- if (!$boolen) {
- dLog('deepseek')->info('对话修改失败', $table_data);
- logDB('deepseek', 'error', '对话修改失败', ['anime_id' => $anime_id]);
- Utils::throwError('20003:对话修改失败');
- }
- // 保存对话记录
- $records = [
- [
- 'uid' => $uid,
- 'anime_id' => $anime_id,
- 'sequence' => 0,
- 'role' => 'user',
- 'content' => $prompt,
- 'created_at' => $now,
- 'updated_at' => $now
- ],
- [
- 'uid' => $uid,
- 'anime_id' => $anime_id,
- 'sequence' => 0,
- 'role' => 'assistant',
- 'content' => $fullContent,
- 'created_at' => $now,
- 'updated_at' => $now
- ]
- ];
- // 保存对话记录
- $boolen3 = DB::table('mp_anime_records')->insert($records);
- if (!$boolen3) {
- Utils::throwError('20003:对话记录保存失败');
- }
- // 单剧集模式:生成并保存剧集信息
- if ($is_single) {
- $anime_name = getProp($anime, 'anime_name', '未命名');
- $episode_arr = $this->buildSingleEpisodeFromScript($script_arr, $fullContent, $anime_name);
- if (empty($episode_arr['acts'])) {
- Utils::throwError('20003:单剧集分镜内容解析失败,请确认输出格式');
- }
- $saveResult = $this->saveEpisodeVersionData(
- $anime_id,
- 1,
- $episode_arr,
- is_array(getProp($script_arr, 'roles')) ? getProp($script_arr, 'roles') : [],
- is_array(getProp($script_arr, 'scenes')) ? getProp($script_arr, 'scenes') : [],
- null,
- null,
- false,
- $content,
- $now
- );
- $single_episode = $saveResult['episode'];
- $episode_id = $saveResult['episode_id'];
- $boolen5 = DB::table('mp_animes')->where('id', $anime_id)->update([
- 'roles' => json_encode($saveResult['merged_roles'], 256),
- 'scenes' => json_encode($saveResult['merged_scenes'], 256),
- 'updated_at' => $now
- ]);
- if ($boolen5 === false) {
- Utils::throwError('20003:单剧集主表资源同步失败');
- }
- $table_data['roles'] = json_encode($saveResult['merged_roles'], 256);
- $table_data['scenes'] = json_encode($saveResult['merged_scenes'], 256);
- $episode_record_content = '确认分镜大纲';
- if ($content !== '') {
- $episode_record_content .= "\n\n###上传内容\n" . $content;
- }
- $episode_records = [
- [
- 'uid' => $uid,
- 'anime_id' => $anime_id,
- 'role' => 'user',
- 'content' => $episode_record_content,
- 'sequence' => 1,
- 'episode_id' => $episode_id,
- 'created_at' => $now,
- 'updated_at' => $now
- ],
- [
- 'uid' => $uid,
- 'anime_id' => $anime_id,
- 'role' => 'assistant',
- 'content' => $fullContent,
- 'sequence' => 1,
- 'episode_id' => $episode_id,
- 'created_at' => $now,
- 'updated_at' => $now
- ]
- ];
- $boolen4 = DB::table('mp_anime_records')->insert($episode_records);
- if (!$boolen4) {
- Utils::throwError('20003:单剧集分镜记录保存失败');
- }
- }
-
- }catch (\Exception $e) {
- DB::rollBack();
- dLog('deepseek')->info('修改对话错误信息: '.$e->getMessage());
- logDB('deepseek', 'error', '修改对话失败', ['error' => $e->getMessage(), 'anime_id' => $anime_id]);
- yield [
- 'type' => 'done',
- 'answer' => $fullContent,
- 'reasoning' => $fullReasoningContent,
- 'usage' => $usage,
- 'error' => $e->getMessage(),
- ];
- return;
- }
- DB::commit();
- dLog('deepseek')->info('保存完毕');
- if ($is_single && !empty($single_episode)) {
- $this->batchSetGlobalRoleImg([
- 'anime_id' => $anime_id,
- ]);
- $this->batchSetGlobalSceneImg([
- 'anime_id' => $anime_id,
- ]);
- }
- $table_data['anime_id'] = $anime_id;
- $table_data['roles'] = json_decode($table_data['roles'], true);
- $table_data['scenes'] = json_decode($table_data['scenes'], true);
- unset($table_data['updated_at']);
- unset($table_data['status']);
- yield [
- 'type' => 'done',
- 'script' => $table_data,
- 'episode' => $single_episode,
- 'answer' => $fullContent,
- 'reasoning' => $fullReasoningContent,
- 'usage' => $usage
- ];
- }
- public function chat($data) {
- $uid = Site::getUid();
- $anime_id = getProp($data, 'anime_id');
- $file = getProp($data, 'file');
- $content = getProp($data, 'content', '');
- $bid = getProp($data, 'bid', 0);
- $script_id = getProp($data, 'script_id', 0);
- $episode_number = getProp($data, 'episode_number', 1);
- $prompt = trim((string)getProp($data, 'prompt', '确认分镜大纲'));
- $anime = DB::table('mp_animes')->where('id', $anime_id)->first();
- if (!$anime) Utils::throwError('20003:该对话不存在!');
- if ((int)getProp($anime, 'is_multi') !== 1 && (int)$episode_number !== 1) Utils::throwError('20003:单剧集不支持其他剧集序号');
- $current_episode = DB::table('mp_anime_episodes')->where('anime_id', $anime_id)->where('episode_number', $episode_number)->where('is_default', 1)->first();
- $base_episode = $current_episode ?: DB::table('mp_anime_episodes')->where('anime_id', $anime_id)->where('episode_number', $episode_number)->orderByDesc('id')->first();
- $roles = $base_episode ? json_decode((string)getProp($base_episode, 'roles'), true) : json_decode((string)getProp($anime, 'roles'), true);
- $scenes = $base_episode ? json_decode((string)getProp($base_episode, 'scenes'), true) : json_decode((string)getProp($anime, 'scenes'), true);
- $roles = is_array($roles) ? $roles : [];
- $scenes = is_array($scenes) ? $scenes : [];
- // 转换主体列表和场景列表的格式
- $roles_content = $this->buildEpisodeItemContent($roles, 'role');
- $scenes_content = $this->buildEpisodeItemContent($scenes, 'scene');
- // 获取音色列表
- // $timbres = DB::table('mp_timbres')->select('timbre_name')->where('is_enabled', 1)->where('id', '<=', 15)->get()->map(function ($value) {
- // $value = (array)$value;
- // $value['timbre_name'] = str_replace('(多情感)', '', $value['timbre_name']);
- // return $value;
- // })->pluck('timbre_name')->toArray();
- // $timbres_content = implode('、', $timbres);
- $timbres_content = "青年男:北京小爷、阳光青年、傲娇霸总、优柔公子、解说小明、青涩小生、低音沉郁、亲切青年
- 青年女:柔美女友、魅力女友、爽快思思、高冷御姐、知性温婉、甜心小美、清新女声、甜美桃子、温柔文雅、甜美小源、贴心闺蜜
- 中年男:胡子叔叔、油腻大叔、神秘法师、优柔帮主
- 中年女:邻居阿姨
- 老年男:幽默大爷
- 老年女:婆婆
- 萌娃:奶气萌娃";
- $existing_roles = $base_episode ? json_decode((string)getProp($base_episode, 'roles'), true) : [];
- $existing_scenes = $base_episode ? json_decode((string)getProp($base_episode, 'scenes'), true) : [];
- $existing_roles = is_array($existing_roles) ? $existing_roles : [];
- $existing_scenes = is_array($existing_scenes) ? $existing_scenes : [];
- // 提取文件内容
- if ($file) {
- $uploaded_content = $this->extractFileContent($file);
- if (!$uploaded_content) {
- Utils::throwError('20003:无法提取文件内容,请检查文件格式');
- }
- } elseif ($script_id) {
- $uploaded_content = $this->getContentByScriptId($script_id);
- if (!$uploaded_content) {
- Utils::throwError('20003:无法获取剧本内容,请检查剧本');
- }
- } elseif ($bid) {
- $uploaded_content = $this->getContentByBid($bid);
- if (!$uploaded_content) {
- Utils::throwError('20003:无法获取书籍内容,请检查书籍');
- }
- } elseif ($content) {
- $uploaded_content = filterContent((string)getProp($data, 'content', ''));
- }
- // elseif ($prompt) {
- // $uploaded_content = filterContent($prompt);
- // }
- else {
- $uploaded_content = '';
- }
- $prompt = trim((string)getProp($data, 'prompt', '确认分镜大纲'));
- $demo = "你是一个专业的文档分析助手及资深编剧,请根据用户提供的文档内容完成示例格式的内容输出(需包含以下几个板块: <剧集标题><故事梗概><美术风格><主体列表><场景列表><分镜剧本>,每个板块之间用###分隔;同时板块之间需满足以下要求:\n
- 强制要求:
- 1.你只负责与剧本相关的内容,如果用户输入与剧本无关的内容,请告知用户并拒绝继续回答,此为第一优先原则,不满足此原则就忽略其他要求并停止继续输出
- 2.如果用户输入修改美术风格、修改画风这类内容,请告知用户不可修改美术风格,并拒绝继续回答
- 3.以上两条要求对修改主体角色或场景不生效,允许用户调整主体角色说明和场景描述等
- 4.用户如果要求你帮忙写一个故事或者剧本,此类要求不属于与剧本无关,请帮助用户完成请求
- 5.强制按照示例格式返回内容,不要更改格式或随意增加符号\n
- 普通要求:
- 1.剧集标题必须与文档内容高度相关,剧集序号是{$episode_number}。
- 2.<主体列表>必须在以下主体中选择多个(根据拆分出的分镜剧本来确认具体几个),不得自定义(需带上冒号后的具体描述):\n{$roles_content}。
- 2.1如果分集内容中出现了<主体列表>中不存在的新主体,则不用在上述主体中选择,而是按照相同的格式新增一个主体显示在主题列表中。
- 2.2主体列表的音色必须在以下音色中($timbres_content)选择,需要角色描述的年纪和性别选择,比如角色是青年男性就只能在北京小爷、阳光青年、傲娇霸总、优柔公子、解说小明、青涩小生、低音沉郁、亲切青年中选择,选好的音色放在描述的最后用{{}}框起来。
- 3.<场景列表>必须在以下场景中选择多个(需带上冒号后的具体描述),根据拆分出的分镜剧本来确认具体几个,除非出现<场景列表>中不存在的场景,否则不得自定义:\n{$scenes_content}。
- 4.<分镜剧本>要求包含“分幕标题”以及“分镜信息”:
- 4.1“分幕标题”是根据章节中的场景进行拆分,类似于“第1幕 场景、时间及室内或室外”的信息,其中场景只能在所给出的场景列表中选(不带场景详细描述)。
- 4.2“分镜信息”需同一场景内的对话内容完成分镜(每一幕分镜尽量不得低于5个),需包含“画面描述”、“构图设计”、“运镜调度”、“配音角色”、“台词内容”、“画面类型”、“尾帧描述”、“情感”、“性别”、“语速”、“音量”、“情感强度”、“音调”这几个部分,其中“画面描述”如果出现主体,只能选择上述主体列表中的一个,主体的名称也需要保持一致(不带主体具体描述)。
- 4.3分镜序号用1、2、3显示,如分镜1、分镜2,并且分镜序号需连续(跨分幕中的分镜序号需连续,但跨剧集中的不需要连续,如第2集的第1幕分镜序号从1开始)。
- 4.4“分镜信息”中的“台词内容”需满足以下要求: (1. 台词来源规则:- 台词内容必须逐字摘录自小说原文中的人物直接对话(带引号的内容)。- 不允许改写、总结、压缩、扩写或拼接原文对白。- 不允许生成原文中不存在的对话。2. 非对话过滤:- 旁白、心理描写、环境描写、动作描写等内容,禁止出现在台词内容中。3. 空台词规则:- 如果该分镜中没有人物对话,则“台词内容”必须返回“无”。- 严禁输出“无台词”、“没有”等任何替代文本。4. 禁止推测:- 不得根据语境补全人物可能说的话。)
- 4.5“分镜信息”中的“配音角色”需对应本集中的<主体列表>中的一个(非对话内容则统一固定为“旁白”角色),不得自己生成。
- 4.6“分镜信息”中的“场景”需对应本集中的<场景列表>中的一个(不带详细表述)。
- 4.7“分镜信息”中的“出镜角色”需对应本集中的<主体列表>中的一个或多个(不带详细描述),如果没有出镜角色则填无。
- 4.8“分镜信息”中的“尾帧描述”需根据本镜和下一镜的剧本信息进行过渡生成,用于后续转视频进行衔接上下镜头的画面。
- 4.9“分镜信息”中的“情感”需根据台词内容和剧情氛围分析得出,可选值为:开心、悲伤、生气、惊讶、恐惧、厌恶、激动、冷漠、中性、沮丧、撒娇、害羞、安慰鼓励、咆哮、温柔、自然讲述、情感电台、磁性、广告营销、气泡音、新闻播报、娱乐八卦,不要在情感可选词后加任何解释性的词语,比如(对自己)、(对别人)等,不要自定义情感,必须在可选值中选择。
- 4.10“分镜信息”中的“性别”需根据配音角色的性别确定,可选值为:1(男)、2(女)、0(中性)。
- 4.11“分镜信息”中的“语速”需根据台词内容和情感氛围确定,可选值范围为:-50到100之间的整数,100代表2.0倍速,-50代表0.5倍数,负值表示减慢,正值表示加快,0表示正常语速。
- 4.12“分镜信息”中的“音量”需根据台词内容和情感氛围确定,可选值范围为:-50到100之间的整数,100代表2.0倍音量,-50代表0.5倍音量,负值表示降低音量,正值表示提高音量,0表示正常音量。
- 4.13“分镜信息”中的“情感强度”需根据情感的强烈程度确定,可选值范围为:1到5之间的整数,默认情况下选择4。
- 4.14“分镜信息”中的“音调”需根据台词内容和情感氛围确定,可选值范围为:-12到12之间的整数,负值表示降低音调,正值表示提高音调,0表示正常音调。\n
- 示例格式:\n
- 第1集:隐形的守护者
- ###故事梗概
- 内容梗概:许芸归家遭徐母毒打,她冷静周旋并编造谎言掩盖行踪,引得一旁阴沉的卞大伟愈发急躁。半个月后,徐母离家,卞大伟兽性大发欲行不轨。生死关头,隐形的程景现身肉搏,屋内家具横飞,卞大伟被“空气”重创。许芸趁乱逃出,却惊见地面凭空浮现一串血脚印。意识到守护者为救自己负伤,许芸不再逃避,对着虚无的空气执拗告白,誓要与这看不见的爱人共进退。
-
- ###美术风格
- 基础画风风格词:韩漫二次元
- 视觉风格描述:整体采用精致的韩漫厚涂风格,以浅灰冷调为基调,营造出重生复仇的清冷与压抑感。画面强调高对比度的光影处理,网吧场景通过低饱和度的杂乱色调表现颓废感,而清晨车祸瞬间则利用强烈的动态模糊与局部高光,突显“透明守护者”存在的神秘感与宿命感。人物形象细腻,眼神充满坚毅与破碎交织的故事感,完美契合偏执救赎的主题。
-
- ###主体列表
- 许芸-校服装:重生后的“假千金”,清醒果敢,不再任人唯亲。她是一名高中生,身材纤细,留着乌黑齐肩短发,眼神坚定。{{甜心小美}}
- 许芸-卫衣装:半个月后居家或外出时的装扮,在清冷的环境中透着一丝坚毅与破碎感。{{爽快思思}}
- 程景:偏执深情的学霸,为救许芸变得“不可见”,是始终默默守护的隐形爱人。他身材高大挺拔,面容英俊。{{阳光青年}}
- 卞大伟:许芸名义上的表哥,阴险暴戾的无业游民。他身材肥胖,面部肉感重,一双小眼睛透着阴冷。{{广州德哥}}
- 徐母:许芸亲生母亲,重男轻女且极度刻薄,面容严厉。{{邻居阿姨}}
-
- ###场景列表
- 徐家老旧厨房:这是徐母劳作和惩罚许芸的地方。空间狭窄阴暗,充满了生活琐碎的压抑感。室内有一个旧式锅炉,案板上散落着面粉和包好的馄饨,光影对比强烈,凸显出家庭环境的冷酷。
- 徐家简陋客厅:半个月后冲突爆发的中心。陈设简单且陈旧,只有几把木凳和一张方桌。窗帘半掩,透进微弱的冷光,营造出一种暴风雨前的宁静与不安感。
- 老旧公寓门口:许芸逃出家门后回望的视点。门框斑驳,透出屋内昏暗压抑的光线,与室外略显空旷的走廊形成对比,象征着逃离与未知的守护。
- 破败巷弄与臭水沟:家门外的外部环境。一条狭窄、潮湿的巷弄,旁边是散发着冷气的臭水沟,水面偶尔泛起涟漪。这里是“透明守护者”受伤后经过的地方,氛围凄清。
- 潮湿的巷弄地面:特写视角。灰色的水泥地面湿迹斑斑,这是许芸发现血脚印并进行最后告白的地方。强调地面的质感和清冷的基调。
-
- ###分镜剧本
- ##第1幕:徐家老旧厨房 白天 室内
- 分镜1
- 画面描述:许芸-校服装站在厨房门口,书包背在肩上,徐母手持扫帚,正对着许芸的背部猛烈挥打。卞大伟站在锅炉后方,双手抱胸,阴沉的目光落在许芸身上。
- 场景:徐家老旧厨房
- 构图设计:全景,低角度仰视
- 运镜调度:手持拍摄
- 配音角色:旁白
- 出镜角色:许芸-校服装、徐母
- 台词内容:我一到家,徐母的扫帚就落在我身上,嘴里一个劲地骂我,
- 画面类型:普通画面
- 尾帧描述:手持拍摄微晃镜头,中年妇女猛力挥动扫帚击打少女背部,少女身体因冲击微颤,阴影中的肥胖男子目光冷冷跟随。
- 分镜2
- 画面描述:徐母面容扭曲,嘴唇快速开合,扫帚挥舞在空中。
- 场景:徐家老旧厨房
- 构图设计:近景,徐母面部特写
- 运镜调度:固定镜头
- 配音角色:徐母
- 出镜角色:徐母
- 台词内容:问我夜不归宿死哪儿去了。
- 画面类型:对口型
- 尾帧描述:固定镜头,中年妇女面部肌肉剧烈抖动,嘴唇快速开合进行辱骂,背景中的扫帚在空中上下挥动。
-
- ##第2幕:徐家简陋客厅 黄昏 室内
- 分镜3
- 画面描述:客厅窗户透出昏黄的夕阳余晖,空荡荡的客厅中央只有几把木凳和一张方桌,显得异常安静。
- 场景:徐家简陋客厅
- 构图设计:全景,景深虚化
- 运镜调度:固定镜头
- 配音角色:旁白
- 出镜角色:无
- 台词内容:可我没想到,半个月后,徐母居然再一次外出,
- 画面类型:普通画面
- 尾帧描述:固定镜头。昏黄的夕阳余晖在地面上缓慢移动,空气中的浮尘在光束中跳动,室内维持着死寂的安静。
- 分镜4
- 画面描述:一张泛黄的火车票特写,上面显示着隔壁市的站名和日期。
- 场景:徐家简陋客厅
- 构图设计:特写,火车票
- 运镜调度:固定镜头
- 配音角色:旁白
- 出镜角色:无
- 台词内容:这次是去隔壁市看望她生病的好姐妹。
- 画面类型:普通画面
- 尾帧描述:固定镜头。光影在火车票表面微微晃动,强调其陈旧的纸张纹理与关键的行程信息。
- \n\n";
- $origin_prompt = trim((string)getProp($data, 'prompt', '确认分镜大纲'));
- $is_regenerate_version = !in_array($origin_prompt, ['确认分镜大纲', '继续策划下一集'], true);
- $is_global_generate_pics = $origin_prompt == '确认分镜大纲' ? 1 : 0;
- $prompt = $origin_prompt;
- $is_single = (int)getProp($anime, 'is_multi') !== 1;
- // 获取章节内容
- $full_content = getProp($anime, 'content');
- // 根据章节内容拆分章节
- $chapters = $this->splitContent($full_content);
- $chapter_content = isset($chapters[($episode_number-1)]['content']) ? $chapters[($episode_number-1)]['content'] : '';
- if (!$chapter_content) $chapter_content = $full_content; // 如果无法拆分章节则直接用原文
- $messages = [];
- if ($prompt == '确认分镜大纲') {
- $content = $uploaded_content ?: $chapter_content;
- if ($is_single) $content = $full_content; // 单剧集使用全文
- if (!$content) {
- Utils::throwError('20003:章节内容无法获取,请确认');
- }
- $question = $is_single
- ? $demo . "请根据以上要求分析完整剧情,直接输出第1集完整单剧集分镜剧本,需一次性生成并可直接保存,剧情内容如下: \n\n$content"
- : $demo . "请根据以上要求分析本章剧情,完成分镜剧本,本章剧情内容如下: \n\n$content";
- // 构建消息
- $messages[] =
- [
- 'role' => 'user',
- 'content' => $question
- ];
- }else if ($prompt == '继续策划下一集') {
- if ($is_single) {
- Utils::throwError('20003:单剧集不支持继续策划下一集');
- }
- $content = $uploaded_content ?: $chapter_content;
- if (!$content) {
- Utils::throwError('20003:章节内容无法获取,请确认');
- }
- $question = $demo."请根据以上要求分析本章剧情,完成分镜剧本(需衔接上一章内容),本章剧情内容如下: \n\n$content";
- // 获取上一集最后记录
- $prev_sequence = $episode_number - 1;
- $messages = $this->getEpisodeChatMessages($anime_id, $prev_sequence);
- // 构建消息
- $messages[] =
- [
- 'role' => 'user',
- 'content' => $question
- ];
- }else {
- $content = $uploaded_content ?: trim((string)getProp($base_episode, 'content'));
- if (!$content) {
- $content = $is_single ? $full_content : ($chapter_content ?: $full_content);
- }
- if (!$content) {
- Utils::throwError('20003:章节内容无法获取,请确认');
- }
- $messages = $this->getEpisodeChatMessages($anime_id, $episode_number);
- $targetEpisodeText = $is_single ? '第1集完整单剧集版本' : "第{$episode_number}集完整的新版本剧集";
- $question = $demo . "\n请基于以上历史对话记录,重新生成{$targetEpisodeText},并严格按示例格式输出完整内容。"
- . "\n\n本次修改要求如下:\n{$origin_prompt}";
- $messages[] =
- [
- 'role' => 'user',
- 'content' => $question
- ];
- }
-
- // 处理文本模型
- $model = getProp($data, 'model');
- if (!$model) {
- // 用户没有输入,从anime表获取
- $anime = DB::table('mp_animes')->where('id', $anime_id)->first();
- $model = getProp($anime, 'model');
- if (!$model) {
- // anime表也没有,使用默认值
- $model = 'doubao-seed-2-0-mini-260215';
- }
- }
- // 验证模型是否在可用模型表中
- if (!DB::table('mp_text_models')->where('model', $model)->where('is_enabled', 1)->exists()) {
- $model = 'doubao-seed-2-0-mini-260215';
- }
- // 模型兼容性处理:deepseek-chat 和 deepseek-reasoner 映射到 deepseek-v4-flash
- $thinkingMode = getProp($data, 'thinking', 'disabled'); // 默认非思考模式
- $reasoningEffort = getProp($data, 'reasoning_effort', 'high'); // 默认思考强度
-
- if ($model === 'deepseek-chat') {
- // deepseek-chat 映射到 deepseek-v4-flash,非思考模式
- $model = 'deepseek-v4-flash';
- $thinkingMode = 'disabled';
- } elseif ($model === 'deepseek-reasoner') {
- // deepseek-reasoner 映射到 deepseek-v4-flash,思考模式
- $model = 'deepseek-v4-flash';
- $thinkingMode = 'enabled';
- }
- $post_data = [
- 'model' => $model,
- 'messages' => $messages,
- // 'max_tokens' => 8192,
- 'temperature' => 0.7,
- 'frequency_penalty' => 0,
- 'presence_penalty' => 0,
- 'response_format' => ['type' => 'text'],
- 'thinking' => ['type' => $thinkingMode],
- 'stream' => true // 启用流式输出
- ];
- if ($thinkingMode == 'enabled') $post_data['reasoning_effort'] = $reasoningEffort;
- // 根据模型类型选择调用方法
- $fullContent = '';
- $fullReasoningContent = '';
- $usage = [];
-
- if (in_array($model, ['deepseek-reasoner', 'deepseek-chat', 'deepseek-v4-flash', 'deepseek-v4-pro'])) {
- // DeepSeek 官方模型使用 DeepSeek API
- $streamGenerator = $this->deepSeekStreamResponse($post_data);
- } else {
- // 其他模型使用火山引擎API
- $streamGenerator = $this->volcEngineChatCompletion($post_data);
- }
- // 处理流式输出
- if (!is_iterable($streamGenerator)) {
- dLog('deepseek')->error('chat流式生成器无效', ['generator_type' => gettype($streamGenerator)]);
- Utils::throwError('20003:接口返回数据异常,请重新请求');
- }
-
- foreach ($streamGenerator as $chunk) {
- if (isset($chunk['type'])) {
- if ($chunk['type'] === 'done') {
- // 最终结果
- $fullContent = $chunk['full_content'];
- $fullReasoningContent = $chunk['full_reasoning'];
- $usage = isset($chunk['usage']) ? $chunk['usage'] : [];
- } else {
- // 逐块yield数据
- yield $chunk;
- }
- }
- }
- dLog('deepseek')->info('完整内容: '.$fullContent);
- logDB('deepseek', 'info', '完整内容', ['content' => $fullContent, 'anime_id' => $anime_id]);
- // 处理完整内容并返回最终结果
- $episode_arr = handleEpisodeContent($fullContent);
- logDB('deepseek', 'info', '解析内容', $episode_arr);
- if (empty($episode_arr['acts'])) {
- // 未生成剧本相关内容,保存对话记录并返回提示
- dLog('deepseek')->info('未生成分镜剧本相关的内容');
- try {
- $now = date('Y-m-d H:i:s');
-
- // 保存对话记录
- $records = [
- [
- 'uid' => $uid,
- 'anime_id' => $anime_id,
- 'sequence' => $episode_number,
- 'role' => 'user',
- 'content' => $prompt,
- 'created_at' => $now,
- 'updated_at' => $now
- ],
- [
- 'uid' => $uid,
- 'anime_id' => $anime_id,
- 'sequence' => $episode_number,
- 'role' => 'assistant',
- 'content' => $fullContent,
- 'created_at' => $now,
- 'updated_at' => $now
- ]
- ];
- DB::table('mp_anime_records')->insert($records);
- } catch (\Exception $e) {
- dLog('deepseek')->error('保存对话记录失败', ['error' => $e->getMessage()]);
- }
-
- yield [
- 'type' => 'done',
- 'episode' => [],
- 'answer' => $fullContent,
- 'reasoning' => $fullReasoningContent,
- 'usage' => $usage,
- 'error' => '未生成分镜剧本相关的内容,请调整提示词再试'
- ];
- return;
- }
- $existing_roles = $base_episode ? json_decode((string)getProp($base_episode, 'roles'), true) : [];
- $existing_scenes = $base_episode ? json_decode((string)getProp($base_episode, 'scenes'), true) : [];
- $existing_roles = is_array($existing_roles) ? $existing_roles : [];
- $existing_scenes = is_array($existing_scenes) ? $existing_scenes : [];
- $now = date('Y-m-d H:i:s');
- try {
- DB::beginTransaction();
- $saveResult = $this->saveEpisodeVersionData(
- $anime_id,
- $episode_number,
- $episode_arr,
- $existing_roles,
- $existing_scenes,
- $current_episode,
- $base_episode,
- $is_regenerate_version,
- $content,
- $now
- );
- $episode = $saveResult['episode'];
- $episode_id = $saveResult['episode_id'];
- $merged_roles = $saveResult['merged_roles'];
- $merged_scenes = $saveResult['merged_scenes'];
- $record_content = $origin_prompt;
- if ($uploaded_content !== '') {
- $record_content .= "\n\n###上传内容\n" . $uploaded_content;
- }
- $records = [
- [
- 'uid' => $uid,
- 'anime_id' => $anime_id,
- 'role' => 'user',
- 'content' => $record_content,
- 'sequence' => $episode_number,
- 'episode_id' => $episode_id,
- 'created_at' => $now,
- 'updated_at' => $now
- ],
- [
- 'uid' => $uid,
- 'anime_id' => $anime_id,
- 'role' => 'assistant',
- 'content' => $fullContent,
- 'sequence' => $episode_number,
- 'episode_id' => $episode_id,
- 'created_at' => $now,
- 'updated_at' => $now
- ]
- ];
- $boolen4 = DB::table('mp_anime_records')->insert($records);
- if (!$boolen4) {
- Utils::throwError('20003:对话记录保存失败');
- }
-
- // 保存模型到anime表
- DB::table('mp_animes')->where('id', $anime_id)->update([
- 'model' => $model,
- 'updated_at' => $now
- ]);
- }catch (\Exception $e) {
- DB::rollBack();
- dLog('deepseek')->info('新建对话错误信息: '.$e->getMessage());
- yield [
- 'type' => 'done',
- 'answer' => $fullContent,
- 'reasoning' => $fullReasoningContent,
- 'usage' => $usage,
- 'error' => $e->getMessage(),
- ];
- return;
- }
-
- DB::commit();
- if ($is_global_generate_pics) {
- // 批量生成全局角色图片
- $this->batchSetGlobalRoleImg([
- 'anime_id' => $anime_id,
- ]);
-
- // 批量生成全局场景图片
- $this->batchSetGlobalSceneImg([
- 'anime_id' => $anime_id,
- ]);
- }
- $episode['episode_id'] = $episode_id;
- $episode['roles'] = $merged_roles;
- $episode['scenes'] = $merged_scenes;
- $episode['acts'] = isset($episode_arr['acts']) && is_array($episode_arr['acts']) ? $episode_arr['acts'] : [];
- yield [
- 'type' => 'done',
- 'episode' => $episode,
- 'answer' => $fullContent,
- 'reasoning' => $fullReasoningContent,
- 'usage' => $usage
- ];
- }
- private function buildSingleEpisodeFromScript(array $script_arr, string $fullContent, string $animeName = ''): array
- {
- $episode_arr = [
- 'episode_title' => $animeName, // 单剧集默认使用剧本名
- 'intro' => getProp($script_arr, 'intro'),
- 'art_style' => getProp($script_arr, 'art_style'),
- 'roles' => is_array(getProp($script_arr, 'roles')) ? getProp($script_arr, 'roles') : [],
- 'scenes' => is_array(getProp($script_arr, 'scenes')) ? getProp($script_arr, 'scenes') : [],
- 'acts' => is_array(getProp($script_arr, 'acts')) ? getProp($script_arr, 'acts') : [],
- ];
- if (empty($episode_arr['acts'])) {
- $fallback_episode_arr = handleEpisodeContent($fullContent);
- if (!empty($fallback_episode_arr['acts'])) {
- $episode_arr = array_merge($fallback_episode_arr, [
- 'intro' => getProp($fallback_episode_arr, 'intro') ?: getProp($script_arr, 'intro'),
- 'art_style' => getProp($fallback_episode_arr, 'art_style') ?: getProp($script_arr, 'art_style'),
- 'roles' => !empty(getProp($fallback_episode_arr, 'roles')) ? getProp($fallback_episode_arr, 'roles') : getProp($script_arr, 'roles', []),
- 'scenes' => !empty(getProp($fallback_episode_arr, 'scenes')) ? getProp($fallback_episode_arr, 'scenes') : getProp($script_arr, 'scenes', []),
- ]);
- }
- }
- // if (!getProp($episode_arr, 'episode_title')) {
- // $episode_title = '';
- // $episodes = getProp($script_arr, 'episodes', []);
- // if (is_array($episodes) && !empty($episodes[0]['title'])) {
- // $episode_title = '第1集:' . $episodes[0]['title'];
- // }
- // if (!$episode_title) {
- // $episode_title = '第1集:' . ($animeName ?: '未命名');
- // }
- // $episode_arr['episode_title'] = $episode_title;
- // }
- return $episode_arr;
- }
- private function saveEpisodeVersionData(
- int $anime_id,
- int $episode_number,
- array $episode_arr,
- array $existing_roles,
- array $existing_scenes,
- $current_episode,
- $base_episode,
- bool $is_regenerate_version,
- string $content,
- string $now
- ): array {
- $generated_roles = isset($episode_arr['roles']) && is_array($episode_arr['roles']) ? $episode_arr['roles'] : [];
- $generated_scenes = isset($episode_arr['scenes']) && is_array($episode_arr['scenes']) ? $episode_arr['scenes'] : [];
- $merged_roles = $this->mergeEpisodeResourceItems($existing_roles, $generated_roles, 'role');
- $merged_scenes = $this->mergeEpisodeResourceItems($existing_scenes, $generated_scenes, 'scene');
-
- // 获取 mp_animes 表中的全局 roles 和 scenes 用于对比
- $anime = DB::table('mp_animes')->where('id', $anime_id)->first();
- $global_roles = json_decode(getProp($anime, 'roles'), true);
- $global_scenes = json_decode(getProp($anime, 'scenes'), true);
- $global_roles = is_array($global_roles) ? $global_roles : [];
- $global_scenes = is_array($global_scenes) ? $global_scenes : [];
- $art_style = getProp($episode_arr, 'art_style') ?: getProp($anime, 'art_style');
-
- // 检查并创建 roles 的图片生成任务
- $merged_roles = $this->checkAndCreateImageTasks($merged_roles, $global_roles, 'role', $art_style);
-
- // 检查并创建 scenes 的图片生成任务
- $merged_scenes = $this->checkAndCreateImageTasks($merged_scenes, $global_scenes, 'scene', $art_style);
- $role_arr = [];
- foreach ($merged_roles as $item) {
- $role_arr[$item['role']] = $item;
- }
- $version_count = DB::table('mp_anime_episodes')
- ->where('anime_id', $anime_id)
- ->where('episode_number', $episode_number)
- ->count('id');
- $episode = [
- 'anime_id' => $anime_id,
- 'episode_number' => $episode_number,
- 'title' => getProp($episode_arr, 'episode_title'),
- 'content' => $content,
- 'intro' => getProp($episode_arr, 'intro'),
- 'art_style' => getProp($episode_arr, 'art_style'),
- 'roles' => json_encode($merged_roles, 256),
- 'scenes' => json_encode($merged_scenes, 256),
- 'generate_status' => '待执行',
- 'generate_at' => $now,
- 'is_default' => 1,
- 'updated_at' => $now,
- ];
- $del_flag = false;
- if ($is_regenerate_version) {
- DB::table('mp_anime_episodes')
- ->where('anime_id', $anime_id)
- ->where('episode_number', $episode_number)
- ->update(['is_default' => 0, 'updated_at' => $now]);
- $episode['sequence'] = $version_count + 1;
- $episode['created_at'] = $now;
- $episode_id = DB::table('mp_anime_episodes')->insertGetId($episode);
- if (!$episode_id) {
- Utils::throwError('20003:创建剧集版本失败');
- }
- } else {
- $target_episode = $current_episode ?: $base_episode;
- if ($target_episode) {
- $episode_id = getProp($target_episode, 'id');
- $episode['sequence'] = getProp($target_episode, 'sequence', 1);
- DB::table('mp_anime_episodes')
- ->where('anime_id', $anime_id)
- ->where('episode_number', $episode_number)
- ->where('id', '<>', $episode_id)
- ->update(['is_default' => 0, 'updated_at' => $now]);
- $boolen = DB::table('mp_anime_episodes')->where('id', $episode_id)->update($episode);
- if ($boolen === false) {
- logDB('deepseek', 'error', '分集内容保存失败', ['anime_id' => $anime_id, 'episode_id' => $episode_id]);
- Utils::throwError('20003:分集内容保存失败');
- }
- $del_flag = true;
- } else {
- DB::table('mp_anime_episodes')
- ->where('anime_id', $anime_id)
- ->where('episode_number', $episode_number)
- ->update(['is_default' => 0, 'updated_at' => $now]);
- $episode['sequence'] = $version_count > 0 ? ($version_count + 1) : 1;
- $episode['created_at'] = $now;
- $episode_id = DB::table('mp_anime_episodes')->insertGetId($episode);
- if (!$episode_id) {
- logDB('deepseek', 'error', '分集内容保存失败', ['anime_id' => $anime_id, 'episode_number' => $episode_number]);
- Utils::throwError('20003:分集内容保存失败');
- }
- }
- }
- $segments = [];
- $acts = isset($episode_arr['acts']) && is_array($episode_arr['acts']) ? $episode_arr['acts'] : [];
- $emotion_list = DB::table('mp_emotion_list')->where('is_enabled', 1)->pluck('emotion_name', 'emotion_code')->toArray();
- $emotion_list = array_flip($emotion_list);
- foreach ($acts as $act) {
- $act_segments = getProp($act, 'segments', []);
- if (!is_array($act_segments)) {
- continue;
- }
- foreach ($act_segments as $segment) {
- $voice_actor = getProp($segment, 'voice_actor');
- $timbre_info = isset($role_arr[$voice_actor]) ? $role_arr[$voice_actor] : [];
- $voice_type = getProp($timbre_info, 'voice_type');
- $timbre_emotion = DB::table('mp_timbres')->where('timbre_type', $voice_type)->value('emotion');
- if ($timbre_emotion) {
- $timbre_emotion = explode(',', $timbre_emotion);
- } else {
- $timbre_emotion = [];
- }
- $emotion = getProp($segment, 'emotion', '中性');
- if (!in_array($emotion, $timbre_emotion)) {
- $emotion = '中性';
- }
- $emotion_type = isset($emotion_list[$emotion]) ? $emotion_list[$emotion] : 'neutral';
- $segments[] = [
- 'anime_id' => $anime_id,
- 'episode_id' => $episode_id,
- 'episode_number' => $episode_number,
- 'act_number' => getProp($act, 'act_number'),
- 'act_title' => getProp($act, 'act_title'),
- 'segment_id' => getProp($segment, 'segment_id'),
- 'segment_number' => getProp($segment, 'segment_number'),
- 'segment_content' => getProp($segment, 'segment_content'),
- 'description' => getProp($segment, 'description'),
- 'composition' => getProp($segment, 'composition'),
- 'camera_movement' => getProp($segment, 'camera_movement'),
- 'voice_actor' => $voice_actor,
- 'dialogue' => getProp($segment, 'dialogue'),
- 'frame_type' => getProp($segment, 'frame_type'),
- 'scene' => getProp($segment, 'scene'),
- 'characters' => getProp($segment, 'characters'),
- 'tail_frame' => getProp($segment, 'tail_frame'),
- 'voice_name' => getProp($timbre_info, 'voice_name'),
- 'voice_type' => $voice_type,
- 'voice_audio_url' => getProp($timbre_info, 'voice_audio_url'),
- 'emotion' => $emotion,
- 'emotion_type' => $emotion_type,
- 'gender' => getProp($segment, 'gender', '0'),
- 'speed_ratio' => getProp($segment, 'speed_ratio', 0),
- 'loudness_ratio' => getProp($segment, 'loudness_ratio', 0),
- 'emotion_scale' => getProp($segment, 'emotion_scale', 4),
- 'pitch' => getProp($segment, 'pitch', 0),
- 'created_at' => $now,
- 'updated_at' => $now
- ];
- }
- }
- if ($segments) {
- if ($del_flag) {
- DB::table('mp_episode_segments')
- ->where('anime_id', $anime_id)
- ->where('episode_id', $episode_id)
- ->delete();
- }
- $boolen = DB::table('mp_episode_segments')->insert($segments);
- if (!$boolen) {
- logDB('deepseek', 'error', '分镜内容保存失败', ['anime_id' => $anime_id, 'episode_id' => $episode_id, 'segments_count' => count($segments)]);
- Utils::throwError('20003:分镜内容保存失败');
- }
- }
- $episode['episode_id'] = $episode_id;
- $episode['roles'] = $merged_roles;
- $episode['scenes'] = $merged_scenes;
- $episode['acts'] = $acts;
- return [
- 'episode' => $episode,
- 'episode_id' => $episode_id,
- 'merged_roles' => $merged_roles,
- 'merged_scenes' => $merged_scenes,
- ];
- }
- private function buildEpisodeItemContent(array $items, string $nameKey): string
- {
- $content = '';
- foreach ($items as $item) {
- $name = trim((string)getProp($item, $nameKey));
- if ($name === '') {
- continue;
- }
- $description = trim((string)getProp($item, 'description'));
- $content .= $name . ': ' . $description . "\n";
- }
- return trim($content);
- }
- private function getEpisodeChatMessages($animeId, $sequence): array
- {
- if ((int)$sequence <= 0) {
- return [];
- }
- return DB::table('mp_anime_records')
- ->where('anime_id', $animeId)
- ->where('sequence', $sequence)
- ->select('role', 'content')
- ->orderBy('created_at')
- ->orderBy('id')
- ->get()
- ->map(function ($value) {
- return (array)$value;
- })
- ->toArray();
- }
- private function mergeEpisodeResourceItems(array $existingItems, array $generatedItems, string $nameKey): array
- {
- $existingMap = [];
- foreach ($existingItems as $item) {
- $itemKey = $this->normalizeEpisodeResourceKey(getProp($item, $nameKey));
- if ($itemKey === '') {
- continue;
- }
- $existingMap[$itemKey] = $item;
- }
- if (!$generatedItems) {
- return array_values($existingItems);
- }
- $merged = [];
- foreach ($generatedItems as $item) {
- $itemKey = $this->normalizeEpisodeResourceKey(getProp($item, $nameKey));
- if ($itemKey === '') {
- continue;
- }
- if (isset($existingMap[$itemKey])) {
- if (trim((string)getProp($item, 'description')) === '') {
- $merged[] = $existingMap[$itemKey];
- continue;
- }
- // 检查内容是否发生变化
- $isChanged = $this->isEpisodeResourceChanged($existingMap[$itemKey], $item, $nameKey);
-
- if (!$isChanged) {
- // nameKey 和 description 一致,继承现有项的 URL、task_id 和 task_status
- $mergedItem = [
- $nameKey => trim((string)getProp($item, $nameKey)),
- 'description' => trim((string)getProp($item, 'description')),
- ];
-
- // 继承现有项的 URL
- $existingUrl = getProp($existingMap[$itemKey], 'url');
- if (!empty($existingUrl)) {
- $mergedItem['url'] = $existingUrl;
- }
-
- // 继承现有项的 task_id
- $existingTaskId = getProp($existingMap[$itemKey], 'task_id');
- if (!empty($existingTaskId)) {
- $mergedItem['task_id'] = $existingTaskId;
- }
-
- // 继承现有项的 task_status
- $existingTaskStatus = getProp($existingMap[$itemKey], 'task_status');
- if (!empty($existingTaskStatus)) {
- $mergedItem['task_status'] = $existingTaskStatus;
- }
-
- // 保留生成项的音色字段
- $voiceName = getProp($item, 'voice_name');
- if (!empty($voiceName)) {
- $mergedItem['voice_name'] = $voiceName;
- }
-
- $voiceType = getProp($item, 'voice_type');
- if (!empty($voiceType)) {
- $mergedItem['voice_type'] = $voiceType;
- }
- $audioUrl = getProp($item, 'voice_audio_url');
- if (!empty($audioUrl)) {
- $mergedItem['voice_audio_url'] = $audioUrl;
- }
-
- $merged[] = $mergedItem;
- } else {
- // nameKey 或 description 不一致,重置 URL、task_id 和 task_status,但保留音色字段
- $mergedItem = [
- $nameKey => trim((string)getProp($item, $nameKey)),
- 'description' => trim((string)getProp($item, 'description')),
- ];
-
- // 保留生成项的音色字段
- $voiceName = getProp($item, 'voice_name');
- if (!empty($voiceName)) {
- $mergedItem['voice_name'] = $voiceName;
- }
-
- $voiceType = getProp($item, 'voice_type');
- if (!empty($voiceType)) {
- $mergedItem['voice_type'] = $voiceType;
- }
-
- $audioUrl = getProp($item, 'voice_audio_url');
- if (!empty($audioUrl)) {
- $mergedItem['voice_audio_url'] = $audioUrl;
- }
-
- // 不继承 URL、task_id 和 task_status(重置状态)
- $merged[] = $mergedItem;
- }
- } else {
- // 新增项,保留生成项的所有字段
- $merged[] = $this->resetEpisodeResourceItem($item, $nameKey, ['url', 'task_id', 'task_status', 'voice_name', 'voice_type', 'voice_audio_url']);
- }
- }
- return $merged ?: array_values($existingItems);
- }
- private function resetEpisodeResourceItem(array $item, string $nameKey, array $preserveFields = []): array
- {
- $resetItem = [
- $nameKey => trim((string)getProp($item, $nameKey)),
- 'description' => trim((string)getProp($item, 'description')),
- ];
-
- // 保留指定的字段
- foreach ($preserveFields as $field) {
- $value = getProp($item, $field);
- if (!empty($value)) {
- $resetItem[$field] = $value;
- }
- }
-
- return $resetItem;
- }
- private function isEpisodeResourceChanged(array $existingItem, array $generatedItem, string $nameKey): bool
- {
- $existingKey = $this->normalizeEpisodeResourceKey(getProp($existingItem, $nameKey));
- $generatedKey = $this->normalizeEpisodeResourceKey(getProp($generatedItem, $nameKey));
- if ($existingKey !== $generatedKey) {
- return true;
- }
- return trim((string)getProp($existingItem, 'description')) !== trim((string)getProp($generatedItem, 'description'));
- }
- private function normalizeEpisodeResourceKey($value): string
- {
- $value = trim((string)$value);
- if ($value === '') {
- return '';
- }
- return strtolower((string)preg_replace('/\s+/u', '', $value));
- }
- // 生成全局角色图片
- private function batchSetGlobalRoleImg($data, $is_force=false) {
- $anime_id = getProp($data, 'anime_id');
- if (!$anime_id) Utils::throwError('1002:请选择对话');
- $anime = DB::table('mp_animes')->where('id', $anime_id)->where('is_deleted', 0)->first();
- $roles = json_decode(getProp($anime, 'roles'), true);
- $art_style = getProp($anime, 'art_style');
-
- $update_flag = false;
- foreach ($roles as &$role) {
- $role_name = getProp($role, 'role');
- if ($role_name == '旁白') continue;
- $description = getProp($role, 'description');
- if ($art_style) $description = "美术风格:\n$art_style\n主体描述:$description";
- // 参考图地址
- $ref_img_url = getProp($role, 'url');
- if (!$is_force && $ref_img_url) continue;
- $update_flag = true;
-
- // 此处调用AIImageGenerationService的生成图片任务接口: createImageGenerationTask(注意使用默认参数,prompt使用角色描述),并返回任务id
- try {
- $params = [
- 'prompt' => $description,
- 'ref_img_urls' => !empty($ref_img_url) ? [$ref_img_url] : []
- ];
-
- $task = $this->aiImageGenerationService->createImageGenerationTask($params);
- $task_id = $task->id;
-
- if (!$task_id) {
- Utils::throwError("20003: 角色({$role_name})生成图片失败");
- }
-
- $role['task_id'] = $task_id;
- $role['task_status'] = 'processing';
- } catch (\Exception $e) {
- Utils::throwError("20003:" . $e->getMessage());
- }
- }
- if (!$update_flag) return true;
-
- // 保存角色信息
- $boolen = DB::table('mp_animes')->where('id', $anime_id)->update([
- 'roles' => json_encode($roles, 256),
- 'generate_status' => '执行中',
- 'generate_at' => date('Y-m-d H:i:s'),
- 'updated_at' => date('Y-m-d H:i:s')
- ]);
- if (!$boolen) {
- Utils::throwError('20003:保存角色信息失败');
- }
- return $boolen;
- }
- // 生成全局场景图片
- private function batchSetGlobalSceneImg($data, $is_force=false) {
- $anime_id = getProp($data, 'anime_id');
- if (!$anime_id) Utils::throwError('1002:请选择对话');
- $anime = DB::table('mp_animes')->where('id', $anime_id)->where('is_deleted', 0)->first();
- $scenes = json_decode(getProp($anime, 'scenes'), true);
- $art_style = getProp($anime, 'art_style');
-
- $update_flag = false;
- foreach ($scenes as &$scene) {
- $scene_name = getProp($scene, 'scene');
- $description = getProp($scene, 'description');
- if ($art_style) $description = "美术风格:\n$art_style\n\n场景描述:$description";
- // 参考图地址
- $ref_img_url = getProp($scene, 'url');
- if (!$is_force && $ref_img_url) continue;
- $update_flag = true;
-
- // 此处调用AIImageGenerationService的生成图片任务接口: createImageGenerationTask(注意使用默认参数,prompt使用角色描述),并返回任务id
- try {
- $params = [
- 'prompt' => $description,
- 'ref_img_urls' => !empty($ref_img_url) ? [$ref_img_url] : []
- ];
-
- $task = $this->aiImageGenerationService->createImageGenerationTask($params);
- $task_id = $task->id;
-
- if (!$task_id) {
- Utils::throwError("20003: 角色({$scene_name})生成图片失败");
- }
-
- $scene['task_id'] = $task_id;
- $scene['task_status'] = 'processing';
- } catch (\Exception $e) {
- Utils::throwError("20003:" . $e->getMessage());
- }
- }
- if (!$update_flag) return true;
-
- // 保存角色信息
- $boolen = DB::table('mp_animes')->where('id', $anime_id)->update([
- 'scenes' => json_encode($scenes, 256),
- 'generate_status' => '执行中',
- 'generate_at' => date('Y-m-d H:i:s'),
- 'updated_at' => date('Y-m-d H:i:s')
- ]);
- if (!$boolen) {
- Utils::throwError('20003:保存角色信息失败');
- }
- return $boolen;
- }
- public function chatChangeImg($data) {
- $segment = getProp($data, 'segment');
- $segment_content = getProp($segment, 'segment_content');
- $prompt = getProp($data, 'prompt');
- $ref_img = getProp($data, 'ref_img');
- $question = "请修改已有的内容{$segment_content}\n修改要求如下:\n{$prompt}";
-
- // 处理文本模型
- $model = getProp($data, 'model');
- if (!$model) {
- // 用户没有输入,从anime表获取
- $segment_id = getProp($segment, 'segment_id');
- if ($segment_id) {
- $episode_id = DB::table('mp_episode_segments')->where('segment_id', $segment_id)->value('episode_id');
- if ($episode_id) {
- $anime_id = DB::table('mp_anime_episodes')->where('id', $episode_id)->value('anime_id');
- if ($anime_id) {
- $model = DB::table('mp_animes')->where('id', $anime_id)->value('model');
- }
- }
- }
- if (!$model) {
- // anime表也没有,使用默认值
- $model = 'doubao-seed-2-0-mini-260215';
- }
- }
- // 验证模型是否在可用模型表中
- if (!DB::table('mp_text_models')->where('model', $model)->where('is_enabled', 1)->exists()) {
- $model = 'doubao-seed-2-0-mini-260215';
- }
- $messages[] =
- [
- 'role' => 'user',
- 'content' => $question
- ];
- $post_data = [
- 'model' => $model,
- 'messages' => $messages,
- // 'max_tokens' => 8192,
- 'temperature' => 0.7,
- 'frequency_penalty' => 0,
- 'presence_penalty' => 0,
- 'response_format' => ['type' => 'text'],
- 'stream' => false // 是否启用流式输出
- ];
- if (in_array($model, ['deepseek-reasoner', 'deepseek-chat', 'deepseek-v4-flash', 'deepseek-v4-pro'])) {
- // DeepSeek 官方模型使用 DeepSeek API
- $chatResult = $this->chatOnly($post_data);
- } else {
- // 其他模型使用火山引擎API
- $chatResult = $this->volcEngineChatCompletion($post_data);
- }
- if (is_array($chatResult)) {
- extract($chatResult);
- }else {
- Utils::throwError('20003: 接口调用失败!');
- }
- return [
- 'type' => 'done',
- // 'episode' => $episode,
- 'answer' => $fullContent,
- 'reasoning' => $fullReasoningContent,
- 'usage' => $usage
- ];
-
- }
- // 仅调用deepseek对话接口
- private function chatOnly($post_data) {
- $client = new Client(['timeout' => 1200, 'verify' => false]);
- $result = $client->post($this->url, ['json' => $post_data, 'headers' => $this->headers]);
- $response = $result->getBody()->getContents();
- $response_arr = json_decode($response, true);
- dLog('deepseek')->info('deepseek请求完成', ['response' => $response_arr]);
- $fullContent = '';
- $fullReasoningContent = [];
- $usage = isset($response_arr['usage']) ? $response_arr['usage'] : [];
- if (isset($response_arr['choices']) && count($response_arr['choices']) > 0) {
- $fullContent = isset($response_arr['choices'][0]['message']['content']) ? $response_arr['choices'][0]['message']['content'] : '';
- $fullReasoningContent = isset($response_arr['choices'][0]['message']['reasoning_content']) ? $response_arr['choices'][0]['message']['reasoning_content'] : '';
- }
- return [
- 'fullContent' => $fullContent,
- 'fullReasoningContent' => $fullReasoningContent,
- 'usage' => $usage
- ];
- }
- private function splitContent($content) {
- // 根据章节标题来拆分章节内容,返回包含标题和内容的二维数组
- $chapters = [];
-
- // 匹配不同格式的章节标题:###第X章、##第X章、第X章
- $pattern = '/^(#{0,3}\s*第[^章]*章[^\n]*)\n(.*?)(?=\n#{0,3}\s*第[^章]*章|\z)/ms';
-
- if (preg_match_all($pattern, $content, $matches, PREG_SET_ORDER)) {
- foreach ($matches as $match) {
- $title = trim($match[1], "# \t\n\r\0\x0B"); // 去除标题前的#号和空白字符
- $chapterContent = trim($match[2]); // 去除内容前后的空白字符
-
- if (!empty($title) && !empty($chapterContent)) {
- $chapters[] = [
- 'title' => $title,
- 'content' => $chapterContent
- ];
- }
- }
- }else {
- $chapters[] = [
- 'title' => '',
- 'content' => $content
- ];
- }
-
- return $chapters;
- }
- public function getContentByBid($bid) {
- $chapters = DB::table('chapters as c')->select('c.id', 'c.name', 'cc.content')
- ->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)->orderBy('c.sequence')->get();
- $content = '';
- foreach ($chapters as $chapter) {
- $content .= $chapter->content . PHP_EOL . PHP_EOL;
- }
- return $content;
- }
- public function getContentByScriptId($script_id) {
- $content = DB::table('mp_scripts')->where('id', $script_id)->value('content');
- if (!$content) $content = '';
- return $content;
- }
- /**
- * 根据文件类型提取文本内容
- *
- * @param mixed $file 文件对象或文件路径
- * @return string
- */
- public function extractFileContent($file) {
- // 获取文件路径和扩展名
- if (is_string($file)) {
- $filePath = $file;
- $extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
- } else {
- // Laravel UploadedFile 对象
- $filePath = $file->getRealPath();
- $extension = strtolower($file->getClientOriginalExtension());
- }
- $content = '';
- switch ($extension) {
- case 'txt':
- $content = $this->extractTxtContent($filePath);
- break;
- case 'pdf':
- $content = $this->extractPdfContent($filePath);
- break;
- case 'doc':
- case 'docx':
- $content = $this->extractWordContent($filePath);
- break;
- // case 'jpg':
- // case 'jpeg':
- // case 'png':
- // case 'gif':
- // case 'bmp':
- // case 'webp':
- // $content = $this->extractImageContent($filePath);
- // break;
- default:
- Utils::throwError('20003:不支持的文件格式,支持:txt、pdf');
- }
- return $content;
- }
- /**
- * 提取 TXT 文件内容
- */
- private function extractTxtContent($filePath) {
- if (!file_exists($filePath)) {
- Utils::throwError('20003:文件不存在');
- }
- $content = file_get_contents($filePath);
- // 尝试检测并转换编码
- $encoding = mb_detect_encoding($content, ['UTF-8', 'GBK', 'GB2312', 'BIG5'], true);
- if ($encoding && $encoding !== 'UTF-8') {
- $content = mb_convert_encoding($content, 'UTF-8', $encoding);
- }
- return trim($content);
- }
- /**
- * 提取 PDF 文件内容
- */
- private function extractPdfContent($filePath) {
- if (!file_exists($filePath)) {
- Utils::throwError('20003:文件不存在');
- }
- try {
- $parser = new PdfParser();
- $pdf = $parser->parseFile($filePath);
- $content = $pdf->getText();
- return trim($content);
- } catch (\Exception $e) {
- dLog('deepseek')->error('PDF解析失败: ' . $e->getMessage());
- logDB('deepseek', 'error', 'PDF解析失败', ['error' => $e->getMessage()]);
- Utils::throwError('20003:PDF解析失败,请确保文件格式正确');
- }
- }
- /**
- * 提取 Word 文件内容(支持 doc 和 docx)
- */
- private function extractWordContent($filePath) {
- if (!file_exists($filePath)) {
- Utils::throwError('20003:文件不存在');
- }
- try {
- $phpWord = WordIOFactory::load($filePath);
- $content = '';
- foreach ($phpWord->getSections() as $section) {
- foreach ($section->getElements() as $element) {
- $content .= $this->extractWordElementText($element);
- }
- }
- return trim($content);
- } catch (\Exception $e) {
- dLog('deepseek')->error('Word解析失败: ' . $e->getMessage());
- logDB('deepseek', 'error', 'Word解析失败', ['error' => $e->getMessage()]);
- Utils::throwError('20003:Word解析失败,请确保文件格式正确');
- }
- }
- /**
- * 递归提取 Word 元素中的文本
- */
- private function extractWordElementText($element) {
- $text = '';
- if (method_exists($element, 'getText')) {
- $text .= $element->getText() . "\n";
- } elseif (method_exists($element, 'getElements')) {
- foreach ($element->getElements() as $childElement) {
- $text .= $this->extractWordElementText($childElement);
- }
- }
- return $text;
- }
- /**
- * 提取图片内容(使用火山引擎 OCR)
- */
- private function extractImageContent($filePath) {
- if (!file_exists($filePath)) {
- Utils::throwError('20003:文件不存在');
- }
- try {
- // 读取图片并转换为 base64
- $imageData = file_get_contents($filePath);
- $base64Image = base64_encode($imageData);
- // 调用火山引擎 OCR 服务
- $content = $this->callVolcEngineOCR($base64Image);
- return trim($content);
- } catch (\Exception $e) {
- dLog('deepseek')->error('图片OCR识别失败: ' . $e->getMessage());
- logDB('deepseek', 'error', '图片OCR识别失败', ['error' => $e->getMessage()]);
- Utils::throwError('20003:图片识别失败');
- }
- }
- /**
- * 调用火山引擎 OCR 服务识别图片文字
- *
- * @param string $base64Image base64 编码的图片数据
- * @return string 识别的文字内容
- */
- private function callVolcEngineOCR($base64Image) {
- $method = 'POST';
- $service = 'cv';
- $host = 'visual.volcengineapi.com';
- $region = env('VOLC_REGION', 'cn-north-1');
- $access_key = env('VOLC_AK');
- $secret_key = env('VOLC_SK');
- $action = 'OCRNormal';
- $version = '2020-08-26';
- if (!$access_key || !$secret_key) {
- Utils::throwError('20003:请配置火山引擎 AK/SK');
- }
- // 请求体
- $body = json_encode([
- 'image_base64' => $base64Image
- ]);
- // 生成签名
- $headers = $this->getVolcEngineSignHeaders(
- $method, $service, $host, $region,
- "Action={$action}&Version={$version}",
- $access_key, $secret_key, $body
- );
- $client = new Client(['timeout' => 60, 'verify' => false]);
- $response = $client->post("https://{$host}/?Action={$action}&Version={$version}", [
- 'headers' => $headers,
- 'body' => $body
- ]);
- $response_arr = json_decode($response->getBody()->getContents(), true);
- // 提取识别的文字
- $textLines = [];
- if (isset($response_arr['data']['line_texts'])) {
- $textLines = $response_arr['data']['line_texts'];
- } elseif (isset($response_arr['data']['ocr_infos'])) {
- foreach ($response_arr['data']['ocr_infos'] as $info) {
- if (isset($info['text'])) {
- $textLines[] = $info['text'];
- }
- }
- }
- if (empty($textLines)) {
- dLog('deepseek')->warning('OCR识别结果为空', ['response' => $response_arr]);
- return '';
- }
- return implode("\n", $textLines);
- }
- /**
- * 生成火山引擎 API 签名头(用于 OCR 等 POST 请求)
- */
- private function getVolcEngineSignHeaders($method, $service, $host, $region, $queryString, $access_key, $secret_key, $body = '') {
- $contentType = 'application/json';
- $accept = 'application/json';
-
- // 获取当前UTC时间
- $t = new DateTime('now', new DateTimeZone('UTC'));
- $xDate = $t->format('Ymd\THis\Z');
- $dateStamp = $t->format('Ymd');
-
- // 计算 body 的 sha256 哈希
- $payloadHash = hash('sha256', $body);
-
- // 1. 拼接规范请求串
- $canonicalUri = '/';
- $canonicalQueryString = $queryString;
- $canonicalHeaders = "content-type:{$contentType}\nhost:{$host}\nx-content-sha256:{$payloadHash}\nx-date:{$xDate}\n";
- $signedHeaders = 'content-type;host;x-content-sha256;x-date';
-
- $canonicalRequest = implode("\n", [
- $method,
- $canonicalUri,
- $canonicalQueryString,
- $canonicalHeaders,
- $signedHeaders,
- $payloadHash
- ]);
-
- // 2. 拼接待签名字符串
- $algorithm = 'HMAC-SHA256';
- $credentialScope = implode('/', [$dateStamp, $region, $service, 'request']);
- $hashedCanonicalRequest = hash('sha256', $canonicalRequest);
-
- $stringToSign = implode("\n", [
- $algorithm,
- $xDate,
- $credentialScope,
- $hashedCanonicalRequest
- ]);
-
- // 3. 计算签名
- $signingKey = $this->getSignatureKey($secret_key, $dateStamp, $region, $service);
- $signature = hash_hmac('sha256', $stringToSign, $signingKey);
-
- // 4. 添加签名到请求头
- $authorizationHeader = sprintf(
- '%s Credential=%s/%s, SignedHeaders=%s, Signature=%s',
- $algorithm,
- $access_key,
- $credentialScope,
- $signedHeaders,
- $signature
- );
-
- return [
- 'Accept' => $accept,
- 'Content-Type' => $contentType,
- 'Host' => $host,
- 'X-Date' => $xDate,
- 'X-Content-Sha256'=> $payloadHash,
- 'Authorization' => $authorizationHeader
- ];
- }
- public function generateScriptWords($cid, $model = 'r1') {
- ini_set('max_execution_time', 0);
- if (!$cid) Utils::throwError('20003: 请选择章节!');
- $content = DB::table('chapters as c')->leftJoin('chapter_contents as cc', 'c.chapter_content_id', '=', 'cc.id')->where('c.id', $cid)->value('cc.content');
-
- // 模型兼容性处理和思考模式设置
- $thinkingMode = 'disabled';
- $reasoningEffort = 'high';
-
- if ($model == 'r1') {
- $model = 'deepseek-v4-flash'; // r1 映射到 deepseek-v4-flash 思考模式
- $thinkingMode = 'enabled';
- } else {
- $model = 'deepseek-v4-flash'; // 默认映射到 deepseek-v4-flash 非思考模式
- $thinkingMode = 'disabled';
- }
-
- $messages = [
- [
- 'role' => 'system',
- 'content' => '下面有一段小说文本,请帮我将文本中的每句话按从上到下的顺序拆分成角色不同的剧本文稿(不得更改上下文顺序和内容),文稿形式严格按照“角色名(男或女):台词{情感}”输出,需要注意以下几点要求:
- 1.角色名后不要加入任何其他词语,只能加不包括旁白的性别,在男或女中选
- 2.非对话部分请全部用旁白角色代替
- 3.情感必须在【开心、悲伤、生气、惊讶、恐惧、厌恶、激动、冷漠、中性、沮丧、撒娇、害羞、安慰鼓励、咆哮、温柔、自然讲述、情感电台、磁性、广告营销、气泡音、新闻播报、娱乐八卦】中选一个,不得使用其他词语'
- ],
- [
- 'role' => 'user',
- 'content' => $content
- ]
- ];
- $post_data = [
- 'model' => $model,
- 'messages' => $messages,
- // 'max_tokens' => 8192,
- 'temperature' => 1,
- 'frequency_penalty' => 0,
- 'presence_penalty' => 0,
- 'thinking' => ['type' => $thinkingMode],
- 'response_format' => [
- 'type' => 'text'
- ],
- 'stream' => false
- ];
- if ($thinkingMode == 'enabled') $post_data['reasoning_effort'] = $reasoningEffort;
- // 根据模型类型选择调用方法
- $fullContent = '';
-
- if (in_array($model, ['deepseek-reasoner', 'deepseek-chat', 'deepseek-v4-flash', 'deepseek-v4-pro'])) {
- // DeepSeek 官方模型使用 DeepSeek API
- $chatResult = $this->chatOnly($post_data);
- } else {
- // 其他模型使用火山引擎API
- $chatResult = $this->volcEngineChatCompletion($post_data);
- }
-
- if (is_array($chatResult)) {
- $fullContent = $chatResult['fullContent'];
- }
- return $fullContent;
- }
- /**
- * 智能化解析集数信息
- * 支持多种表达方式,如:改成3集、调整成5集、修改为10集等
- *
- * @param string $prompt 用户输入的提示词
- * @return int|null 解析出的集数,如果没有找到则返回null
- */
- private function extractEpisodeNumber($prompt)
- {
- if (empty($prompt)) {
- return null;
- }
-
- // 定义各种可能的表达模式
- $patterns = [
- // 基本模式:改成N集、调整成N集、修改成N集
- '/(?:改成|调整成|修改成|变成|设置成|设为|改为|调为|修改为|变为)\s*(\d+)\s*集/u',
-
- // 扩展模式:分成N集、拆分成N集、分割成N集
- '/(?:分成|拆分成|分割成|划分成|分为)\s*(\d+)\s*集/u',
-
- // 数量模式:N集、共N集、总共N集
- '/(?:共|总共|一共|总计)?\s*(\d+)\s*集/u',
-
- // 制作模式:制作N集、生成N集、创建N集
- '/(?:制作|生成|创建|产出|输出)\s*(\d+)\s*集/u',
-
- // 计划模式:计划N集、预计N集、打算N集
- '/(?:计划|预计|打算|准备|想要)\s*(\d+)\s*集/u',
-
- // 需要模式:需要N集、要N集
- '/(?:需要|要|需)\s*(\d+)\s*集/u',
-
- // 中文数字模式:改成三集、调整成五集等
- '/(?:改成|调整成|修改成|变成|设置成|设为|改为|调为|修改为|变为|分成|拆分成|分割成|划分成|分为|制作|生成|创建|产出|输出|计划|预计|打算|准备|想要|需要|要|需)\s*([一二三四五六七八九十]+)\s*集/u',
-
- // 英文数字模式
- '/(?: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',
- ];
-
- // 中文数字转阿拉伯数字的映射
- $chineseNumbers = [
- '一' => 1, '二' => 2, '三' => 3, '四' => 4, '五' => 5,
- '六' => 6, '七' => 7, '八' => 8, '九' => 9, '十' => 10,
- '十一' => 11, '十二' => 12, '十三' => 13, '十四' => 14, '十五' => 15,
- '十六' => 16, '十七' => 17, '十八' => 18, '十九' => 19, '二十' => 20
- ];
-
- // 逐个尝试匹配模式
- foreach ($patterns as $pattern) {
- if (preg_match($pattern, $prompt, $matches)) {
- $number = trim($matches[1]);
-
- // 如果是中文数字,转换为阿拉伯数字
- if (isset($chineseNumbers[$number])) {
- return $chineseNumbers[$number];
- }
-
- // 如果是阿拉伯数字,直接返回
- if (is_numeric($number)) {
- $episodeNum = intval($number);
- // 合理性检查:集数应该在1-100之间
- if ($episodeNum >= 1 && $episodeNum <= 100) {
- return $episodeNum;
- }
- }
- }
- }
-
- return null;
- }
- /**
- * 调用火山引擎对话(Chat) API
- *
- * @param array $params 包含以下参数:
- * - model: 模型ID(必填)
- * - messages: 消息列表(必填)
- * - stream: 是否流式输出(可选,默认false)
- * - max_tokens: 最大输出token数(可选)
- * - temperature: 采样温度(可选,默认1)
- * - top_p: 核采样概率(可选,默认0.7)
- * - frequency_penalty: 频率惩罚系数(可选,默认0)
- * - presence_penalty: 存在惩罚系数(可选,默认0)
- * - stop: 停止词(可选)
- * - tools: 工具列表(可选)
- * @return array|Generator 非流式返回数组,流式返回生成器
- */
- public function volcEngineChatCompletion(array $params)
- {
- $apiKey = env('VOLC_AI_API_KEY');
-
- if (empty($apiKey)) {
- Utils::throwError('20003:火山引擎API Key未配置,请检查环境变量VOLC_AI_API_KEY');
- }
- // 构建请求参数
- $originalModel = $params['model'] ?? 'deepseek-v3-2-251201';
- $requestData = [
- 'model' => isset(\App\Consts\BaseConst::MODELS_MAP[$originalModel]) ? \App\Consts\BaseConst::MODELS_MAP[$originalModel] : $originalModel,
- 'messages' => $params['messages'] ?? [],
- ];
- if (!in_array($originalModel, $this->valid_text_models)) {
- Utils::throwError('20003:该模型不支持!');
- }
- // 添加可选参数
- if (isset($params['stream'])) {
- $requestData['stream'] = $params['stream'];
- }
-
- if (isset($params['stream_options'])) {
- $requestData['stream_options'] = $params['stream_options'];
- }
- if (isset($params['max_tokens'])) {
- $requestData['max_tokens'] = $params['max_tokens'];
- }
- if (isset($params['max_completion_tokens'])) {
- $requestData['max_completion_tokens'] = $params['max_completion_tokens'];
- }
- if (isset($params['temperature'])) {
- $requestData['temperature'] = $params['temperature'];
- }
- if (isset($params['top_p'])) {
- $requestData['top_p'] = $params['top_p'];
- }
- if (isset($params['frequency_penalty'])) {
- $requestData['frequency_penalty'] = $params['frequency_penalty'];
- }
- if (isset($params['presence_penalty'])) {
- $requestData['presence_penalty'] = $params['presence_penalty'];
- }
- if (isset($params['stop'])) {
- $requestData['stop'] = $params['stop'];
- }
- if (isset($params['tools'])) {
- $requestData['tools'] = $params['tools'];
- }
- if (isset($params['tool_choice'])) {
- $requestData['tool_choice'] = $params['tool_choice'];
- }
- if (isset($params['response_format'])) {
- $requestData['response_format'] = $params['response_format'];
- }
- if (isset($params['thinking'])) {
- // 如果 thinking 已经是数组格式,直接使用;否则转换为正确格式
- if (is_array($params['thinking'])) {
- $requestData['thinking'] = $params['thinking'];
- if (isset($params['reasoning_effort']) && $params['thinking']['type'] == 'enabled') {
- $requestData['reasoning_effort'] = $params['reasoning_effort'];
- }
- } else {
- $requestData['thinking'] = ['type' => $params['thinking']];
- if (isset($params['reasoning_effort']) && $params['thinking'] == 'enabled') {
- $requestData['reasoning_effort'] = $params['reasoning_effort'];
- }
- }
- } else {
- $requestData['thinking'] = ['type' => 'disabled'];
- }
- dLog('deepseek')->info('火山引擎对话API请求参数: ', $requestData);
- $client = new Client(['verify' => false, 'timeout' => 1200]);
- try {
- // 判断是否为流式输出
- $isStream = isset($params['stream']) && $params['stream'] === true;
- if ($isStream) {
- // 流式输出
- return $this->volcEngineChatCompletionStream($client, $apiKey, $requestData);
- } else {
- // 非流式输出
- $response = $client->post('https://ark.cn-beijing.volces.com/api/v3/chat/completions', [
- 'headers' => [
- 'Authorization' => 'Bearer ' . $apiKey,
- 'Content-Type' => 'application/json',
- ],
- 'json' => $requestData
- ]);
- $responseData = json_decode($response->getBody(), true);
- dLog('deepseek')->info('火山引擎对话API响应: ', $responseData);
- $usage = isset($responseData['usage']) ? $responseData['usage'] : [];
- if (isset($responseData['choices']) && count($responseData['choices']) > 0) {
- $fullContent = isset($responseData['choices'][0]['message']['content']) ? $responseData['choices'][0]['message']['content'] : '';
- $fullReasoningContent = isset($responseData['choices'][0]['message']['reasoning_content']) ? $responseData['choices'][0]['message']['reasoning_content'] : '';
- }
- return [
- 'fullContent' => $fullContent,
- 'fullReasoningContent' => $fullReasoningContent,
- 'usage' => $usage
- ];
- }
- } catch (\Exception $e) {
- dLog('deepseek')->error('火山引擎对话API请求异常: ' . $e->getMessage());
- logDB('deepseek', 'error', '火山引擎对话API请求失败', ['error' => $e->getMessage(), 'model' => $requestData['model']]);
- Utils::throwError('20003:火山引擎对话API请求失败: ' . $e->getMessage());
- }
- }
- /**
- * 火山引擎对话API流式输出
- *
- * @param Client $client HTTP客户端
- * @param string $apiKey API密钥
- * @param array $requestData 请求数据
- * @return Generator
- */
- private function volcEngineChatCompletionStream($client, $apiKey, $requestData)
- {
- try {
- $response = $client->post('https://ark.cn-beijing.volces.com/api/v3/chat/completions', [
- 'headers' => [
- 'Authorization' => 'Bearer ' . $apiKey,
- 'Content-Type' => 'application/json',
- ],
- 'json' => $requestData,
- 'stream' => true
- ]);
- $body = $response->getBody();
- $buffer = '';
- $fullContent = '';
- $fullReasoningContent = '';
- $usage = [];
- dLog('deepseek')->info('开始读取火山引擎对话API流式响应');
- // 逐行读取流式响应
- while (!$body->eof()) {
- $chunk = $body->read(1024);
- $buffer .= $chunk;
-
- // 按行分割
- $lines = explode("\n", $buffer);
- $buffer = array_pop($lines);
-
- foreach ($lines as $line) {
- $line = trim($line);
-
- if (empty($line)) {
- continue;
- }
-
- // 检查是否是SSE数据行
- if (strpos($line, 'data: ') !== 0) {
- continue;
- }
- $data = substr($line, 6);
-
- if ($data === '[DONE]') {
- dLog('deepseek')->info('收到结束标记');
- break 2;
- }
- try {
- $json = json_decode($data, true);
-
- if (json_last_error() !== JSON_ERROR_NONE) {
- dLog('deepseek')->warning('JSON解析错误: ' . json_last_error_msg());
- continue;
- }
-
- if (isset($json['choices'][0]['delta'])) {
- $delta = $json['choices'][0]['delta'];
-
- // 处理思维链内容
- if (isset($delta['reasoning_content'])) {
- $fullReasoningContent .= $delta['reasoning_content'];
- yield [
- 'type' => 'reasoning',
- 'content' => $delta['reasoning_content'],
- 'full_reasoning' => $fullReasoningContent
- ];
- }
-
- // 处理回答内容
- if (isset($delta['content'])) {
- $fullContent .= $delta['content'];
- yield [
- 'type' => 'content',
- 'content' => $delta['content'],
- ];
- }
- }
-
- // 获取使用统计信息
- if (isset($json['usage'])) {
- $usage = $json['usage'];
- dLog('deepseek')->info('收到使用统计', ['usage' => $usage]);
- }
-
- } catch (\Exception $e) {
- dLog('deepseek')->error('解析流式响应失败: ' . $e->getMessage());
- continue;
- }
- }
- }
- dLog('deepseek')->info('流式读取完成', [
- 'content_length' => strlen($fullContent),
- 'reasoning_length' => strlen($fullReasoningContent)
- ]);
- yield [
- 'type' => 'done',
- 'full_content' => $fullContent,
- 'full_reasoning' => $fullReasoningContent,
- 'usage' => $usage
- ];
- } catch (\Exception $e) {
- dLog('deepseek')->error('火山引擎对话API流式请求异常: ' . $e->getMessage());
- logDB('deepseek', 'error', '火山引擎对话API流式请求失败', ['error' => $e->getMessage()]);
- throw $e;
- }
- }
- /**
- * 检查并创建图片生成任务
- *
- * @param array $merged_items 合并后的资源列表(roles 或 scenes)
- * @param array $global_items 全局资源列表(来自 mp_animes 表)
- * @param string $nameKey 资源名称字段('role' 或 'scene')
- * @param string $art_style 美术风格
- * @return array 更新后的资源列表
- */
- private function checkAndCreateImageTasks(array $merged_items, array $global_items, string $nameKey, string $art_style = ''): array
- {
- // 构建全局资源映射表,用于快速查找
- $global_map = [];
- foreach ($global_items as $item) {
- $itemKey = $this->normalizeEpisodeResourceKey(getProp($item, $nameKey));
- if ($itemKey === '') {
- continue;
- }
- $global_map[$itemKey] = $item;
- }
- // 遍历合并后的资源列表,检查是否需要创建图片生成任务
- foreach ($merged_items as &$item) {
- $item_name = getProp($item, $nameKey);
- $item_description = getProp($item, 'description');
- // $item_url = getProp($item, 'url');
-
- // // 如果已有图片URL,跳过
- // if (!empty($item_url)) {
- // continue;
- // }
-
- // 如果是旁白角色,跳过
- if ($nameKey === 'role' && $item_name === '旁白') {
- continue;
- }
-
- $itemKey = $this->normalizeEpisodeResourceKey($item_name);
- if ($itemKey === '') {
- continue;
- }
-
- $need_create_task = false;
-
- // 条件1:在全局资源中找不到匹配的名称
- if (!isset($global_map[$itemKey])) {
- $need_create_task = true;
- } else {
- // 条件2:名称匹配但描述不一致
- $global_description = trim((string)getProp($global_map[$itemKey], 'description'));
- $current_description = trim((string)$item_description);
-
- if ($global_description !== $current_description) {
- $need_create_task = true;
- }
- }
-
- // 如果需要创建任务,调用图片生成服务
- if ($need_create_task) {
- try {
- $description = $item_description;
-
- // 添加美术风格前缀
- if ($art_style) {
- $prefix = $nameKey === 'role' ? '主体描述' : '场景描述';
- $description = "美术风格:\n{$art_style}\n\n{$prefix}:{$description}";
- }
-
- $params = [
- 'prompt' => $description,
- 'ref_img_urls' => []
- ];
-
- $task = $this->aiImageGenerationService->createImageGenerationTask($params);
- $task_id = $task->id;
-
- if ($task_id) {
- $item['task_id'] = $task_id;
- $item['task_status'] = 'processing';
-
- $resource_type = $nameKey === 'role' ? '角色' : '场景';
- dLog('deepseek')->info("{$resource_type}({$item_name})创建图片生成任务成功", ['task_id' => $task_id]);
- }
- } catch (\Exception $e) {
- $resource_type = $nameKey === 'role' ? '角色' : '场景';
- dLog('deepseek')->error("{$resource_type}({$item_name})创建图片生成任务失败: " . $e->getMessage());
- logDB('deepseek', 'error', "{$resource_type}创建图片生成任务失败", ['name' => $item_name, 'error' => $e->getMessage()]);
- // 不抛出异常,继续处理其他资源
- }
- }
- }
-
- return $merged_items;
- }
- /**
- * 解析分镜内容的公开方法
- */
- public function parseSegmentContent($segment_content, $model = null) {
- if (!$model) {
- $model = DB::table('mp_text_models')->where('is_enabled', 1)->orderBy('id')->value('model');
- if (!$model) {
- return $segment_content;
- }
- }
- // 构建系统提示词
- $systemPrompt = "你是一个专业的分镜内容解析助手。请将用户提供的分镜内容解析成标准格式,包含以下字段:
- 画面描述:详细描述画面内容
- 构图设计:镜头构图方式
- 运镜调度:摄影机运动方式
- 配音角色:配音的角色名称
- 台词内容:角色说的话
- 画面类型:普通画面或对口型
- 场景:拍摄场景
- 出镜角色:画面中出现的角色
- 尾帧描述:镜头结束时的画面描述
- 情感:配音情感(开心、悲伤、生气、惊讶、恐惧、厌恶、激动、冷漠、中性、沮丧、撒娇、害羞、安慰鼓励、咆哮、温柔、自然讲述、情感电台、磁性、广告营销、气泡音、新闻播报、娱乐八卦)
- 性别:1(男)、2(女)、0(中性)
- 语速:-50到100之间的整数
- 音量:-50到100之间的整数
- 情感强度:1到5之间的整数
- 音调:-12到12之间的整数
- 请严格按照上述格式输出,如果某个字段无法确定,请设置合理的默认值。";
- // 构建消息
- $messages = [
- [
- 'role' => 'system',
- 'content' => $systemPrompt
- ],
- [
- 'role' => 'user',
- 'content' => $segment_content
- ]
- ];
- $post_data = [
- 'model' => $model,
- 'messages' => $messages,
- // 'max_tokens' => 2048,
- 'temperature' => 0.7,
- 'frequency_penalty' => 0,
- 'presence_penalty' => 0,
- 'response_format' => ['type' => 'text'],
- 'stream' => false
- ];
- try {
- if (in_array($model, ['deepseek-reasoner', 'deepseek-chat', 'deepseek-v4-flash', 'deepseek-v4-pro'])) {
- // 使用DeepSeek API
- $chatResult = $this->chatOnly($post_data);
- } else {
- // 使用火山引擎API
- $chatResult = $this->volcEngineChatCompletion($post_data);
- }
-
- if (is_array($chatResult) && !empty($chatResult['fullContent'])) {
- return $chatResult['fullContent'];
- }
- } catch (\Exception $e) {
- dLog('deepseek')->error('解析分镜内容失败: ' . $e->getMessage());
- logDB('deepseek', 'error', '解析分镜内容失败', ['error' => $e->getMessage()]);
- }
- return $segment_content;
- }
- }
|