|
| 1 | + |
| 2 | + |
| 3 | +import Layout from "@/components/Layout"; |
| 4 | +import Button from "@/components/Common/button"; |
| 5 | +import Section from "@/components/Common/section"; |
| 6 | +import Alert from "@/components/Common/alert"; |
| 7 | +import ThemePlatformIcon from "@/components/Common/themeIcons" |
| 8 | +import Tabs from "@/components/Common/tab"; |
| 9 | +import Step from "@/components/Common/step"; |
| 10 | +import Card from "@/components/Common/card"; |
| 11 | +import Important from "@/components/Common/important"; |
| 12 | +import Highlight from "@/components/Common/highlight"; |
| 13 | +import Link from "next/link"; |
| 14 | +import PlatformIcon from "@/components/Common/icons"; |
| 15 | +import HighlightTabs from "@/components/Common/HighlightTabs"; |
| 16 | +import IconContainer from "@/components/Common/IconContainer"; |
| 17 | +import { |
| 18 | + GoContainer, |
| 19 | + GoDatabase, |
| 20 | + GoRocket, |
| 21 | + GoServer, |
| 22 | + GoMail, |
| 23 | + GoGlobe, |
| 24 | + GoArrowLeft, |
| 25 | + GoTelescope, |
| 26 | +} from "react-icons/go"; |
| 27 | + |
| 28 | +import Head from "next/head"; |
| 29 | + |
| 30 | + <Layout> |
| 31 | +<Head> |
| 32 | +<title>مستندات اتصال به AI در فریمورک Fastify - لیارا</title> |
| 33 | +<meta property="og:title" content="مستندات خدمات رایانش ابری لیارا" /> |
| 34 | +<meta property="og:description" content="مستندات مربوط به آشنایی با نحوه اتصال به سرویس هوش مصنوعی لیارا در فریمورک Fastify" /> |
| 35 | +<meta property="og:image" content="https://media.liara.ir/logos/liara-poster.jpg" /> |
| 36 | +</Head> |
| 37 | + |
| 38 | + |
| 39 | +# اتصال به هوش مصنوعی در فریمورک Fastify |
| 40 | +<hr className="mb-2" /> |
| 41 | +میتوانید از AI SDK در یک سرور Fastify استفاده کنید تا متن و آبجکتها را تولید کرده و بهصورت استریم به کلاینت ارسال نمایید. |
| 42 | + |
| 43 | +<hr className="mb-2" /> |
| 44 | + |
| 45 | +مثالهای زیر، یک سرور HTTP ساده راهاندازی میکنند که روی پورت <Important>8080</Important> در حال گوش کردن است. میتوانید با استفاده از دستور زیر در <Important>curl</Important>، سرور خود را آزمایش کنید: |
| 46 | + |
| 47 | +<div className="h-2" /> |
| 48 | +<div dir='ltr'> |
| 49 | + <Highlight className="bash"> |
| 50 | + {`curl -X POST http://localhost:8080`} |
| 51 | + </Highlight> |
| 52 | +</div> |
| 53 | +<div className="h-2" /> |
| 54 | + |
| 55 | +<Section id='data-stream' title='استریم دادهها' /> |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +<div className="h-2" /> |
| 61 | +در مسیر <Important>src/index.ts</Important> قطعه کد زیر را قرار دهید: |
| 62 | + |
| 63 | +<div className="h-2" /> |
| 64 | +<div dir='ltr'> |
| 65 | + <Highlight className="js"> |
| 66 | + {`import { createOpenAI } from '@ai-sdk/openai'; |
| 67 | +import { streamText } from 'ai'; |
| 68 | +import Fastify from 'fastify'; |
| 69 | +import { config } from 'dotenv'; |
| 70 | +
|
| 71 | +config(); |
| 72 | +
|
| 73 | +const my_model = createOpenAI({ |
| 74 | + baseURL: process.env.BASE_URL!, |
| 75 | + apiKey: process.env.LIARA_API_KEY!, |
| 76 | +}); |
| 77 | +
|
| 78 | +const fastify = Fastify({ logger: true }); |
| 79 | +
|
| 80 | +fastify.post('/', async function (request, reply) { |
| 81 | + const result = streamText({ |
| 82 | + model: my_model('openai/gpt-4o-mini'), |
| 83 | + prompt: 'Invent a new holiday and describe its traditions.', |
| 84 | + }); |
| 85 | +
|
| 86 | + reply.header('Content-Type', 'text/plain; charset=utf-8'); |
| 87 | +
|
| 88 | + return reply.send(result.toDataStream()); |
| 89 | +}); |
| 90 | +
|
| 91 | +fastify.listen({ port: 8080 });`} |
| 92 | + </Highlight> |
| 93 | +</div> |
| 94 | +<div className="h-2" /> |
| 95 | + |
| 96 | +<Alert variant="info"> |
| 97 | +<p> |
| 98 | +متغیرهای محیطی <Important>BASE_URL</Important> و <Important>LIARA_API_KEY</Important> همان baseUrl <a href="https://liara.ir/products/ai/" className="text-[#2196f3]">سرویس هوش مصنوعی لیارا</a> و <a href="/references/api/about/#api-access-key" className="text-[#2196f3]">کلید API لیارا</a> هستند که باید در بخش متغیرهای محیطی برنامه خود، آنها را تنظیم کنید. |
| 99 | +</p> |
| 100 | +</Alert> |
| 101 | + |
| 102 | + |
| 103 | +<hr className="mb-2" /> |
| 104 | +<Section id='sending-custom-data' title='ارسال دادههای سفارشی' /> |
| 105 | + |
| 106 | +متد <Important>createDataStream</Important> میتواند برای ارسال دادههای سفارشی به کلاینت مورد استفاده قرار گیرد. |
| 107 | + |
| 108 | +<div className="h-2" /> |
| 109 | +<div dir='ltr'> |
| 110 | + <Highlight className="js"> |
| 111 | + {`// curl -X POST http://localhost:8080/stream-data |
| 112 | +import { createOpenAI } from '@ai-sdk/openai'; |
| 113 | +import { createDataStream, streamText } from 'ai'; |
| 114 | +import Fastify from 'fastify'; |
| 115 | +import { config } from 'dotenv'; |
| 116 | +
|
| 117 | +config(); |
| 118 | +
|
| 119 | +const my_model = createOpenAI({ |
| 120 | + baseURL: process.env.BASE_URL!, |
| 121 | + apiKey: process.env.LIARA_API_KEY!, |
| 122 | +}); |
| 123 | +
|
| 124 | +const fastify = Fastify({ logger: true }); |
| 125 | +
|
| 126 | +fastify.post('/stream-data', async function (request, reply) { |
| 127 | + // immediately start streaming the response |
| 128 | + const dataStream = createDataStream({ |
| 129 | + execute: async dataStreamWriter => { |
| 130 | + dataStreamWriter.writeData('initialized call'); |
| 131 | +
|
| 132 | + const result = streamText({ |
| 133 | + model: my_model('openai/gpt-4o-mini'), |
| 134 | + prompt: 'Invent a new holiday and describe its traditions.', |
| 135 | + }); |
| 136 | +
|
| 137 | + result.mergeIntoDataStream(dataStreamWriter); |
| 138 | + }, |
| 139 | + onError: error => { |
| 140 | + // Error messages are masked by default for security reasons. |
| 141 | + // If you want to expose the error message to the client, you can do so here: |
| 142 | + return error instanceof Error ? error.message : String(error); |
| 143 | + }, |
| 144 | + }); |
| 145 | +
|
| 146 | + // Mark the response as a v1 data stream: |
| 147 | + reply.header('X-Vercel-AI-Data-Stream', 'v1'); |
| 148 | + reply.header('Content-Type', 'text/plain; charset=utf-8'); |
| 149 | +
|
| 150 | + return reply.send(dataStream); |
| 151 | +}); |
| 152 | +
|
| 153 | +fastify.listen({ port: 8080 });`} |
| 154 | + </Highlight> |
| 155 | +</div> |
| 156 | + |
| 157 | +<hr className="mb-2" /> |
| 158 | +<Section id='text-stream' title='استریم متن' /> |
| 159 | + |
| 160 | +میتوانید با استفاده از متد <Important>textStream</Important>، یک استریم متنی به کلاینت ارسال کنید. |
| 161 | + |
| 162 | +<div className="h-2" /> |
| 163 | +<div dir='ltr'> |
| 164 | + <Highlight className="js"> |
| 165 | + {`import { createOpenAI } from '@ai-sdk/openai'; |
| 166 | +import { streamText } from 'ai'; |
| 167 | +import Fastify from 'fastify'; |
| 168 | +import { config } from 'dotenv'; |
| 169 | +
|
| 170 | +
|
| 171 | +config(); |
| 172 | +
|
| 173 | +const my_model = createOpenAI({ |
| 174 | + baseURL: process.env.BASE_URL!, |
| 175 | + apiKey: process.env.LIARA_API_KEY!, |
| 176 | +}); |
| 177 | +const fastify = Fastify({ logger: true }); |
| 178 | +
|
| 179 | +fastify.post('/', async function (request, reply) { |
| 180 | + const result = streamText({ |
| 181 | + model: my_model('openai/gpt-4o-mini'), |
| 182 | + prompt: 'Invent a new holiday and describe its traditions.', |
| 183 | + }); |
| 184 | +
|
| 185 | + reply.header('Content-Type', 'text/plain; charset=utf-8'); |
| 186 | +
|
| 187 | + return reply.send(result.textStream); |
| 188 | +}); |
| 189 | +
|
| 190 | +fastify.listen({ port: 8080 });`} |
| 191 | + </Highlight> |
| 192 | +</div> |
| 193 | +<div className="h-2" /> |
| 194 | + |
| 195 | + |
| 196 | +<Alert variant="success"> |
| 197 | +<p> |
| 198 | +پروژه فوق را میتوانید بهصورت کامل در <a href="https://github.com/liara-cloud/ai-sdk-examples/tree/master/API-Servers/fastify" className="text-[#2196f3]">گیتهاب لیارا</a>، مشاهده کنید. |
| 199 | +</p> |
| 200 | +</Alert> |
| 201 | + |
| 202 | +</Layout> |
| 203 | + |
0 commit comments