
文檔:https://declarai.com
源代碼:https://github.com/vendi-ai/declarai
Declarai將您的Python代碼轉換為LLM任務,使您可以輕鬆地將LLM集成到現有的代碼庫中。它以一個簡單的原則運行:只需定義python函數/類即可。通過使用Docstrings和類型提示來註釋此功能,您可以為AI模型提供明確的說明集,而無需其他努力。
聲明函數後,Declarai會智能編譯該函數的Docstrings,並將提示鍵入AI模型的提示,以確保模型確切地了解所需的內容。
執行任務後,Declarai檢索AI的響應並解析它,然後將其轉換為Python函數的返回類型。這消除了您的任何手動解析或後處理。
Declarai將其保留為本地:從其核心中,Declarai是關於擁抱本地Python實踐的核心。您無需學習新的語法或適應其他編碼範式。只需像往常一樣編寫Python函數,然後讓Declarai無縫處理AI集成即可。
AI任務用於任何業務邏輯或轉換。
import declarai
gpt_35 = declarai . openai ( model = "gpt-3.5-turbo" )
@ gpt_35 . task
def rank_by_severity ( message : str ) -> int :
"""
Rank the severity of the provided message by it's urgency.
Urgency is ranked on a scale of 1-5, with 5 being the most urgent.
:param message: The message to rank
:return: The urgency of the message
"""
rank_by_severity ( message = "The server is down!" )
>> > 5
rank_by_severity ( message = "How was your weekend?" ))
>> > 1AI聊天用於與AI模型的迭代對話,AI模型可以記住以前的消息和上下文。
import declarai
gpt_35 = declarai . openai ( model = "gpt-3.5-turbo" )
@ gpt_35 . experimental . chat
class SQLBot :
"""
You are a sql assistant. You help with SQL related questions
"""
sql_bot = SQLBot ()
sql_bot . send ( "When should I use a LEFT JOIN?" )
> " You should use a LEFT JOIN when you want to return all rows from ....pip install declarai export OPENAI_API_KEY= < your openai token >或初始化dectarai對象時通過令牌
import declarai
gpt_35 = declarai . openai ( model = "gpt-3.5-turbo" , openai_token = "<your-openai-key>" )使用@task裝飾器輕鬆製作AI驅動功能。只需添加一些類型的提示和一些文檔,然後觀看Declarai做魔術!
import declarai
gpt_35 = declarai . openai ( model = "gpt-3.5-turbo" )
@ gpt_35 . task
def generate_poem ( title : str ) -> str :
"""
Write a 4 line poem on the provided title
"""
res = generate_poem (
title = "Declarai, the declarative AI framework for LLMs"
)
print ( res )
# Declarai, the AI framework,
# Empowers LLMs with declarative power,
# Efficiently transforming data and knowledge,
# Unlocking insights in every hour.不是那裡最好的詩,但是嘿!您已經寫了第一個聲明性的AI代碼!
Declarai的目的是通過強制使用DOC串和鍵入來促進清潔和可讀的代碼。結果代碼是可讀的,易於維護。
python原語
import declarai
gpt_35 = declarai . openai ( model = "gpt-3.5-turbo" )
@ gpt_35 . task
def rank_by_severity ( message : str ) -> int :
"""
Rank the severity of the provided message by it's urgency.
Urgency is ranked on a scale of 1-5, with 5 being the most urgent.
:param message: The message to rank
:return: The urgency of the message
"""
rank_by_severity ( message = "The server is down!" )
>> > 5
rank_by_severity ( message = "How was your weekend?" ))
>> > 1python列表/dicts等。
from typing import List
import declarai
gpt_35 = declarai . openai ( model = "gpt-3.5-turbo" )
@ gpt_35 . task
def multi_value_extraction ( text : str ) -> List [ str ]:
"""
Extract the phone numbers from the provided text
:param text: content to extract phone number from
:return: The phone numbers that where identified in the input text
"""
multi_value_extraction (
text = "Hey jenny, n you can call me at 124-3435-132. n "
"you can also reach me at +43-938-243-223"
)
>> > [ '124-3435-132' , '+43-938-243-223' ]Python複雜對象
from datetime import datetime
import declarai
gpt_35 = declarai . openai ( model = "gpt-3.5-turbo" )
@ gpt_35 . task
def datetime_parser ( raw_date : str ) -> datetime :
"""
Parse the input into a valid datetime string of the format YYYY-mm-ddThh:mm:ss
:param raw_date: The provided raw date
:return: The parsed datetime output
"""
datetime_parser ( raw_date = "Janury 1st 2020" ))
>> > 2020 - 01 - 01 00 : 00 : 00 from pydantic import BaseModel
from typing import List , Dict
import declarai
class Animal ( BaseModel ):
name : str
family : str
leg_count : int
gpt_35 = declarai . openai ( model = "gpt-3.5-turbo" )
@ gpt_35 . task
def suggest_animals ( location : str ) -> Dict [ int , List [ Animal ]]:
"""
Create a list of numbers from 0 to 5
for each number, suggest a list of animals with that number of legs
:param location: The location where the animals can be found
:return: A list of animal leg count and for each count, the corresponding animals
"""
suggest_animals ( location = "jungle" )
>> > {
0 : [
Animal ( name = 'snake' , family = 'reptile' , leg_count = 0 )
],
2 : [
Animal ( name = 'monkey' , family = 'mammal' , leg_count = 2 ),
Animal ( name = 'parrot' , family = 'bird' , leg_count = 2 )
],
4 : [
Animal ( name = 'tiger' , family = 'mammal' , leg_count = 4 ),
Animal ( name = 'elephant' , family = 'mammal' , leg_count = 4 )
]
} import declarai
gpt_35 = declarai . openai ( model = "gpt-3.5-turbo" )
@ gpt_35 . task
def sentiment_classification ( string : str , examples : List [ str , int ]) -> int :
"""
Classify the sentiment of the provided string, based on the provided examples.
The sentiment is ranked on a scale of 1-5, with 5 being the most positive.
{% for example in examples %}
{{ example[0] }} // {{ example[1] }}
{% endfor %}
{{ string }} //
"""
sentiment_classification ( string = "I love this product but there are some annoying bugs" ,
examples = [[ "I love this product" , 5 ], [ "I hate this product" , 1 ]])
>> > 4 import declarai
gpt_35 = declarai . openai ( model = "gpt-3.5-turbo" )
@ gpt_35 . experimental . chat
class CalculatorBot :
"""
You a calculator bot,
given a request, you will return the result of the calculation
"""
def send ( self , message : str ) -> int : ...
calc_bot = CalculatorBot ()
calc_bot . send ( message = "1 + 1" )
>> > 2有關詳盡的介紹,功能和最佳實踐,請探討我們的官方文檔和初學者指南。
加入我們的使命,使聲明性的AI在一起更好!查看我們的貢獻指南以開始。