> ## Documentation Index
> Fetch the complete documentation index at: https://docs.morph-data.io/llms.txt
> Use this file to discover all available pages before exploring further.

# text_to_sql

<Warning>
  The `morph_lib.ai` package has been deprecated since v0.1.5 to reduce the size of the morph-data package.

  It is planned to be provided in the form of an external adapter in the future.
</Warning>

Generates SQL from a given prompt.

<CodeGroup>
  ```python OpenAI theme={"dark"}
  # 📦 Package: morph_lib.ai.openai.code_generation
  # 🛠️ Function: text_to_sql

  def text_to_sql(
      prompt: str,
      api_key: str,
      table_names: List[str],
      model: Optional[str] = "gpt-4o",
      connection: Optional[str] = None,
      schema_name: Optional[str] = None,
  ) -> TextConversionResponse:
  ```

  ```python Anthropic theme={"dark"}
  # 📦 Package: morph_lib.ai.anthropic.code_generation
  # 🛠️ Function: text_to_sql

  def text_to_sql(
      prompt: str,
      api_key: str,
      table_names: List[str],
      model: Optional[str] = "claude-3-5-haiku-latest",
      max_tokens: Optional[int] = 1024,
      connection: Optional[str] = None,
      schema_name: Optional[str] = None,
  ) -> TextConversionResponse:
  ```

  ```python Azure theme={"dark"}
  # 📦 Package: morph_lib.ai.azure.code_generation
  # 🛠️ Function: text_to_sql

  def text_to_sql(
      prompt: str,
      api_key: str,
      table_names: List[str],
      azure_endpoint: str,
      deployment_name: Optional[str] = "gpt4",
      api_version: Optional[str] = "2024-10-01-preview",
      connection: Optional[str] = None,
      schema_name: Optional[str] = None,
  ) -> TextConversionResponse:
  ```

  ```python Groq theme={"dark"}
  # 📦 Package: morph_lib.ai.groq.code_generation
  # 🛠️ Function: text_to_sql

  def text_to_sql(
      prompt: str,
      api_key: str,
      table_names: List[str],
      model: Optional[str] = "llama-3.1-70b-versatile",
      connection: Optional[str] = None,
      schema_name: Optional[str] = None,
  ) -> TextConversionResponse:
  ```
</CodeGroup>

### Parameters

These are the parameters for OpenAI. Set the authentication parameters for other models as appropriate from the function definitions above.

<ParamField body="prompt" type="string" required>
  Prompt
</ParamField>

<ParamField body="api_key" type="string" required>
  API key for using the LLM model
</ParamField>

<ParamField body="table_names" type="list" required>
  List of table names used for generation
</ParamField>

<ParamField body="model" type="string" optional>
  LLM model to use
</ParamField>

<ParamField body="connection" type="string" optional>
  Connection used for generation
</ParamField>

<ParamField body="schema_name" type="string" optional>
  Schema name used for generation
</ParamField>

### Example

```python theme={"dark"}
from morph_lib.ai.openai import text_to_sql
import os

@morph.func
def func_name(context):
    sql = text_to_sql(
		prompt,
		os.environ["OPENAI_API_KEY"],
		["user"],
		"gpt-4o",
	)
	return execute_sql(sql)
```
