liveBook · Manning

Semantic function

As explained in AI Agents in Action

A semantic function is a function whose behavior is specified by a prompt or profile and executed through interaction with a large language model.

A semantic function packages a prompt or profile as an executable function whose behavior is produced through interaction with a large language model. It can run independently or become part of a plugin or skill. section 5.3.1 section 5.4

With Semantic Kernel, create a kernel, connect an OpenAI or Azure OpenAI chat-completion service, define a prompt template, and invoke the resulting function asynchronously. Context variables let the same prompt accept values such as subject, genre, format, and custom instructions. section 5.3.1 section 5.3.2

Listing 5.10 SK_context_variables.py
#top section omitted…
prompt = """     #1
system:

You have vast knowledge of everything and can recommend anything provided 
you are given the following criteria, the subject, genre, format and any 
other custom information.

user:
Please recommend a {{$format}} with the subject {{$subject}} and {{$genre}}.
Include the following custom information: {{$custom}}
"""

prompt_template_config = sk.PromptTemplateConfig(     #2
    template=prompt,
    name="tldr",
    template_format="semantic-kernel",
    input_variables=[
        InputVariable(
            name="format", 
            description="The format to recommend", 
            is_required=True
        ),
        InputVariable(
            name="suject", 
            description="The subject to recommend", 
            is_required=True
        ),
        InputVariable(
            name="genre", 
            description="The genre to recommend", 
            is_required=True
        ),
        InputVariable(
            name="custom",
            description="Any custom information [CA]
                       to enhance the recommendation",
            is_required=True,
        ),
    ],
    execution_settings=execution_settings,
)

recommend_function = kernel.create_function_from_prompt(     #3
    prompt_template_config=prompt_template_config,
    function_name="Recommend_Movies",
    plugin_name="Recommendation",
)

async def run_recommendation(     #4
    subject="time travel",
    format="movie", 
    genre="medieval", 
           custom="must be a comedy"
):
    recommendation = await kernel.invoke(
        recommend_function,
        sk.KernelArguments(subject=subject,
                      format=format, 
                      genre=genre, 
                      custom=custom),     #5
    )
    print(recommendation)


# Use asyncio.run to execute the async function
asyncio.run(run_recommendation())    #5

###Output
One movie that fits the criteria of being about time travel, set in a 
medieval period, and being a comedy is "The Visitors" (Les Visiteurs) 
from 1993. This French film, directed by Jean-Marie Poiré, follows a 
knight and his squire who are transported to the modern era by a 
wizard’s spell gone wrong.…

Inspect the prompt template showing system and user instructions, required input variables, and Semantic Kernel placeholder syntax. section 5.3.2

A semantic function can be organized and extended in several ways:

  • Store it as a plugin or skill with a configuration file describing completion settings and inputs, plus an skprompt.txt file containing the prompt. section 5.4.1
  • Register the plugin with the kernel so it can be invoked directly or made available through an LLM chat or agent interface. section 5.4.1
  • Compose it with native functions when prompts need code-based actions such as loading information from a database or using an API. section 5.4.3
  • Expose it as an agent action whose prompt-derived description helps an LLM select the function through tool calling. section 7.5

Movie recommendation function

A Recommend_Movies semantic function receives a list of previously watched films and prompts the LLM to recommend an unseen movie. A more advanced version embeds a native function that loads the watched-movie list, allowing the semantic function to run without requiring the user to provide it. section 5.4.1 section 5.4.3

Practice tasks include:

  • Create a creative function that writes a poem or tells a children's story from user input. section 5.7
  • Combine a meal-planning semantic function with a native nutrition lookup function. section 5.7

How AI Agents in Action covers it

How deep it goes
Explains templates, variables, plugins, invocation, and composition with native functions, but does not detail model internals.
Examples
Uses movie recommendations, creative writing, meal planning, database lookup, and tree-of-thought answer evaluation.
What it assumes
Assumes a configured Python workspace and environment variables for an OpenAI or Azure OpenAI service.
What you can do after
Build it from scratch with Semantic Kernel, parameterize prompts, register plugins, invoke functions, and combine prompts with code actions.
How it connects
Connects prompt templates, contextual variables, plugins, native functions, tool calling, Semantic Kernel, and agent interfaces.

Also explained in

Compare how 2 books explain semantic function →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI Agents in Action. The section links point to the parts of the book it draws on.