Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/sublayer/components/output_adapters/formattable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def format_property(property)
result[:items] = property.items.is_a?(OpenStruct) ? format_property(property.items) : property.items
when 'object'
result[:properties] = build_json_schema(property.properties) if property.properties
result[:additionalProperties] = false if property.properties
result[:required] = property.properties.select(&:required).map(&:name) if property.properties
end

Expand Down
36 changes: 14 additions & 22 deletions lib/sublayer/providers/open_ai.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Sublayer.configuration.ai_provider = Sublayer::Providers::OpenAI
# Sublayer.configuration.ai_model = "gpt-4o"
# Sublayer.configuration.ai_model = "gpt-4o-2024-08-06"

module Sublayer
module Providers
Expand All @@ -26,22 +26,21 @@ def self.call(prompt:, output_adapter:)
"content": prompt
}
],
tool_choice: { type: "function", function: { name: output_adapter.name }},
tools: [
{
type: "function",
function: {
name: output_adapter.name,
description: output_adapter.description,
parameters: {
type: "object",
properties: output_adapter.format_properties
},
response_format: {
type: "json_schema",
json_schema: {
name: "response",
strict: true,
schema: {
type: "object",
additionalProperties: false,
properties: output_adapter.format_properties,
required: output_adapter.format_required
}
}
]
})
}
}
)

after_request = Time.now
response_time = after_request - before_request
Expand All @@ -58,14 +57,7 @@ def self.call(prompt:, output_adapter:)

message = response.dig("choices", 0, "message")

raise "No function called" unless message["tool_calls"]

function_body = message.dig("tool_calls", 0, "function", "arguments")

raise "Error generating with OpenAI. Empty response. Try rewording your output adapter params to be from the perspective of the model. Full Response: #{response}" if function_body == "{}"
raise "Error generating with OpenAI. Error: Max tokens exceeded. Try breaking your problem up into smaller pieces." if response["choices"][0]["finish_reason"] == "length"

results = JSON.parse(function_body)[output_adapter.name]
JSON.parse(message["content"])[output_adapter.name]
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions spec/components/output_adapters/formattable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def add_property(property)
name: { type: 'string', description: 'The name' },
age: { type: 'integer', description: 'The age' }
},
additionalProperties: false,
required: ['name']
}
})
Expand Down Expand Up @@ -72,9 +73,11 @@ def add_property(property)
state: { type: 'string', description: 'The state' },
zip: { type: 'string', description: 'The zip' }
},
additionalProperties: false,
required: ['street', 'city', 'state', 'zip']
}
},
additionalProperties: false,
required: ['name', 'address']
}
})
Expand Down
2 changes: 1 addition & 1 deletion spec/generators/product_description_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def generate
context "OpenAI" do
before do
Sublayer.configuration.ai_provider = Sublayer::Providers::OpenAI
Sublayer.configuration.ai_model = "gpt-4o"
Sublayer.configuration.ai_model = "gpt-4o-2024-08-06"
end

it "generates an with the correct keys" do
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
---
http_interactions:
- request:
method: post
uri: https://api.openai.com/v1/chat/completions
body:
encoding: UTF-8
string: '{"model":"gpt-4o","messages":[{"role":"user","content":" You are
a skilled product copywriter. Create compelling product descriptions for the
following:\n\n Product Name: Super Gadget\n Product Category: Electronics\n\n Please
provide the following:\n 1. A brief one-sentence description of the product\n 2.
A detailed paragraph describing the product\n 3. A comma-separated list
of key product features\n 4. A brief description of the target audience
for this product\n"}],"tool_choice":{"type":"function","function":{"name":"product_description"}},"tools":[{"type":"function","function":{"name":"product_description","description":"Generate
product descriptions","parameters":{"type":"object","properties":{"product_description":{"type":"object","description":"Generate
product descriptions","properties":{"short_description":{"type":"string","description":"A
brief one-sentence description of the product"},"long_description":{"type":"string","description":"A
detailed paragraph describing the product"},"key_features":{"type":"string","description":"A
comma-separated list of key product features"},"target_audience":{"type":"string","description":"A
brief description of the target audience for this product"}}}}},"required":["product_description"]}}]}'
headers:
Content-Type:
- application/json
Authorization:
- Bearer <OPENAI_API_KEY>
Accept-Encoding:
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
Accept:
- "*/*"
User-Agent:
- Ruby
response:
status:
code: 200
message: OK
headers:
Date:
- Tue, 13 Aug 2024 16:57:16 GMT
Content-Type:
- application/json
Transfer-Encoding:
- chunked
Connection:
- keep-alive
Openai-Organization:
- sublayer
Openai-Processing-Ms:
- '5135'
Openai-Version:
- '2020-10-01'
Strict-Transport-Security:
- max-age=15552000; includeSubDomains; preload
X-Ratelimit-Limit-Requests:
- '10000'
X-Ratelimit-Limit-Tokens:
- '2000000'
X-Ratelimit-Remaining-Requests:
- '9999'
X-Ratelimit-Remaining-Tokens:
- '1999876'
X-Ratelimit-Reset-Requests:
- 6ms
X-Ratelimit-Reset-Tokens:
- 3ms
X-Request-Id:
- req_a2609d47efd3d93efb4b49f732f80cf3
Cf-Cache-Status:
- DYNAMIC
Set-Cookie:
- __cf_bm=7Ica4pVcKjtC87CDqFWe9ACh5nnru2ZtPKDEzgZu07I-1723568236-1.0.1.1-vNRQTKeJjVisZPMxaiPnk4CttN79OHcvpiiAEs0nbPUM3aV.kgYdSe76scUSoX76JqCnq2gOKKLTZmbpLJWIyw;
path=/; expires=Tue, 13-Aug-24 17:27:16 GMT; domain=.api.openai.com; HttpOnly;
Secure; SameSite=None
- _cfuvid=LmqaMgx9YJL8j2hpt5srCyNGXHA75S9A6YHD9mlWYjU-1723568236196-0.0.1.1-604800000;
path=/; domain=.api.openai.com; HttpOnly; Secure; SameSite=None
X-Content-Type-Options:
- nosniff
Server:
- cloudflare
Cf-Ray:
- 8b2a3e22cd2b41d3-EWR
Alt-Svc:
- h3=":443"; ma=86400
body:
encoding: ASCII-8BIT
string: |
{
"id": "chatcmpl-9vp7XvcmCFU7p7BOOHiSrMJEowJJh",
"object": "chat.completion",
"created": 1723568231,
"model": "gpt-4o-2024-05-13",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": null,
"tool_calls": [
{
"id": "call_drxKY5ciXdx4F9glwtn9qk2I",
"type": "function",
"function": {
"name": "product_description",
"arguments": "{\"product_description\":{\"short_description\":\"Revolutionize your daily tasks with the cutting-edge Super Gadget.\",\"long_description\":\"The Super Gadget is the ultimate electronic companion designed to make your life easier, more efficient, and enjoyable. Whether you need it for work, leisure, or staying connected, this all-in-one device combines advanced technology with user-friendly features. Boasting a sleek design and robust performance, the Super Gadget seamlessly integrates with your daily routine, offering unparalleled functionality at your fingertips. With its long battery life, high-definition display, and intuitive interface, this gadget is perfect for tech enthusiasts and casual users alike. Embrace the future of convenience with the Super Gadget and transform the way you interact with technology.\",\"key_features\":\"sleek design, robust performance, long battery life, high-definition display, intuitive interface, all-in-one functionality\",\"target_audience\":\"tech enthusiasts, casual users, busy professionals, students, gadget lovers\"}}"
}
}
],
"refusal": null
},
"logprobs": null,
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 203,
"completion_tokens": 186,
"total_tokens": 389
},
"system_fingerprint": "fp_3aa7262c27"
}
recorded_at: Tue, 13 Aug 2024 16:57:16 GMT
recorded_with: VCR 6.2.0