LLM FOO เป็นโครงการที่ทันสมัยผสมผสานศิลปะของกังฟูกับวิทยาศาสตร์ของรุ่นภาษาขนาดใหญ่ ... หรือที่จริงแล้วนี่คือการสร้างสคีมา JSON Tool OpenAI โดยอัตโนมัติแยกวิเคราะห์การโทรและสร้างผลลัพธ์ให้กับโมเดลแชท และจากนั้นก็มียูทิลิตี้ที่สอง is_statement_true ที่ใช้เคล็ดลับ Genius logit_bias ที่ใช้โทเค็นเอาต์พุตเพียงตัวเดียว
แต่เดี๋ยวก่อนฉันหวังว่านี่จะกลายเป็นชุดของฟังก์ชั่น LLM ตัวช่วย LLM ที่มีประโยชน์ขนาดเล็กที่จะทำให้สิ่งปลูกสร้างง่ายขึ้นเพราะ APIs ขอบเลือดออกในปัจจุบันเป็นระเบียบเล็กน้อยและฉันคิดว่าเราทำได้ดีกว่า

pip install llmfoo คุณต้องมี OpenAI_API_KEY ใน Env และความสามารถในการโทรหารุ่น gpt-4-1106-preview
is_statement_true ควรเข้าใจง่าย สร้างคำแถลงภาษาธรรมชาติและตรวจสอบกับเกณฑ์หรือความจริงทั่วไป คุณได้รับบูลีนกลับ
ฟังก์ชั่น pdf2md ที่เพิ่งเปิดตัวใหม่ช่วยให้สามารถแปลงเอกสาร PDF เป็นรูปแบบ markdown ทำให้ง่ายต่อการประมวลผลและรวมเข้ากับระบบที่ใช้ LLM คุณลักษณะนี้มีประโยชน์อย่างยิ่งสำหรับการแยกข้อความและตารางจาก PDFs และเปลี่ยนเป็นรูปแบบที่จัดการได้มากขึ้น
pdftocairo ในระบบของคุณเพื่อแปลงหน้า PDF เป็นรูปภาพ from llmfoo . pdf2md import process_pdf
from pathlib import Path
pdf_path = Path ( "path/to/your/document.pdf" )
output_dir = Path ( "path/to/output/directory" )
# Process the PDF and generate Markdown
markdown_file = process_pdf ( pdf_path , output_dir )ฟังก์ชั่นนี้จะประมวลผลแต่ละหน้าของ PDF พยายามที่จะแยกข้อความตัวเลขและตารางและแปลงเป็นไฟล์ markdown ในไดเรกทอรีเอาต์พุตที่ระบุ
@tool คำอธิบายประกอบopenai_schema เพื่อส่งคืนสคีมา (คุณสามารถแก้ไขได้จาก JSON หากคุณไม่พอใจกับสิ่งที่เครื่องจักรทำ)openai_tool_call เพื่อโทรออกเครื่องมือและส่งคืนผลลัพธ์ในรูปแบบข้อความแชท APIopenai_tool_output เพื่อโทรออกเครื่องมือและส่งคืนผลลัพธ์ในรูปแบบเอาท์พุทเครื่องมือ Assistant API from time import sleep
from openai import OpenAI
from llmfoo . functions import tool
from llmfoo import is_statement_true
def test_is_statement_true_with_default_criteria ():
assert is_statement_true ( "Earth is a planet." )
assert not is_statement_true ( "1 + 2 = 5" )
def test_is_statement_true_with_own_criteria ():
assert not is_statement_true ( "Temperature outside is -2 degrees celsius" ,
criteria = "Temperature above 0 degrees celsius" )
assert is_statement_true ( "1984 was written by George Orwell" ,
criteria = "George Orwell is the author of 1984" )
def test_is_statement_true_criteria_can_change_truth_value ():
assert is_statement_true ( "Earth is 3rd planet from the Sun" )
assert not is_statement_true ( "Earth is 3rd planet from the Sun" ,
criteria = "Earth is stated to be 5th planet from the Sun" )
@ tool
def adder ( x : int , y : int ) -> int :
return x + y
@ tool
def multiplier ( x : int , y : int ) -> int :
return x * y
client = OpenAI ()
def test_chat_completion_with_adder ():
number1 = 3267182746
number2 = 798472847
messages = [
{
"role" : "user" ,
"content" : f"What is { number1 } + { number2 } ?"
}
]
response = client . chat . completions . create (
model = "gpt-4-1106-preview" ,
messages = messages ,
tools = [ adder . openai_schema ]
)
messages . append ( response . choices [ 0 ]. message )
messages . append ( adder . openai_tool_call ( response . choices [ 0 ]. message . tool_calls [ 0 ]))
response2 = client . chat . completions . create (
model = "gpt-4-1106-preview" ,
messages = messages ,
tools = [ adder . openai_schema ]
)
assert str ( adder ( number1 , number2 )) in response2 . choices [ 0 ]. message . content . replace ( "," , "" )
def test_assistant_with_multiplier ():
number1 = 1238763428176
number2 = 172388743612
assistant = client . beta . assistants . create (
name = "The Calc Machina" ,
instructions = "You are a calculator with a funny pirate accent." ,
tools = [ multiplier . openai_schema ],
model = "gpt-4-1106-preview"
)
thread = client . beta . threads . create ( messages = [
{
"role" : "user" ,
"content" : f"What is { number1 } * { number2 } ?"
}
])
run = client . beta . threads . runs . create (
thread_id = thread . id ,
assistant_id = assistant . id
)
while True :
run_state = client . beta . threads . runs . retrieve (
run_id = run . id ,
thread_id = thread . id ,
)
if run_state . status not in [ 'in_progress' , 'requires_action' ]:
break
if run_state . status == 'requires_action' :
tool_call = run_state . required_action . submit_tool_outputs . tool_calls [ 0 ]
run = client . beta . threads . runs . submit_tool_outputs (
thread_id = thread . id ,
run_id = run . id ,
tool_outputs = [
multiplier . openai_tool_output ( tool_call )
]
)
sleep ( 1 )
sleep ( 0.1 )
messages = client . beta . threads . messages . list ( thread_id = thread . id )
assert str ( multiplier ( number1 , number2 )) in messages . data [ 0 ]. content [ 0 ]. text . value . replace ( "," , "" )สนใจที่จะบริจาค? ชอบที่จะได้รับความช่วยเหลือเพื่อให้โครงการนี้ดีขึ้น! APIs ภายใต้มีการเปลี่ยนแปลงและระบบยังคงเป็นรุ่นแรกมาก
โครงการนี้ได้รับใบอนุญาตภายใต้ใบอนุญาต MIT