Skip to content

Commit

Permalink
Merge pull request #2 from shohey1226/schema_with_funcation_calling
Browse files Browse the repository at this point in the history
Schema with funcation calling
  • Loading branch information
shohey1226 committed Jul 2, 2023
2 parents b148ca3 + 68c67a9 commit bf00c21
Show file tree
Hide file tree
Showing 3 changed files with 254 additions and 0 deletions.
40 changes: 40 additions & 0 deletions lib/llm_memory/broca.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,46 @@ def respond(args)
end
end

def respond_with_schema(context: {}, schema: {})
response_content = respond(context)
begin
response = client.chat(
parameters: {
model: "gpt-3.5-turbo-0613", # as of July 3, 2023
messages: [
{
role: "user",
content: response_content
}
],
functions: [
{
name: "broca",
description: "Formating the content with the specified schema",
parameters: schema
}
]
}
)
LlmMemory.logger.debug(response)
message = response.dig("choices", 0, "message")
if message["role"] == "assistant" && message["function_call"]
function_name = message.dig("function_call", "name")
args =
JSON.parse(
message.dig("function_call", "arguments"),
{symbolize_names: true}
)
if function_name == "broca"
args
end
end
rescue => e
LlmMemory.logger.info(e.inspect)
nil
end
end

def generate_prompt(args)
erb = ERB.new(@prompt)
erb.result_with_hash(args)
Expand Down

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

19 changes: 19 additions & 0 deletions spec/llm_memory/broca_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,24 @@
res = broca.respond(related_docs: related_docs, query_str: "what is my name?")
expect(res).to include("Shohei")
end

it "runs respond_with_schema method", :vcr do
related_docs = [{content: "My name is Shohei"}, {content: "I'm a software engineer"}]
broca = LlmMemory::Broca.new(prompt: template)
res = broca.respond_with_schema(
context: {related_docs: related_docs, query_str: "what is my name?"},
schema: {
type: :object,
properties: {
name: {
type: :string,
description: "The name of person"
}
},
required: ["name"]
}
)
expect(res).to include({name: "Shohei"})
end
end
end

0 comments on commit bf00c21

Please sign in to comment.