استخدم API Openai مع Ruby! ؟ ❤
دفق النص باستخدام GPT-4O ، ونسخ وترجمة الصوت مع الهمس ، أو قم بإنشاء صور باستخدام Dall · E ...
استأجرني لبناء القضبان+AI App | القضبان AI | ؟ Ruby AI Builders Discord | ؟ x | ؟ جوهرة الأنثروبور | ؟ جوهرة ميدوني
أضف هذا السطر إلى Gemfile في التطبيق الخاص بك:
gem "ruby-openai"
ثم تنفذ:
$ bundle install
أو التثبيت مع:
$ gem install ruby-openai
وتتطلب:
require "openai"
لاختبار سريع ، يمكنك تمرير الرمز المميز مباشرة إلى عميل جديد:
client = OpenAI :: Client . new (
access_token : "access_token_goes_here" ,
log_errors : true # Highly recommended in development, so you can see what errors OpenAI is returning. Not recommended in production because it could leak private data to your logs.
)
لإعداد أكثر قوة ، يمكنك تكوين GEM باستخدام مفاتيح API الخاصة بك ، على سبيل المثال في ملف openai.rb
. لا تقم مطلقًا بأسرار الشفرات في قاعدة الشفرة الخاصة بك - بدلاً من ذلك ، استخدم شيئًا مثل Dotenv لتمرير المفاتيح بأمان إلى بيئاتك.
OpenAI . configure do | config |
config . access_token = ENV . fetch ( "OPENAI_ACCESS_TOKEN" )
config . organization_id = ENV . fetch ( "OPENAI_ORGANIZATION_ID" ) # Optional
config . log_errors = true # Highly recommended in development, so you can see what errors OpenAI is returning. Not recommended in production because it could leak private data to your logs.
end
ثم يمكنك إنشاء عميل مثل هذا:
client = OpenAI :: Client . new
لا يزال بإمكانك تجاوز الإعدادات الافتراضية للتكوين عند إنشاء عملاء جدد ؛ أي خيارات غير متضمنة سوف تعود إلى أي مجموعة تكوين عالمية مع Openai.configure. على سبيل المثال ، ستقوم المؤسسة _id ، request_timeout ، إلخ.
client = OpenAI :: Client . new ( access_token : "access_token_goes_here" )
request_timeout
عند تهيئة العميل. client = OpenAI :: Client . new (
access_token : "access_token_goes_here" ,
uri_base : "https://oai.hconeai.com/" ,
request_timeout : 240 ,
extra_headers : {
"X-Proxy-TTL" => "43200" , # For https://github.com/6/openai-caching-proxy-worker#specifying-a-cache-ttl
"X-Proxy-Refresh" : "true" , # For https://github.com/6/openai-caching-proxy-worker#refreshing-the-cache
"Helicone-Auth" : "Bearer HELICONE_API_KEY" , # For https://docs.helicone.ai/getting-started/integration-method/openai-proxy
"helicone-stream-force-format" => "true" , # Use this with Helicone otherwise streaming drops chunks # https://github.com/alexrudall/ruby-openai/issues/251
}
)
أو عند تكوين الجوهرة:
OpenAI . configure do | config |
config . access_token = ENV . fetch ( "OPENAI_ACCESS_TOKEN" )
config . log_errors = true # Optional
config . organization_id = ENV . fetch ( "OPENAI_ORGANIZATION_ID" ) # Optional
config . uri_base = "https://oai.hconeai.com/" # Optional
config . request_timeout = 240 # Optional
config . extra_headers = {
"X-Proxy-TTL" => "43200" , # For https://github.com/6/openai-caching-proxy-worker#specifying-a-cache-ttl
"X-Proxy-Refresh" : "true" , # For https://github.com/6/openai-caching-proxy-worker#refreshing-the-cache
"Helicone-Auth" : "Bearer HELICONE_API_KEY" # For https://docs.helicone.ai/getting-started/integration-method/openai-proxy
} # Optional
end
يمكنك تمرير رؤوس لكل كائن عميل ديناميكيًا ، والذي سيتم دمجه مع أي رؤوس تم تعيينها على مستوى العالم باستخدام Openai.configure:
client = OpenAI :: Client . new ( access_token : "access_token_goes_here" )
client . add_headers ( "X-Proxy-TTL" => "43200" )
بشكل افتراضي ، لا يقوم ruby-openai
بتسجيل أي Faraday::Error
تم مواجهته أثناء تنفيذ طلب شبكة لتجنب تسرب البيانات (على سبيل المثال 400s و 500s وأخطاء SSL والمزيد - انظر هنا للحصول على قائمة كاملة من الفئات الفرعية من Faraday::Error
and and ما الذي يمكن أن يسبب لهم).
إذا كنت ترغب في تمكين هذه الوظيفة ، فيمكنك تعيين log_errors
على true
عند تكوين العميل:
client = OpenAI :: Client . new ( log_errors : true )
يمكنك تمرير البرامج الوسيطة فاراداي إلى العميل في كتلة ، على سبيل المثال. لتمكين التسجيل المطوّل مع مسجل روبي:
client = OpenAI :: Client . new do | f |
f . response :logger , Logger . new ( $stdout ) , bodies : true
end
لاستخدام واجهة برمجة تطبيقات Azure Openai Service ، يمكنك تكوين GEM مثل هذا:
OpenAI . configure do | config |
config . access_token = ENV . fetch ( "AZURE_OPENAI_API_KEY" )
config . uri_base = ENV . fetch ( "AZURE_OPENAI_URI" )
config . api_type = :azure
config . api_version = "2023-03-15-preview"
end
حيث AZURE_OPENAI_URI
هو على سبيل المثال https://custom-domain.openai.azure.com/openai/deployments/gpt-35-turbo
يتيح لك Ollama تشغيل LLMs مفتوحة المصدر ، مثل Llama 3 ، محليًا. إنه يوفر توافق الدردشة مع API Openai.
يمكنك تنزيل Ollama هنا. على MacOS ، يمكنك تثبيت وتشغيل Ollama مثل هذا:
brew install ollama
ollama serve
ollama pull llama3:latest # In new terminal tab.
قم بإنشاء عميل باستخدام خادم Ollama الخاص بك والنموذج المسحوب ، وقم ببث محادثة مجانًا:
client = OpenAI :: Client . new (
uri_base : "http://localhost:11434"
)
client . chat (
parameters : {
model : "llama3" , # Required.
messages : [ { role : "user" , content : "Hello!" } ] , # Required.
temperature : 0.7 ,
stream : proc do | chunk , _bytesize |
print chunk . dig ( "choices" , 0 , "delta" , "content" )
end
}
)
# => Hi! It's nice to meet you. Is there something I can help you with, or would you like to chat?
دردشة GROQ API متوافقة على نطاق واسع مع API Openai ، مع بعض الاختلافات البسيطة. احصل على رمز الوصول من هنا ، ثم:
client = OpenAI :: Client . new (
access_token : "groq_access_token_goes_here" ,
uri_base : "https://api.groq.com/openai"
)
client . chat (
parameters : {
model : "llama3-8b-8192" , # Required.
messages : [ { role : "user" , content : "Hello!" } ] , # Required.
temperature : 0.7 ,
stream : proc do | chunk , _bytesize |
print chunk . dig ( "choices" , 0 , "delta" , "content" )
end
}
)
يقوم Openai بتوزيع النص المطري على الرموز ، وهي كلمات أو أجزاء من الكلمات. (هذه الرموز لا علاقة لها بـ API Access_Token.) يمكن أن يساعدك حساب الرموز في تقدير تكاليفك. يمكن أن يساعدك أيضًا في التأكد من أن حجم النص المطالب الخاص بك ضمن حدود Max-Token في نافذة سياق النموذج الخاص بك ، واختيار معلمة إتمام max_tokens
المناسبة حتى تتناسب ردك أيضًا.
لتقدير الرمز المميز للنص الخاص بك:
OpenAI . rough_token_count ( "Your text" )
إذا كنت بحاجة إلى عدد أكثر دقة ، فحاول tiktoken_ruby.
هناك نماذج مختلفة يمكن استخدامها لإنشاء النص. للحصول على قائمة كاملة واسترداد المعلومات حول نموذج واحد:
client . models . list
client . models . retrieve ( id : "gpt-4o" )
GPT هو نموذج يمكن استخدامه لإنشاء نص في نمط محادثة. يمكنك استخدامه لإنشاء استجابة لتسلسل الرسائل:
response = client . chat (
parameters : {
model : "gpt-4o" , # Required.
messages : [ { role : "user" , content : "Hello!" } ] , # Required.
temperature : 0.7 ,
}
)
puts response . dig ( "choices" , 0 , "message" , "content" )
# => "Hello! How may I assist you today?"
دليل سريع لتدفق الدردشة مع Rails 7 و Hotwire
يمكنك البث من واجهة برمجة التطبيقات في الوقت الفعلي ، والتي يمكن أن تكون أسرع بكثير وتستخدم لإنشاء تجربة مستخدم أكثر جاذبية. تمرير PROC (أو أي كائن مع طريقة #call
) إلى معلمة stream
لتلقي دفق أجزاء الانتهاء عند إنشاؤها. في كل مرة يتم استلام قطعة واحدة أو أكثر ، سيتم استدعاء PROC مرة واحدة مع كل قطعة ، تم تحليلها كتجزئة. إذا قام Openai بإرجاع خطأ ، ruby-openai
سترفع خطأ فاراداي.
client . chat (
parameters : {
model : "gpt-4o" , # Required.
messages : [ { role : "user" , content : "Describe a character called Anna!" } ] , # Required.
temperature : 0.7 ,
stream : proc do | chunk , _bytesize |
print chunk . dig ( "choices" , 0 , "delta" , "content" )
end
}
)
# => "Anna is a young woman in her mid-twenties, with wavy chestnut hair that falls to her shoulders..."
ملاحظة: من أجل الحصول على معلومات الاستخدام ، يمكنك توفير معلمة stream_options
وسيوفر Openai قطعة نهائية مع الاستخدام. هنا مثال:
stream_proc = proc { | chunk , _bytesize | puts "--------------" ; puts chunk . inspect ; }
client . chat (
parameters : {
model : "gpt-4o" ,
stream : stream_proc ,
stream_options : { include_usage : true } ,
messages : [ { role : "user" , content : "Hello!" } ] ,
}
)
# => --------------
# => {"id"=>"chatcmpl-7bbq05PiZqlHxjV1j7OHnKKDURKaf", "object"=>"chat.completion.chunk", "created"=>1718750612, "model"=>"gpt-4o-2024-05-13", "system_fingerprint"=>"fp_9cb5d38cf7", "choices"=>[{"index"=>0, "delta"=>{"role"=>"assistant", "content"=>""}, "logprobs"=>nil, "finish_reason"=>nil}], "usage"=>nil}
# => --------------
# => {"id"=>"chatcmpl-7bbq05PiZqlHxjV1j7OHnKKDURKaf", "object"=>"chat.completion.chunk", "created"=>1718750612, "model"=>"gpt-4o-2024-05-13", "system_fingerprint"=>"fp_9cb5d38cf7", "choices"=>[{"index"=>0, "delta"=>{"content"=>"Hello"}, "logprobs"=>nil, "finish_reason"=>nil}], "usage"=>nil}
# => --------------
# => ... more content chunks
# => --------------
# => {"id"=>"chatcmpl-7bbq05PiZqlHxjV1j7OHnKKDURKaf", "object"=>"chat.completion.chunk", "created"=>1718750612, "model"=>"gpt-4o-2024-05-13", "system_fingerprint"=>"fp_9cb5d38cf7", "choices"=>[{"index"=>0, "delta"=>{}, "logprobs"=>nil, "finish_reason"=>"stop"}], "usage"=>nil}
# => --------------
# => {"id"=>"chatcmpl-7bbq05PiZqlHxjV1j7OHnKKDURKaf", "object"=>"chat.completion.chunk", "created"=>1718750612, "model"=>"gpt-4o-2024-05-13", "system_fingerprint"=>"fp_9cb5d38cf7", "choices"=>[], "usage"=>{"prompt_tokens"=>9, "completion_tokens"=>9, "total_tokens"=>18}}
يمكنك استخدام نموذج GPT-4 Vision لإنشاء وصف للصورة:
messages = [
{ "type" : "text" , "text" : "What’s in this image?" } ,
{ "type" : "image_url" ,
"image_url" : {
"url" : "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg" ,
} ,
}
]
response = client . chat (
parameters : {
model : "gpt-4-vision-preview" , # Required.
messages : [ { role : "user" , content : messages } ] , # Required.
}
)
puts response . dig ( "choices" , 0 , "message" , "content" )
# => "The image depicts a serene natural landscape featuring a long wooden boardwalk extending straight ahead"
يمكنك تعيين Response_format لطلب الردود في JSON:
response = client . chat (
parameters : {
model : "gpt-4o" ,
response_format : { type : "json_object" } ,
messages : [ { role : "user" , content : "Hello! Give me some JSON please." } ] ,
temperature : 0.7 ,
} )
puts response . dig ( "choices" , 0 , "message" , "content" )
# =>
# {
# "name": "John",
# "age": 30,
# "city": "New York",
# "hobbies": ["reading", "traveling", "hiking"],
# "isStudent": false
# }
يمكنك بثه أيضًا!
response = client . chat (
parameters : {
model : "gpt-4o" ,
messages : [ { role : "user" , content : "Can I have some JSON please?" } ] ,
response_format : { type : "json_object" } ,
stream : proc do | chunk , _bytesize |
print chunk . dig ( "choices" , 0 , "delta" , "content" )
end
}
)
# =>
# {
# "message": "Sure, please let me know what specific JSON data you are looking for.",
# "JSON_data": {
# "example_1": {
# "key_1": "value_1",
# "key_2": "value_2",
# "key_3": "value_3"
# },
# "example_2": {
# "key_4": "value_4",
# "key_5": "value_5",
# "key_6": "value_6"
# }
# }
# }
يمكنك وصف الوظائف والتمرير لها وسيختار النموذج بذكاء إخراج كائن JSON يحتوي على وسيط للاتصال بها - على سبيل المثال ، لاستخدام طريقتك get_current_weather
للحصول على الطقس في موقع معين. لاحظ أن tool_choice اختياري ، ولكن إذا استبعدت ذلك ، فسيختار النموذج ما إذا كان سيتم استخدام الوظيفة أم لا (انظر هنا).
def get_current_weather ( location : , unit : "fahrenheit" )
# Here you could use a weather api to fetch the weather.
"The weather in #{ location } is nice ? #{ unit } "
end
messages = [
{
"role" : "user" ,
"content" : "What is the weather like in San Francisco?" ,
} ,
]
response =
client . chat (
parameters : {
model : "gpt-4o" ,
messages : messages , # Defined above because we'll use it again
tools : [
{
type : "function" ,
function : {
name : "get_current_weather" ,
description : "Get the current weather in a given location" ,
parameters : { # Format: https://json-schema.org/understanding-json-schema
type : :object ,
properties : {
location : {
type : :string ,
description : "The city and state, e.g. San Francisco, CA" ,
} ,
unit : {
type : "string" ,
enum : %w[ celsius fahrenheit ] ,
} ,
} ,
required : [ "location" ] ,
} ,
} ,
}
] ,
# Optional, defaults to "auto"
# Can also put "none" or specific functions, see docs
tool_choice : "required"
} ,
)
message = response . dig ( "choices" , 0 , "message" )
if message [ "role" ] == "assistant" && message [ "tool_calls" ]
message [ "tool_calls" ] . each do | tool_call |
tool_call_id = tool_call . dig ( "id" )
function_name = tool_call . dig ( "function" , "name" )
function_args = JSON . parse (
tool_call . dig ( "function" , "arguments" ) ,
{ symbolize_names : true } ,
)
function_response =
case function_name
when "get_current_weather"
get_current_weather ( ** function_args ) # => "The weather is nice ?"
else
# decide how to handle
end
# For a subsequent message with the role "tool", OpenAI requires the preceding message to have a tool_calls argument.
messages << message
messages << {
tool_call_id : tool_call_id ,
role : "tool" ,
name : function_name ,
content : function_response
} # Extend the conversation with the results of the functions
end
second_response = client . chat (
parameters : {
model : "gpt-4o" ,
messages : messages
}
)
puts second_response . dig ( "choices" , 0 , "message" , "content" )
# At this point, the model has decided to call functions, you've called the functions
# and provided the response back, and the model has considered this and responded.
end
# => "It looks like the weather is nice and sunny in San Francisco! If you're planning to go out, it should be a pleasant day."
اضغط على واجهة برمجة تطبيقات Openai لإكمالها باستخدام نماذج GPT-3 الأخرى:
response = client . completions (
parameters : {
model : "gpt-4o" ,
prompt : "Once upon a time" ,
max_tokens : 5
}
)
puts response [ "choices" ] . map { | c | c [ "text" ] }
# => [", there lived a great"]
يمكنك استخدام نقطة نهاية التضمين للحصول على متجه من الأرقام التي تمثل إدخال. يمكنك بعد ذلك مقارنة هذه المتجهات للمدخلات المختلفة للتحقق بكفاءة من مدى تشابه المدخلات.
response = client . embeddings (
parameters : {
model : "text-embedding-ada-002" ,
input : "The food was delicious and the waiter..."
}
)
puts response . dig ( "data" , 0 , "embedding" )
# => Vector representation of your embedding
تتيح لك نقطة نهاية الدفعات إنشاء وإدارة دفعات كبيرة من طلبات API للتشغيل بشكل غير متزامن. حاليًا ، تكون نقاط النهاية المدعومة للدفعات هي /v1/chat/completions
(API API) و /v1/embeddings
(API inbeddings).
لاستخدام نقطة نهاية الدفعات ، تحتاج أولاً إلى تحميل ملف JSONL الذي يحتوي على طلبات الدُفعات باستخدام نقطة نهاية الملفات. يجب تحميل الملف باستخدام الغرض المحدد على batch
. يمثل كل سطر في ملف JSONL طلبًا واحدًا ويجب أن يكون له التنسيق التالي:
{
"custom_id" : " request-1 " ,
"method" : " POST " ,
"url" : " /v1/chat/completions " ,
"body" : {
"model" : " gpt-4o " ,
"messages" : [
{ "role" : " system " , "content" : " You are a helpful assistant. " },
{ "role" : " user " , "content" : " What is 2+2? " }
]
}
}
بمجرد تحميل ملف JSONL ، يمكنك إنشاء مجموعة جديدة من خلال توفير معرف الملف ونقطة النهاية ونافذة الإكمال:
response = client . batches . create (
parameters : {
input_file_id : "file-abc123" ,
endpoint : "/v1/chat/completions" ,
completion_window : "24h"
}
)
batch_id = response [ "id" ]
يمكنك استرداد المعلومات حول مجموعة معينة باستخدام معرفها:
batch = client . batches . retrieve ( id : batch_id )
لإلغاء دفعة قيد التقدم:
client . batches . cancel ( id : batch_id )
يمكنك أيضًا سرد جميع الدُفعات:
client . batches . list
بمجرد وجود الدُفعة ["confere_at"] ، يمكنك جلب ملفات الإخراج أو الخطأ:
batch = client . batches . retrieve ( id : batch_id )
output_file_id = batch [ "output_file_id" ]
output_response = client . files . content ( id : output_file_id )
error_file_id = batch [ "error_file_id" ]
error_response = client . files . content ( id : error_file_id )
هذه الملفات بتنسيق JSONL ، مع كل سطر يمثل الإخراج أو الخطأ لطلب واحد. يمكن أن تكون الخطوط بأي ترتيب:
{
"id" : " response-1 " ,
"custom_id" : " request-1 " ,
"response" : {
"id" : " chatcmpl-abc123 " ,
"object" : " chat.completion " ,
"created" : 1677858242 ,
"model" : " gpt-4o " ,
"choices" : [
{
"index" : 0 ,
"message" : {
"role" : " assistant " ,
"content" : " 2+2 equals 4. "
}
}
]
}
}
إذا فشل الطلب بخطأ غير HTTP ، فسيحتوي كائن الخطأ على مزيد من المعلومات حول سبب الفشل.
ضع بياناتك في ملف .jsonl
مثل هذا:
{ "prompt" : " Overjoyed with my new phone! -> " , "completion" : " positive " }
{ "prompt" : " @lakers disappoint for a third straight night -> " , "completion" : " negative " }
ومرر المسار (أو كائن stringio) إلى client.files.upload
لتحميله إلى Openai ، ثم التفاعل معه:
client . files . upload ( parameters : { file : "path/to/sentiment.jsonl" , purpose : "fine-tune" } )
client . files . list
client . files . retrieve ( id : "file-123" )
client . files . content ( id : "file-123" )
client . files . delete ( id : "file-123" )
يمكنك إرسال مسار الملف:
client . files . upload ( parameters : { file : "path/to/file.pdf" , purpose : "assistants" } )
أو كائن ملف
my_file = File . open ( "path/to/file.pdf" , "rb" )
client . files . upload ( parameters : { file : my_file , purpose : "assistants" } )
انظر أنواع الملفات المدعومة على وثائق API.
قم بتحميل بيانات الضبط الدقيقة في ملف .jsonl
على النحو الوارد أعلاه واحصل على معرفه:
response = client . files . upload ( parameters : { file : "path/to/sarcasm.jsonl" , purpose : "fine-tune" } )
file_id = JSON . parse ( response . body ) [ "id" ]
يمكنك بعد ذلك استخدام معرف الملف هذا لإنشاء وظيفة ضبط دقيقة:
response = client . finetunes . create (
parameters : {
training_file : file_id ,
model : "gpt-4o"
} )
fine_tune_id = response [ "id" ]
هذا سوف يمنحك معرف النصوص. إذا ارتكبت خطأً ، فيمكنك إلغاء نموذج النقطة الدقيقة قبل معالجته:
client . finetunes . cancel ( id : fine_tune_id )
قد تحتاج إلى الانتظار لفترة قصيرة لإكمالها. بمجرد معالجتها ، يمكنك استخدام قائمة أو استرداد للحصول على اسم النموذج الذي تم ضبطه:
client . finetunes . list
response = client . finetunes . retrieve ( id : fine_tune_id )
fine_tuned_model = response [ "fine_tuned_model" ]
يمكن بعد ذلك استخدام اسم الطراز الذي تم ضبطه في إكمال الدردشة:
response = client . chat (
parameters : {
model : fine_tuned_model ,
messages : [ { role : "user" , content : "I love Mondays!" } ]
}
)
response . dig ( "choices" , 0 , "message" , "content" )
يمكنك أيضًا التقاط الأحداث لوظيفة:
client . finetunes . list_events ( id : fine_tune_id )
تعطي كائنات متجر Vector أداة البحث عن الملفات القدرة على البحث في ملفاتك.
يمكنك إنشاء متجر متجه جديد:
response = client . vector_stores . create (
parameters : {
name : "my vector store" ,
file_ids : [ "file-abc123" , "file-def456" ]
}
)
vector_store_id = response [ "id" ]
بالنظر إلى vector_store_id
، يمكنك retrieve
قيم الحقل الحالية:
client . vector_stores . retrieve ( id : vector_store_id )
يمكنك الحصول على list
بجميع متاجر المتجهات المتوفرة حاليًا بموجب المؤسسة:
client . vector_stores . list
يمكنك تعديل متجر متجه موجود ، باستثناء file_ids
:
response = client . vector_stores . modify (
id : vector_store_id ,
parameters : {
name : "Modified Test Vector Store" ,
}
)
يمكنك حذف متاجر المتجهات:
client . vector_stores . delete ( id : vector_store_id )
تمثل ملفات متجر المتجهات الملفات داخل متجر متجه.
يمكنك إنشاء ملف متجر متجه جديد عن طريق إرفاق ملف إلى متجر متجه.
response = client . vector_store_files . create (
vector_store_id : "vector-store-abc123" ,
parameters : {
file_id : "file-abc123"
}
)
vector_store_file_id = response [ "id" ]
بالنظر إلى vector_store_file_id
، يمكنك retrieve
قيم الحقل الحالي:
client . vector_store_files . retrieve (
vector_store_id : "vector-store-abc123" ,
id : vector_store_file_id
)
يمكنك الحصول على list
بجميع ملفات متجر المتجهات المتوفرة حاليًا ضمن متجر المتجهات:
client . vector_store_files . list ( vector_store_id : "vector-store-abc123" )
يمكنك حذف ملف متجر المتجهات:
client . vector_store_files . delete (
vector_store_id : "vector-store-abc123" ,
id : vector_store_file_id
)
ملاحظة: سيؤدي ذلك إلى إزالة الملف من متجر Vector ولكن لن يتم حذف الملف نفسه. لحذف الملف ، استخدم نقطة نهاية ملف حذف.
تمثل مجموعات ملفات متجر Vector عمليات لإضافة ملفات متعددة إلى متجر متجه.
يمكنك إنشاء مجموعة ملفات متجه جديدة من خلال إرفاق ملفات متعددة بمتجر متجه.
response = client . vector_store_file_batches . create (
vector_store_id : "vector-store-abc123" ,
parameters : {
file_ids : [ "file-abc123" , "file-def456" ]
}
)
file_batch_id = response [ "id" ]
بالنظر إلى file_batch_id
، يمكنك retrieve
قيم الحقل الحالي:
client . vector_store_file_batches . retrieve (
vector_store_id : "vector-store-abc123" ,
id : file_batch_id
)
يمكنك الحصول على list
بجميع ملفات متجر Vector في مجموعة متوفرة حاليًا تحت متجر Vector:
client . vector_store_file_batches . list (
vector_store_id : "vector-store-abc123" ,
id : file_batch_id
)
يمكنك إلغاء دفعة ملفات متجه متجر (هذه المحاولات لإلغاء معالجة الملفات في هذه الدفعة في أقرب وقت ممكن):
client . vector_store_file_batches . cancel (
vector_store_id : "vector-store-abc123" ,
id : file_batch_id
)
المساعدون هم فاعلون مفعولون يمكن أن يكون لديهم العديد من المحادثات واستخدام أدوات لأداء المهام (انظر نظرة عامة على مساعد).
لإنشاء مساعد جديد:
response = client . assistants . create (
parameters : {
model : "gpt-4o" ,
name : "OpenAI-Ruby test assistant" ,
description : nil ,
instructions : "You are a Ruby dev bot. When asked a question, write and run Ruby code to answer the question" ,
tools : [
{ type : "code_interpreter" } ,
{ type : "file_search" }
] ,
tool_resources : {
code_interpreter : {
file_ids : [ ] # See Files section above for how to upload files
} ,
file_search : {
vector_store_ids : [ ] # See Vector Stores section above for how to add vector stores
}
} ,
"metadata" : { my_internal_version_id : "1.0.0" }
}
)
assistant_id = response [ "id" ]
بالنظر إلى assistant_id
يمكنك retrieve
قيم الحقل الحالية:
client . assistants . retrieve ( id : assistant_id )
يمكنك الحصول على list
بجميع المساعدين المتاحة حاليًا بموجب المنظمة:
client . assistants . list
يمكنك تعديل مساعد موجود باستخدام معرف مساعد (انظر وثائق API):
response = client . assistants . modify (
id : assistant_id ,
parameters : {
name : "Modified Test Assistant for OpenAI-Ruby" ,
metadata : { my_internal_version_id : '1.0.1' }
}
)
يمكنك حذف المساعدين:
client . assistants . delete ( id : assistant_id )
بمجرد إنشاء مساعد كما هو موضح أعلاه ، تحتاج إلى إعداد Thread
من Messages
للمساعد للعمل عليه (انظر مقدمة عن المساعدين). على سبيل المثال ، كإعداد أولي يمكنك القيام به:
# Create thread
response = client . threads . create # Note: Once you create a thread, there is no way to list it
# or recover it currently (as of 2023-12-10). So hold onto the `id`
thread_id = response [ "id" ]
# Add initial message from user (see https://platform.openai.com/docs/api-reference/messages/createMessage)
message_id = client . messages . create (
thread_id : thread_id ,
parameters : {
role : "user" , # Required for manually created messages
content : "Can you help me write an API library to interact with the OpenAI API please?"
}
) [ "id" ]
# Retrieve individual message
message = client . messages . retrieve ( thread_id : thread_id , id : message_id )
# Review all messages on the thread
messages = client . messages . list ( thread_id : thread_id )
للتنظيف بعد أن لم تعد هناك حاجة إلى خيط:
# To delete the thread (and all associated messages):
client . threads . delete ( id : thread_id )
client . messages . retrieve ( thread_id : thread_id , id : message_id ) # -> Fails after thread is deleted
لتقديم موضوع ليتم تقييمه باستخدام نموذج مساعد ، قم بإنشاء Run
كما يلي:
# Create run (will use instruction/model/tools from Assistant's definition)
response = client . runs . create (
thread_id : thread_id ,
parameters : {
assistant_id : assistant_id ,
max_prompt_tokens : 256 ,
max_completion_tokens : 16
}
)
run_id = response [ 'id' ]
يمكنك دفق أجزاء الرسائل عند وصولها:
client . runs . create (
thread_id : thread_id ,
parameters : {
assistant_id : assistant_id ,
max_prompt_tokens : 256 ,
max_completion_tokens : 16 ,
stream : proc do | chunk , _bytesize |
if chunk [ "object" ] == "thread.message.delta"
print chunk . dig ( "delta" , "content" , 0 , "text" , "value" )
end
end
}
)
للحصول على حالة التشغيل:
response = client . runs . retrieve ( id : run_id , thread_id : thread_id )
status = response [ 'status' ]
يمكن أن تتضمن استجابة status
السلاسل التالية في queued
، in_progress
، requires_action
، cancelling
، cancelled
، failed
، completed
، أو expired
صلاحيتها على النحو التالي:
while true do
response = client . runs . retrieve ( id : run_id , thread_id : thread_id )
status = response [ 'status' ]
case status
when 'queued' , 'in_progress' , 'cancelling'
puts 'Sleeping'
sleep 1 # Wait one second and poll again
when 'completed'
break # Exit loop and report result to user
when 'requires_action'
# Handle tool calls (see below)
when 'cancelled' , 'failed' , 'expired'
puts response [ 'last_error' ] . inspect
break # or `exit`
else
puts "Unknown status response: #{ status } "
end
end
إذا كانت استجابة status
تشير إلى أن run
قد completed
، فسيحتوي thread
المرتبط على رسالة أو أكثر من messages
الجديدة المرفقة:
# Either retrieve all messages in bulk again, or...
messages = client . messages . list ( thread_id : thread_id , parameters : { order : 'asc' } )
# Alternatively retrieve the `run steps` for the run which link to the messages:
run_steps = client . run_steps . list ( thread_id : thread_id , run_id : run_id , parameters : { order : 'asc' } )
new_message_ids = run_steps [ 'data' ] . filter_map do | step |
if step [ 'type' ] == 'message_creation'
step . dig ( 'step_details' , "message_creation" , "message_id" )
end # Ignore tool calls, because they don't create new messages.
end
# Retrieve the individual messages
new_messages = new_message_ids . map do | msg_id |
client . messages . retrieve ( id : msg_id , thread_id : thread_id )
end
# Find the actual response text in the content array of the messages
new_messages . each do | msg |
msg [ 'content' ] . each do | content_item |
case content_item [ 'type' ]
when 'text'
puts content_item . dig ( 'text' , 'value' )
# Also handle annotations
when 'image_file'
# Use File endpoint to retrieve file contents via id
id = content_item . dig ( 'image_file' , 'file_id' )
end
end
end
يمكنك أيضًا تحديث البيانات الوصفية على الرسائل ، بما في ذلك الرسائل التي تأتي من المساعد.
metadata = {
user_id : "abc123"
}
message = client . messages . modify (
id : message_id ,
thread_id : thread_id ,
parameters : { metadata : metadata } ,
)
في أي وقت يمكنك إدراج جميع عمليات التشغيل التي تم تنفيذها على موضوع معين أو تعمل حاليًا:
client . runs . list ( thread_id : thread_id , parameters : { order : "asc" , limit : 3 } )
يمكنك أيضًا إنشاء موضوع وتشغيله في مكالمة واحدة مثل هذا:
response = client . runs . create_thread_and_run ( parameters : { assistant_id : assistant_id } )
run_id = response [ 'id' ]
thread_id = response [ 'thread_id' ]
يمكنك تضمين الصور في موضوع وسيتم وصفها وقراءتها بواسطة LLM. في هذا المثال ، أستخدم هذا الملف:
require "openai"
# Make a client
client = OpenAI :: Client . new (
access_token : "access_token_goes_here" ,
log_errors : true # Don't log errors in production.
)
# Upload image as a file
file_id = client . files . upload (
parameters : {
file : "path/to/example.png" ,
purpose : "assistants" ,
}
) [ "id" ]
# Create assistant (You could also use an existing one here)
assistant_id = client . assistants . create (
parameters : {
model : "gpt-4o" ,
name : "Image reader" ,
instructions : "You are an image describer. You describe the contents of images." ,
}
) [ "id" ]
# Create thread
thread_id = client . threads . create [ "id" ]
# Add image in message
client . messages . create (
thread_id : thread_id ,
parameters : {
role : "user" , # Required for manually created messages
content : [
{
"type" : "text" ,
"text" : "What's in this image?"
} ,
{
"type" : "image_file" ,
"image_file" : { "file_id" : file_id }
}
]
}
)
# Run thread
run_id = client . runs . create (
thread_id : thread_id ,
parameters : { assistant_id : assistant_id }
) [ "id" ]
# Wait until run in complete
status = nil
until status == "completed" do
sleep ( 0.1 )
status = client . runs . retrieve ( id : run_id , thread_id : thread_id ) [ 'status' ]
end
# Get the response
messages = client . messages . list ( thread_id : thread_id , parameters : { order : 'asc' } )
messages . dig ( "data" , - 1 , "content" , 0 , "text" , "value" )
=> "The image contains a placeholder graphic with a tilted, stylized representation of a postage stamp in the top part, which includes an abstract landscape with hills and a sun. Below the stamp, in the middle of the image, there is italicized text in a light golden color that reads, " This is just an example. " The background is a light pastel shade, and a yellow border frames the entire image."
في حالة السماح للمساعد بالوصول إلى أدوات function
(يتم تعريفها بنفس الطريقة التي تتم بها الوظائف أثناء إكمال الدردشة) ، فقد تحصل على رمز حالة requires_action
عندما يريد المساعد أن تقوم بتقييم واحدة أو أكثر من أدوات الوظائف:
def get_current_weather ( location : , unit : "celsius" )
# Your function code goes here
if location =~ /San Francisco/i
return unit == "celsius" ? "The weather is nice ? at 27°C" : "The weather is nice ? at 80°F"
else
return unit == "celsius" ? "The weather is icy ? at -5°C" : "The weather is icy ? at 23°F"
end
end
if status == 'requires_action'
tools_to_call = response . dig ( 'required_action' , 'submit_tool_outputs' , 'tool_calls' )
my_tool_outputs = tools_to_call . map { | tool |
# Call the functions based on the tool's name
function_name = tool . dig ( 'function' , 'name' )
arguments = JSON . parse (
tool . dig ( "function" , "arguments" ) ,
{ symbolize_names : true } ,
)
tool_output = case function_name
when "get_current_weather"
get_current_weather ( ** arguments )
end
{
tool_call_id : tool [ 'id' ] ,
output : tool_output ,
}
}
client . runs . submit_tool_outputs (
thread_id : thread_id ,
run_id : run_id ,
parameters : { tool_outputs : my_tool_outputs }
)
end
لاحظ أن لديك 10 دقائق لإرسال إخراج الأداة الخاص بك قبل انتهاء تشغيل التشغيل.
خذ نفسا عميقا. قد تحتاج إلى مشروب لهذا واحد.
من الممكن أن يشارك Openai الأجزاء التي استخدمتها في خط أنابيب RAT الداخلي لإنشاء نتائج Filesearch.
يمكن العثور على مثال على المواصفات هنا ، فقط حتى تعرف أنه ممكن.
إليك كيفية الحصول على القطع المستخدمة في بحث الملف. في هذا المثال ، أستخدم هذا الملف:
require "openai"
# Make a client
client = OpenAI :: Client . new (
access_token : "access_token_goes_here" ,
log_errors : true # Don't log errors in production.
)
# Upload your file(s)
file_id = client . files . upload (
parameters : {
file : "path/to/somatosensory.pdf" ,
purpose : "assistants"
}
) [ "id" ]
# Create a vector store to store the vectorised file(s)
vector_store_id = client . vector_stores . create ( parameters : { } ) [ "id" ]
# Vectorise the file(s)
vector_store_file_id = client . vector_store_files . create (
vector_store_id : vector_store_id ,
parameters : { file_id : file_id }
) [ "id" ]
# Check that the file is vectorised (wait for status to be "completed")
client . vector_store_files . retrieve ( vector_store_id : vector_store_id , id : vector_store_file_id ) [ "status" ]
# Create an assistant, referencing the vector store
assistant_id = client . assistants . create (
parameters : {
model : "gpt-4o" ,
name : "Answer finder" ,
instructions : "You are a file search tool. Find the answer in the given files, please." ,
tools : [
{ type : "file_search" }
] ,
tool_resources : {
file_search : {
vector_store_ids : [ vector_store_id ]
}
}
}
) [ "id" ]
# Create a thread with your question
thread_id = client . threads . create ( parameters : {
messages : [
{ role : "user" ,
content : "Find the description of a nociceptor." }
]
} ) [ "id" ]
# Run the thread to generate the response. Include the "GIVE ME THE CHUNKS" incantation.
run_id = client . runs . create (
thread_id : thread_id ,
parameters : {
assistant_id : assistant_id
} ,
query_parameters : { include : [ "step_details.tool_calls[*].file_search.results[*].content" ] } # incantation
) [ "id" ]
# Get the steps that happened in the run
steps = client . run_steps . list (
thread_id : thread_id ,
run_id : run_id ,
parameters : { order : "asc" }
)
# Retrieve all the steps. Include the "GIVE ME THE CHUNKS" incantation again.
steps = steps [ "data" ] . map do | step |
client . run_steps . retrieve (
thread_id : thread_id ,
run_id : run_id ,
id : step [ "id" ] ,
parameters : { include : [ "step_details.tool_calls[*].file_search.results[*].content" ] } # incantation
)
end
# Now we've got the chunk info, buried deep. Loop through the steps and find chunks if included:
chunks = steps . flat_map do | step |
included_results = step . dig ( "step_details" , "tool_calls" , 0 , "file_search" , "results" )
next if included_results . nil? || included_results . empty?
included_results . flat_map do | result |
result [ "content" ] . map do | content |
content [ "text" ]
end
end
end . compact
# The first chunk will be the closest match to the prompt. Finally, if you want to view the completed message(s):
client . messages . list ( thread_id : thread_id )
إنشاء الصور باستخدام dall · e 2 أو dall · e 3!
بالنسبة إلى dall · e 2 ، يجب أن يكون حجم أي صور تم إنشاؤها واحدة من 256x256
أو 512x512
أو 1024x1024
- إذا لم يتم تحديدها ، فستكون الصورة افتراضًا إلى 1024x1024
.
response = client . images . generate (
parameters : {
prompt : "A baby sea otter cooking pasta wearing a hat of some sort" ,
size : "256x256" ,
}
)
puts response . dig ( "data" , 0 , "url" )
# => "https://oaidalleapiprodscus.blob.core.windows.net/private/org-Rf437IxKhh..."
بالنسبة إلى dall · e 3 ، يجب أن يكون حجم أي صور تم إنشاؤها واحدة من 1024x1024
أو 1024x1792
أو 1792x1024
. بالإضافة إلى ذلك ، يمكن تحديد جودة الصورة إما إلى standard
أو hd
.
response = client . images . generate (
parameters : {
prompt : "A springer spaniel cooking pasta wearing a hat of some sort" ,
model : "dall-e-3" ,
size : "1024x1792" ,
quality : "standard" ,
}
)
puts response . dig ( "data" , 0 , "url" )
# => "https://oaidalleapiprodscus.blob.core.windows.net/private/org-Rf437IxKhh..."
املأ الجزء الشفاف من الصورة ، أو تحميل قناع بأقسام شفافة للإشارة إلى أجزاء الصورة التي يمكن تغييرها وفقًا لمطالبك ...
response = client . images . edit (
parameters : {
prompt : "A solid red Ruby on a blue background" ,
image : "image.png" ,
mask : "mask.png" ,
}
)
puts response . dig ( "data" , 0 , "url" )
# => "https://oaidalleapiprodscus.blob.core.windows.net/private/org-Rf437IxKhh..."
إنشاء اختلافات n من الصورة.
response = client . images . variations ( parameters : { image : "image.png" , n : 2 } )
puts response . dig ( "data" , 0 , "url" )
# => "https://oaidalleapiprodscus.blob.core.windows.net/private/org-Rf437IxKhh..."
تمرير سلسلة للتحقق مما إذا كانت تنتهك سياسة محتوى Openai:
response = client . moderations ( parameters : { input : "I'm worried about that." } )
puts response . dig ( "results" , 0 , "category_scores" , "hate" )
# => 5.505014632944949e-05
Whisper هو خطاب نصي على نموذج يمكن استخدامه لإنشاء نص بناءً على ملفات الصوت:
تأخذ API ترجمات API كإدخال ملف الصوت في أي من اللغات المدعومة ونسخ الصوت إلى اللغة الإنجليزية.
response = client . audio . translate (
parameters : {
model : "whisper-1" ,
file : File . open ( "path_to_file" , "rb" ) ,
}
)
puts response [ "text" ]
# => "Translation of the text"
يأخذ API Transcriptions كإدخال ملف الصوت الذي تريد نسخه وإرجاع النص بتنسيق ملف الإخراج المطلوب.
يمكنك تمرير لغة ملف الصوت لتحسين جودة النسخ. اللغات المدعومة مدرجة هنا. تحتاج إلى توفير اللغة كرمز ISO-639-1 ، على سبيل المثال. "EN" للغة الإنجليزية أو "NE" لنيبالي. يمكنك البحث عن الرموز هنا.
response = client . audio . transcribe (
parameters : {
model : "whisper-1" ,
file : File . open ( "path_to_file" , "rb" ) ,
language : "en" , # Optional
}
)
puts response [ "text" ]
# => "Transcription of the text"
يأخذ واجهة برمجة تطبيقات الكلام كإدخال النص والصوت ويعيد محتوى ملف الصوت الذي يمكنك الاستماع إليه.
response = client . audio . speech (
parameters : {
model : "tts-1" ,
input : "This is a speech test!" ,
voice : "alloy" ,
response_format : "mp3" , # Optional
speed : 1.0 , # Optional
}
)
File . binwrite ( 'demo.mp3' , response )
# => mp3 file that plays: "This is a speech test!"
يمكن القبض على أخطاء HTTP مثل هذا:
begin
OpenAI :: Client . new . models . retrieve ( id : "gpt-4o" )
rescue Faraday :: Error => e
raise "Got a Faraday error: #{ e } "
end
بعد التحقق من الريبو ، قم بتشغيل bin/setup
لتثبيت التبعيات. يمكنك تشغيل bin/console
للحصول على مطالبة تفاعلية تتيح لك التجربة.
لتثبيت هذه الأحجار الكريمة على جهازك المحلي ، قم بتشغيل bundle exec rake install
.
لتشغيل جميع الاختبارات ، قم بتنفيذ bundle exec rake
، والتي ستقوم أيضًا بتشغيل Linter (Rubocop). يستخدم هذا المستودع VCR لتسجيل طلبات API.
تحذير
إذا كان لديك OPENAI_ACCESS_TOKEN
في ENV
الخاص بك ، فسيستخدم تشغيل المواصفات هذا لتشغيل المواصفات مقابل واجهة برمجة التطبيقات الفعلية ، والتي ستكون بطيئة وتكلفك المال - سنتان أو أكثر! قم بإزالته من بيئتك مع unset
أو ما شابه إذا كنت ترغب فقط في تشغيل المواصفات مقابل استجابات VCR المخزنة.
قم بتشغيل المواصفات أولاً بدون VCR حتى تضغطوا بالفعل على واجهة برمجة التطبيقات. هذا سيكلف 2 سنت أو أكثر. قم بتعيين Openai_access_token في بيئتك أو مررها مثل هذا:
OPENAI_ACCESS_TOKEN=123abc bundle exec rspec
bundle exec rake release
قم CHANGELOG.md
رقم الإصدار bundle install
version.rb
ملف .gem
إلى rubygems.org.
يتم الترحيب بتقارير الأخطاء وطلبات السحب على Github على https://github.com/alexrudall/ruby-openai. يهدف هذا المشروع إلى أن يكون مساحة آمنة ومرحبة للتعاون ، ومن المتوقع أن يلتزم المساهمون بمدونة قواعد السلوك.
الجوهرة متوفرة كمصدر مفتوح بموجب شروط ترخيص معهد ماساتشوستس للتكنولوجيا.
من المتوقع أن يتبع كل من يتفاعلون في قواعد الكود الخاصة بمشروع Ruby Openai ، ومتتبعات المشكلات ، وغرف الدردشة والقوائم البريدية مدونة قواعد السلوك.