|
| 1 | +import Layout from "@/components/Layout"; |
| 2 | +import Button from "@/components/Common/button"; |
| 3 | +import Section from "@/components/Common/section"; |
| 4 | +import Alert from "@/components/Common/alert"; |
| 5 | +import ThemePlatformIcon from "@/components/Common/themeIcons" |
| 6 | +import Tabs from "@/components/Common/tab"; |
| 7 | +import Step from "@/components/Common/step"; |
| 8 | +import Card from "@/components/Common/card"; |
| 9 | +import Important from "@/components/Common/important"; |
| 10 | +import Highlight from "@/components/Common/highlight"; |
| 11 | +import Link from "next/link"; |
| 12 | +import PlatformIcon from "@/components/Common/icons"; |
| 13 | +import HighlightTabs from "@/components/Common/HighlightTabs"; |
| 14 | +import IconContainer from "@/components/Common/IconContainer"; |
| 15 | +import { |
| 16 | + GoContainer, |
| 17 | + GoDatabase, |
| 18 | + GoRocket, |
| 19 | + GoServer, |
| 20 | + GoMail, |
| 21 | + GoGlobe, |
| 22 | + GoArrowLeft, |
| 23 | + GoTelescope, |
| 24 | +} from "react-icons/go"; |
| 25 | + |
| 26 | +import Head from "next/head"; |
| 27 | + |
| 28 | +<Layout> |
| 29 | +<Head> |
| 30 | +<title>مستندات اتصال تیمها به AI - لیارا</title> |
| 31 | +<meta property="og:title" content="مستندات خدمات رایانش ابری لیارا" /> |
| 32 | +<meta property="og:description" content="مستندات مربوط به نحوه اتصال تیمها به سرویس هوش مصنوعی لیارا" /> |
| 33 | +<meta property="og:image" content="https://media.liara.ir/logos/liara-poster.jpg" /> |
| 34 | +</Head> |
| 35 | + |
| 36 | + |
| 37 | +# نحوه اتصال تیمها به هوش مصنوعی |
| 38 | +<hr className="mb-2" /> |
| 39 | + |
| 40 | +اگر در <a href="/references/team/about/" className="text-[#2196f3]">تیم خود در لیارا</a>، یک سرویس هوش مصنوعی دارید و قصد دارید که در زبانهای برنامهنویسی مختلف، |
| 41 | +به آن، متصل شوید. تنها کافیست تا در تنظیمات اتصال به سرویس، یک هدر به نام <Important>x-teamid</Important> تنظیم کنید |
| 42 | +و در آن، <a href="/references/api/about/#team-id" className="text-[#2196f3]">شناسه تیم</a> خود را قرار دهید. |
| 43 | + |
| 44 | +<div className="h-2" /> |
| 45 | + |
| 46 | + |
| 47 | +در ادامه، نحوه اتصال به سرویس هوش مصنوعی در تیمها با تنظیم شناسه تیم بهعنوان هدر، قرار گرفته است: |
| 48 | + |
| 49 | +<Tabs |
| 50 | + tabs={["OpenAI SDK", "AI SDK"]} |
| 51 | + content={[ |
| 52 | + <> |
| 53 | + <Tabs |
| 54 | + tabs={[ |
| 55 | + { |
| 56 | + label: "cURL", |
| 57 | + }, |
| 58 | + { |
| 59 | + label: "JavaScript", |
| 60 | + icon: <PlatformIcon platform="nodejs" style={{ width: 32, height: 32 }} />, |
| 61 | + }, |
| 62 | + { |
| 63 | + label: "PHP", |
| 64 | + icon: <PlatformIcon platform="php" style={{ width: 32, height: 32 }} />, |
| 65 | + }, |
| 66 | + { |
| 67 | + label: "Python", |
| 68 | + icon: <PlatformIcon platform="python" style={{ width: 32, height: 32 }} />, |
| 69 | + }, |
| 70 | + { |
| 71 | + label: "NET.", |
| 72 | + icon: <PlatformIcon platform="dotnet" style={{ width: 32, height: 32 }} />, |
| 73 | + }, |
| 74 | + { |
| 75 | + label: "Go", |
| 76 | + icon: <PlatformIcon platform="go" style={{ width: 32, height: 32 }} />, |
| 77 | + }, |
| 78 | + ]} |
| 79 | + content={[ |
| 80 | + |
| 81 | + <> |
| 82 | + <div className="h-2" /> |
| 83 | + <div dir="ltr"> |
| 84 | + <Highlight className="bash"> |
| 85 | + {`curl <baseUrl>/chat/completions \\ |
| 86 | + -H "Content-Type: application/json" \\ |
| 87 | + -H "Authorization: Bearer <LIARA_API_KEY>" \\ |
| 88 | + -H "x-teamid: <team-id>" \\ |
| 89 | + -d '{ |
| 90 | + "model": "<model-name>", |
| 91 | + "messages": [ |
| 92 | + {"role": "system", "content": "You are a helpful assistant."}, |
| 93 | + {"role": "user", "content": "Hello!"} |
| 94 | + ] |
| 95 | +}' |
| 96 | +`} |
| 97 | + </Highlight> |
| 98 | + </div> |
| 99 | + <div className="h-2" /> |
| 100 | + </>, |
| 101 | + <> |
| 102 | + <div className="h-2" /> |
| 103 | + <div dir="ltr"> |
| 104 | + <Highlight className="js"> |
| 105 | + {`import OpenAI from 'openai'; |
| 106 | +
|
| 107 | +const openai = new OpenAI({ |
| 108 | + baseURL: '<baseUrl>', |
| 109 | + apiKey: '<LIARA_API_KEY>', |
| 110 | + defaultHeaders: { |
| 111 | + 'x-teamid': '<team-id>' |
| 112 | + }, |
| 113 | +}); |
| 114 | +
|
| 115 | +async function main() { |
| 116 | + const completion = await openai.chat.completions.create({ |
| 117 | + model: '<model-name>', |
| 118 | + messages: [ |
| 119 | + { |
| 120 | + role: 'user', |
| 121 | + content: 'Hello!', |
| 122 | + }, |
| 123 | + ], |
| 124 | + }); |
| 125 | +
|
| 126 | + console.log(completion.choices[0].message); |
| 127 | +} |
| 128 | +
|
| 129 | +main(); |
| 130 | +`} |
| 131 | + </Highlight> |
| 132 | + </div> |
| 133 | + <div className="h-2" /> |
| 134 | + </>, |
| 135 | + <> |
| 136 | + <div className="h-2" /> |
| 137 | + <div dir="ltr"> |
| 138 | + <Highlight className="php"> |
| 139 | + {`<?php |
| 140 | +
|
| 141 | +require 'vendor/autoload.php'; |
| 142 | +
|
| 143 | +use OpenAI\\Client; |
| 144 | +use OpenAI\\Laravel\\Facades\\OpenAI; |
| 145 | +
|
| 146 | +$yourApiKey = '<LIARA_API_KEY>'; |
| 147 | +$baseUrl = '<baseUrl>'; |
| 148 | +$model = '<model-name>'; |
| 149 | +
|
| 150 | +$client = \\OpenAI::factory() |
| 151 | + ->withApiKey($yourApiKey) |
| 152 | + ->withBaseUri($baseUrl) |
| 153 | + ->withHttpHeader('x-teamid', '<team-id>') |
| 154 | + ->make(); |
| 155 | +
|
| 156 | +$result = $client->chat()->create([ |
| 157 | + 'model' => $model, |
| 158 | + 'messages' => [ |
| 159 | + ['role' => 'user', 'content' => 'Hello!'], |
| 160 | + ], |
| 161 | +]); |
| 162 | +
|
| 163 | +echo $result->choices[0]->message->content; |
| 164 | +`} |
| 165 | + </Highlight> |
| 166 | + </div> |
| 167 | + <div className="h-2" /> |
| 168 | + </>, |
| 169 | + <> |
| 170 | + <div className="h-2" /> |
| 171 | + <div dir="ltr"> |
| 172 | + <Highlight className="python"> |
| 173 | + {`from openai import OpenAI |
| 174 | +
|
| 175 | +client = OpenAI( |
| 176 | + base_url="<baseUrl>", |
| 177 | + api_key="<LIARA_API_KEY>", |
| 178 | + default_headers={ |
| 179 | + "x-teamid": "<team-id>" |
| 180 | + } |
| 181 | +) |
| 182 | +
|
| 183 | +completion = client.chat.completions.create( |
| 184 | + model="<model-name>", |
| 185 | + messages=[ |
| 186 | + { |
| 187 | + "role": "user", |
| 188 | + "content": 'Hello!' |
| 189 | + } |
| 190 | + ] |
| 191 | +) |
| 192 | +
|
| 193 | +print(completion.choices[0].message.content) |
| 194 | +`} |
| 195 | + </Highlight> |
| 196 | + </div> |
| 197 | + <div className="h-2" /> |
| 198 | + </>, |
| 199 | + <> |
| 200 | + <div className="h-2" /> |
| 201 | + <div dir="ltr"> |
| 202 | + <Highlight className="dotnet"> |
| 203 | + {`using System.ClientModel; |
| 204 | +using System.ClientModel.Primitives; |
| 205 | +using OpenAI; |
| 206 | +using OpenAI.Chat; |
| 207 | +
|
| 208 | +class Program |
| 209 | +{ |
| 210 | + static void Main() |
| 211 | + { |
| 212 | + string apiKey = "<LIARA_API_KEY>"; |
| 213 | + |
| 214 | + string baseUrl = "<baseUrl>"; |
| 215 | + |
| 216 | + OpenAIClientOptions options = new OpenAIClientOptions |
| 217 | + { |
| 218 | + Endpoint = new Uri(baseUrl), |
| 219 | + Transport = new HttpClientPipelineTransport( |
| 220 | + new HttpClient |
| 221 | + { |
| 222 | + DefaultRequestHeaders = |
| 223 | + { |
| 224 | + { "x-teamid", "<team-id>" } |
| 225 | + } |
| 226 | + } |
| 227 | + ) |
| 228 | + } |
| 229 | +
|
| 230 | + ChatClient client = new(model: "<model-name>", credential: new ApiKeyCredential(apiKey), options: options); |
| 231 | +
|
| 232 | + ChatCompletion completion = client.CompleteChat("Hello!"); |
| 233 | +
|
| 234 | + Console.WriteLine($"[ASSISTANT]: {completion.Content[0].Text}"); |
| 235 | + } |
| 236 | +} |
| 237 | +`} |
| 238 | + </Highlight> |
| 239 | + </div> |
| 240 | + <div className="h-2" /> |
| 241 | + </>, |
| 242 | + <> |
| 243 | + <div className="h-2" /> |
| 244 | + <div dir="ltr"> |
| 245 | + <Highlight className="go"> |
| 246 | + {`package main |
| 247 | +
|
| 248 | +import ( |
| 249 | + "context" |
| 250 | + "fmt" |
| 251 | +
|
| 252 | + "github.com/openai/openai-go" |
| 253 | + "github.com/openai/openai-go/option" |
| 254 | +) |
| 255 | +
|
| 256 | +func main() { |
| 257 | + const model = "<model-name>" |
| 258 | +
|
| 259 | + client := openai.NewClient( |
| 260 | + option.WithAPIKey("<LIARA_API_KEY>"), |
| 261 | + option.WithBaseURL("<baseUrl>"), |
| 262 | + option.WithHeader("x-teamid", "<team-id>"), |
| 263 | + ) |
| 264 | +
|
| 265 | + chatCompletion, err := client.Chat.Completions.New(context.TODO(), openai.ChatCompletionNewParams{ |
| 266 | + Messages: []openai.ChatCompletionMessageParamUnion{ |
| 267 | + openai.UserMessage("Hello!"), |
| 268 | + }, |
| 269 | + Model: model, |
| 270 | + }) |
| 271 | + if err != nil { |
| 272 | + panic(err.Error()) |
| 273 | + } |
| 274 | +
|
| 275 | + fmt.Println("Model Response:", chatCompletion.Choices[0].Message.Content) |
| 276 | +} |
| 277 | +`} |
| 278 | + </Highlight> |
| 279 | + </div> |
| 280 | + <div className="h-2" /> |
| 281 | + </>, |
| 282 | + |
| 283 | + ]} |
| 284 | + /> |
| 285 | + |
| 286 | + </>, |
| 287 | + |
| 288 | + |
| 289 | + <> |
| 290 | + <Tabs |
| 291 | + tabs={[ |
| 292 | + { |
| 293 | + label: "JavaScript", |
| 294 | + icon: <PlatformIcon platform="nodejs" style={{ width: 32, height: 32 }} />, |
| 295 | + }, |
| 296 | + { |
| 297 | + label: "TypeScript", |
| 298 | + icon: <ThemePlatformIcon light="https://media.liara.ir/logos/typescript.svg" dark="https://media.liara.ir/logos/typescript.svg" style={{ width: 18, height: 18 }} />, |
| 299 | + }, |
| 300 | + |
| 301 | + ]} |
| 302 | + |
| 303 | + content ={[ |
| 304 | + <> |
| 305 | + |
| 306 | + <div className="h-2" /> |
| 307 | + <div dir="ltr"> |
| 308 | + <Highlight className="js"> |
| 309 | + {`// npm i @ai-sdk/openai-compatible |
| 310 | +import { createOpenAICompatible } from '@ai-sdk/openai-compatible'; |
| 311 | +import { generateText } from 'ai'; |
| 312 | +
|
| 313 | +const { text } = await generateText({ |
| 314 | + model: createOpenAICompatible({ |
| 315 | + baseURL: "<baseUrl>", |
| 316 | + name: 'example', |
| 317 | + apiKey: "<LIARA_API_KEY>", |
| 318 | + headers: { 'x-teamid': "<team-id>" }, |
| 319 | + }).chatModel("<model-name>"), |
| 320 | + prompt: 'hey.', |
| 321 | +}); |
| 322 | +
|
| 323 | +console.log('Generated Text:', text); |
| 324 | +`} |
| 325 | + </Highlight> |
| 326 | + </div> |
| 327 | + <div className="h-2" /> |
| 328 | + |
| 329 | + </>, |
| 330 | + |
| 331 | + <> |
| 332 | + <> |
| 333 | + <div className="h-2" /> |
| 334 | + <div dir="ltr"> |
| 335 | + <Highlight className="js"> |
| 336 | + {`// npm i @ai-sdk/openai-compatible |
| 337 | +import { createOpenAICompatible } from '@ai-sdk/openai-compatible'; |
| 338 | +import { generateText } from 'ai'; |
| 339 | +
|
| 340 | +async function main() { |
| 341 | + const model = createOpenAICompatible({ |
| 342 | + baseURL: "<baseUrl>", |
| 343 | + name: 'example', |
| 344 | + apiKey: "<LIARA_API_KEY>", |
| 345 | + headers: { 'x-teamid': "<team-id>" }, |
| 346 | + }).chatModel("<model-name>"); |
| 347 | +
|
| 348 | + const { text } = await generateText({ |
| 349 | + model, |
| 350 | + prompt: 'hey.', |
| 351 | +
|
| 352 | + }); |
| 353 | +
|
| 354 | + console.log('Generated Text:', text); |
| 355 | +} |
| 356 | +
|
| 357 | +main().catch(console.error); |
| 358 | +`} |
| 359 | + </Highlight> |
| 360 | + </div> |
| 361 | + <div className="h-2" /> |
| 362 | + </> |
| 363 | + </> |
| 364 | + ]} |
| 365 | + /> |
| 366 | + </> |
| 367 | + |
| 368 | + ]} |
| 369 | + |
| 370 | +/> |
| 371 | + |
| 372 | +<p> |
| 373 | + در قطعه کدهای فوق، بهجای <Important><baseUrl></Important>، آدرس سرویس هوش مصنوعی خود را قرار دهید و بهجای <Important><LIARA_API_KEY></Important>، کلید API خود را وارد کنید. همچنین، بهجای <Important><model-name></Important>، نام یکی از مدلهای فوق را قرار دهید. |
| 374 | + مقدار <Important><team-id></Important>، باید شناسه تیم مدنظرتان، باشد. |
| 375 | +</p> |
| 376 | + |
| 377 | +</Layout> |
0 commit comments