Supercharge Generative AI ที่ให้บริการกับ Friendli
ไคลเอนต์ Friendli นำเสนออินเทอร์เฟซที่สะดวกสบายในการโต้ตอบกับบริการปลายทางที่ Friendli Suite มอบให้ ซึ่งเป็นโซลูชั่นขั้นสูงสุดสำหรับการให้บริการโมเดล AI เชิงสร้างสรรค์ ออกแบบมาเพื่อความยืดหยุ่นและประสิทธิภาพ รองรับการทำงานทั้งแบบซิงโครนัสและอะซิงโครนัส ทำให้ง่ายต่อการรวมความสามารถ AI อันทรงพลังเข้ากับแอปพลิเคชันของคุณ
ในการเริ่มต้นกับ Friendli ให้ติดตั้งแพ็คเกจไคลเอนต์โดยใช้ pip
:
pip install friendli-client
สำคัญ
คุณต้องตั้งค่าตัวแปรสภาพแวดล้อม FRIENDLI_TOKEN
ก่อนที่จะเริ่มต้นอินสแตนซ์ไคลเอ็นต์ด้วย client = Friendli()
หรือคุณสามารถระบุค่าของโทเค็นการเข้าถึงส่วนบุคคลของคุณเป็นอาร์กิวเมนต์ token
เมื่อสร้างไคลเอ็นต์ได้ เช่น:
from friendli import Friendli
client = Friendli ( token = "YOUR PERSONAL ACCESS TOKEN" )
Friendli Serverless Endpoint นำเสนออินเทอร์เฟซแบบคลิกและเล่นที่เรียบง่ายสำหรับการเข้าถึงโมเดลโอเพ่นซอร์สยอดนิยม เช่น Llama 3.1 ด้วยการเรียกเก็บเงินแบบจ่ายต่อโทเค็น เหมาะอย่างยิ่งสำหรับการสำรวจและการทดลอง
หากต้องการโต้ตอบกับโมเดลที่โฮสต์โดยตำแหน่งข้อมูลแบบไร้เซิร์ฟเวอร์ ให้ระบุโค้ดโมเดลที่คุณต้องการใช้ในอาร์กิวเมนต์ model
โปรดดูตารางราคาสำหรับรายการรหัสรุ่นที่มีจำหน่ายและราคา
from friendli import Friendli
client = Friendli ()
chat_completion = client . chat . completions . create (
model = "meta-llama-3.1-8b-instruct" ,
messages = [
{
"role" : "user" ,
"content" : "Tell me how to make a delicious pancake" ,
}
],
)
print ( chat_completion . choices [ 0 ]. message . content )
อุปกรณ์ปลายทางเฉพาะของ Friendli ช่วยให้คุณสามารถเรียกใช้โมเดล AI ที่สร้างแบบกำหนดเองของคุณบนทรัพยากร GPU เฉพาะได้
หากต้องการโต้ตอบกับตำแหน่งข้อมูลเฉพาะ ให้ระบุ ID ตำแหน่งข้อมูลในอาร์กิวเมนต์ model
import os
from friendli import Friendli
client = Friendli (
team_id = os . environ [ "TEAM_ID" ], # If not provided, default team is used.
use_dedicated_endpoint = True ,
)
chat_completion = client . chat . completions . create (
model = os . environ [ "ENDPOINT_ID" ],
messages = [
{
"role" : "user" ,
"content" : "Tell me how to make a delicious pancake" ,
}
],
)
print ( chat_completion . choices [ 0 ]. message . content )
Friendli Container เหมาะสำหรับผู้ใช้ที่ต้องการให้บริการ LLM ภายในโครงสร้างพื้นฐานของตนเอง ด้วยการปรับใช้ Friendli Engine ในคอนเทนเนอร์บน GPU ภายในองค์กรหรือบนคลาวด์ คุณสามารถรักษาการควบคุมข้อมูลและการดำเนินงานของคุณได้อย่างสมบูรณ์ รับประกันความปลอดภัยและการปฏิบัติตามนโยบายภายใน
from friendli import Friendli
client = Friendli ( base_url = "http://0.0.0.0:8000" )
chat_completion = client . chat . completions . create (
messages = [
{
"role" : "user" ,
"content" : "Tell me how to make a delicious pancake" ,
}
],
)
print ( chat_completion . choices [ 0 ]. message . content )
import asyncio
from friendli import AsyncFriendli
client = AsyncFriendli ()
async def main () -> None :
chat_completion = await client . chat . completions . create (
model = "meta-llama-3.1-8b-instruct" ,
messages = [
{
"role" : "user" ,
"content" : "Tell me how to make a delicious pancake" ,
}
],
)
print ( chat_completion . choices [ 0 ]. message . content )
asyncio . run ( main ())
from friendli import Friendli
client = Friendli ()
stream = client . chat . completions . create (
model = "meta-llama-3.1-8b-instruct" ,
messages = [
{
"role" : "user" ,
"content" : "Tell me how to make a delicious pancake" ,
}
],
stream = True ,
)
for chunk in stream :
print ( chunk . choices [ 0 ]. delta . content or "" , end = "" , flush = True )
ไคลเอนต์ async ( AsyncFriendli
) ใช้อินเทอร์เฟซเดียวกันเพื่อสตรีมการตอบสนอง
import asyncio
from friendli import AsyncFriendli
client = AsyncFriendli ()
async def main () -> None :
stream = await client . chat . completions . create (
model = "meta-llama-3.1-8b-instruct" ,
messages = [
{
"role" : "user" ,
"content" : "Tell me how to make a delicious pancake" ,
}
],
stream = True ,
)
async for chunk in stream :
print ( chunk . choices [ 0 ]. delta . content or "" , end = "" , flush = True )
asyncio . run ( main ())
หากตำแหน่งข้อมูลของคุณให้บริการโมเดล Multi-LoRA คุณสามารถส่งคำขอไปยังอะแดปเตอร์ตัวใดตัวหนึ่งได้โดยระบุเส้นทางของอะแดปเตอร์ในอาร์กิวเมนต์ model
สำหรับ Friendli Dedicated Endpoints ให้ระบุ ID ตำแหน่งข้อมูลและเส้นทางของอะแด็ปเตอร์ที่คั่นด้วยเครื่องหมายทวิภาค ( :
)
import os
from friendli import Friendli
client = Friendli (
team_id = os . environ [ "TEAM_ID" ], # If not provided, default team is used.
use_dedicated_endpoint = True ,
)
chat_completion = client . lora . completions . create (
model = f" { os . environ [ 'ENDPOINT_ID' ] } : { os . environ [ 'ADAPTER_ROUTE' ] } " ,
messages = [
{
"role" : "user" ,
"content" : "Tell me how to make a delicious pancake" ,
}
],
)
สำหรับ Friendli Container เพียงระบุชื่ออะแดปเตอร์
import os
from friendli import Friendli
client = Friendli ( base_url = "http://0.0.0.0:8000" )
chat_completion = client . lora . completions . create (
model = os . environ [ "ADAPTER_NAME" ],
messages = [
{
"role" : "user" ,
"content" : "Tell me how to make a delicious pancake" ,
}
],
)
สำคัญ
gRPC รองรับเฉพาะ Friendli Container เท่านั้น และใช้ได้เฉพาะ API การสตรีม v1/completions
เท่านั้น
เมื่อ Frienldi Container ทำงานในโหมด gPRC ไคลเอ็นต์สามารถโต้ตอบกับเซิร์ฟเวอร์ gRPC ได้โดยกำหนดค่าเริ่มต้นด้วยอาร์กิวเมนต์ use_grpc=True
from friendli import Friendli
client = Friendli ( base_url = "0.0.0.0:8000" , use_grpc = True )
stream = client . chat . completions . create (
messages = [
{
"role" : "user" ,
"content" : "Tell me how to make a delicious pancake" ,
}
],
stream = True , # Only streaming mode is available
)
for chunk in stream :
print ( chunk . choices [ 0 ]. delta . content or "" , end = "" , flush = True )
ไคลเอนต์ใช้ httpx
เพื่อส่งคำขอ HTTP คุณสามารถระบุ httpx.Client
ที่กำหนดเองได้เมื่อเริ่มต้น Friendli
import httpx
from friendli import Friendli
with httpx . Client () as client :
client = Friendli ( http_client = http_client )
สำหรับไคลเอ็นต์ async คุณสามารถระบุ httpx.AsyncClient
ได้
import httx
from friendli import AsyncFriendli
with httpx . AsyncClient () as client :
client = AsyncFriendli ( http_client = http_client )
import grpc
from friendli import Friendli
with grpc . insecure_channel ( "0.0.0.0:8000" ) as channel :
client = Friendli ( use_grpc = True , grpc_channel = channel )
คุณสามารถใช้อินเทอร์เฟซเดียวกันสำหรับไคลเอ็นต์ async
import grpc . aio
from friendli import AsyncFriendli
async with grpc . aio . insecure_channel ( "0.0.0.0:8000" ) as channel :
client = AsyncFriendli ( use_grpc = True , grpc_channel = channel )
ไคลเอนต์ Friendli มีหลายวิธีในการจัดการและปล่อยทรัพยากร
ทั้งไคลเอนต์ Friendli
และ AsyncFriendli
สามารถเชื่อมต่อเครือข่ายหรือทรัพยากรอื่น ๆ ได้ตลอดอายุการใช้งาน เพื่อให้แน่ใจว่าทรัพยากรเหล่านี้ได้รับการเผยแพร่อย่างถูกต้อง คุณควรเรียกใช้เมธอด close()
หรือใช้ไคลเอ็นต์ภายในตัวจัดการบริบท
from friendli import Friendli
client = Friendli ()
# Use the client for various operations...
# When done, close the client to release resources
client . close ()
สำหรับไคลเอ็นต์แบบอะซิงโครนัส รูปแบบจะคล้ายกัน:
import asyncio
from friendli import AsyncFriendli
client = AsyncFriendli ()
# Use the client for various async operations...
# When done, close the client to release resources
await client . close ()
คุณยังสามารถใช้ตัวจัดการบริบทเพื่อปิดไคลเอ็นต์โดยอัตโนมัติและเผยแพร่ทรัพยากรเมื่อออกจากบล็อก ทำให้เป็นวิธีที่ปลอดภัยและสะดวกยิ่งขึ้นในการจัดการทรัพยากร
from friendli import Friendli
with Friendli () as client :
...
สำหรับการใช้งานแบบอะซิงโครนัส:
import asyncio
from friendli import AsyncFriendli
async def main ():
async with AsyncFriendli () as client :
...
asyncio . run ( main ())
เมื่อใช้การตอบสนองแบบสตรีม การปิดการเชื่อมต่อ HTTP อย่างถูกต้องหลังจากการโต้ตอบเสร็จสิ้นเป็นสิ่งสำคัญ ตามค่าเริ่มต้น การเชื่อมต่อจะถูกปิดโดยอัตโนมัติเมื่อมีการใช้ข้อมูลทั้งหมดจากสตรีม (เช่น เมื่อ for-loop ถึงจุดสิ้นสุด) อย่างไรก็ตาม หากการสตรีมถูกขัดจังหวะด้วยข้อยกเว้นหรือปัญหาอื่นๆ การเชื่อมต่ออาจยังคงเปิดอยู่และจะไม่ถูกปล่อยจนกว่าจะถูกรวบรวมแบบขยะ เพื่อให้แน่ใจว่าการเชื่อมต่อและทรัพยากรพื้นฐานทั้งหมดได้รับการเผยแพร่อย่างถูกต้อง สิ่งสำคัญคือต้องปิดการเชื่อมต่ออย่างชัดเจน โดยเฉพาะอย่างยิ่งเมื่อการสตรีมสิ้นสุดลงก่อนกำหนด
from friendli import Friendli
client = Friendli ()
stream = client . chat . completions . create (
model = "meta-llama-3.1-8b-instruct" ,
messages = [
{
"role" : "user" ,
"content" : "Tell me how to make a delicious pancake" ,
}
],
stream = True ,
)
try :
for chunk in stream :
print ( chunk . choices [ 0 ]. delta . content or "" , end = "" , flush = True )
finally :
stream . close () # Ensure the stream is closed after use
สำหรับการสตรีมแบบอะซิงโครนัส:
import asyncio
from friendli import AsyncFriendli
client = AsyncFriendli ()
async def main ():
stream = await client . chat . completions . create (
model = "meta-llama-3.1-8b-instruct" ,
messages = [
{
"role" : "user" ,
"content" : "Tell me how to make a delicious pancake" ,
}
],
stream = True ,
)
try :
async for chunk in stream :
print ( chunk . choices [ 0 ]. delta . content or "" , end = "" , flush = True )
finally :
await stream . close () # Ensure the stream is closed after use
asyncio . run ( main ())
คุณยังสามารถใช้ตัวจัดการบริบทเพื่อปิดไคลเอ็นต์โดยอัตโนมัติและเผยแพร่ทรัพยากรเมื่อออกจากบล็อก ทำให้เป็นวิธีที่ปลอดภัยและสะดวกยิ่งขึ้นในการจัดการทรัพยากร
from friendli import Friendli
client = Friendli ()
with client . chat . completions . create (
model = "meta-llama-3.1-8b-instruct" ,
messages = [
{
"role" : "user" ,
"content" : "Tell me how to make a delicious pancake" ,
}
],
stream = True ,
) as stream :
for chunk in stream :
print ( chunk . choices [ 0 ]. delta . content or "" , end = "" , flush = True )
สำหรับการสตรีมแบบอะซิงโครนัส:
import asyncio
from friendli import AsyncFriendli
client = AsyncFriendli ()
async def main ():
async with await client . chat . completions . create (
model = "meta-llama-3.1-8b-instruct" ,
messages = [
{
"role" : "user" ,
"content" : "Tell me how to make a delicious pancake" ,
}
],
stream = True ,
) as stream :
async for chunk in stream :
print ( chunk . choices [ 0 ]. delta . content or "" , end = "" , flush = True )
asyncio . run ( main ())
เมื่อใช้อินเทอร์เฟซ gRPC กับการสตรีม คุณอาจต้องการยกเลิกการดำเนินการสตรีมที่กำลังดำเนินอยู่ก่อนที่จะเสร็จสิ้น สิ่งนี้มีประโยชน์อย่างยิ่งหากคุณต้องการหยุดการสตรีมเนื่องจากการหมดเวลาหรือเงื่อนไขอื่น ๆ
สำหรับการสตรีม gRPC แบบซิงโครนัส:
from friendli import Friendli
client = Friendli ( base_url = "0.0.0.0:8000" , use_grpc = True )
stream = client . chat . completions . create (
messages = [
{
"role" : "user" ,
"content" : "Tell me how to make a delicious pancake" ,
}
],
stream = True ,
)
try :
for chunk in stream :
print ( chunk . choices [ 0 ]. delta . content or "" , end = "" , flush = True )
except SomeException :
stream . cancel () # Cancel the stream in case of an error or interruption
สำหรับการสตรีม gRPC แบบอะซิงโครนัส:
import asyncio
from friendli import AsyncFriendli
client = AsyncFriendli ( base_url = "0.0.0.0:8000" , use_grpc = True )
async def main ():
stream = await client . chat . completions . create (
messages = [
{
"role" : "user" ,
"content" : "Tell me how to make a delicious pancake" ,
}
],
stream = True ,
)
try :
async for chunk in stream :
print ( chunk . choices [ 0 ]. delta . content or "" , end = "" , flush = True )
except SomeException :
stream . cancel () # Cancel the stream in case of an error or interruption
asyncio . run ( main ())
คุณยังสามารถเรียกใช้ API การสร้างได้โดยตรงกับ CLI
friendli api chat-completions create
-g " user Tell me how to make a delicious pancake "
-m meta-llama-3.1-8b-instruct
สำหรับข้อมูลเพิ่มเติมเกี่ยวกับคำสั่ง friendli
ให้รัน friendli --help
ในเทอร์มินัลเชลล์ของคุณ ซึ่งจะแสดงรายการตัวเลือกโดยละเอียดและคำแนะนำการใช้งานให้กับคุณ
เคล็ดลับ
ตรวจสอบเอกสารอย่างเป็นทางการของเราเพื่อเรียนรู้เพิ่มเติม!